Created
April 29, 2015 06:20
-
-
Save SirwanAfifi/fc565a417a5468dd4348 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SmObjectFactory | |
{ | |
private static readonly Lazy<Container> _containerBuilder = | |
new Lazy<Container>(defaultContainer, LazyThreadSafetyMode.ExecutionAndPublication); | |
public static IContainer Container | |
{ | |
get { return _containerBuilder.Value; } | |
} | |
private static Container defaultContainer() | |
{ | |
var container = new Container(ioc => | |
{ | |
ioc.AddRegistry<AutomapperRegistry>(); | |
ioc.Scan(scan => | |
{ | |
scan.TheCallingAssembly(); | |
scan.WithDefaultConventions(); | |
scan.AddAllTypesOf<Profile>().NameBy(item => item.FullName); | |
}); | |
ioc.For<IUnitOfWork>() | |
.HybridHttpOrThreadLocalScoped() | |
.Use<ApplicationDbContext>() | |
// Remove these 2 lines if you want to use a connection string named connectionString1, defined in the web.config file. | |
.Ctor<string>("connectionString") | |
.Is("Data Source=(local);Initial Catalog=TestDbIdentity;Integrated Security = true"); | |
ioc.For<ApplicationDbContext>().HybridHttpOrThreadLocalScoped() | |
.Use(context => (ApplicationDbContext)context.GetInstance<IUnitOfWork>()); | |
ioc.For<DbContext>().HybridHttpOrThreadLocalScoped() | |
.Use(context => (ApplicationDbContext)context.GetInstance<IUnitOfWork>()); | |
ioc.For<IUserStore<ApplicationUser, int>>() | |
.HybridHttpOrThreadLocalScoped() | |
.Use<UserStore<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>>(); | |
ioc.For<IRoleStore<CustomRole, int>>() | |
.HybridHttpOrThreadLocalScoped() | |
.Use<RoleStore<CustomRole, int, CustomUserRole>>(); | |
ioc.For<IAuthenticationManager>() | |
.Use(() => HttpContext.Current.GetOwinContext().Authentication); | |
ioc.For<IApplicationSignInManager>() | |
.HybridHttpOrThreadLocalScoped() | |
.Use<ApplicationSignInManager>(); | |
ioc.For<IApplicationRoleManager>() | |
.HybridHttpOrThreadLocalScoped() | |
.Use<ApplicationRoleManager>(); | |
// map same interface to different concrete classes | |
ioc.For<IIdentityMessageService>().Use<SmsService>(); | |
ioc.For<IIdentityMessageService>().Use<EmailService>(); | |
ioc.For<IApplicationUserManager>().HybridHttpOrThreadLocalScoped() | |
.Use<ApplicationUserManager>() | |
.Ctor<IIdentityMessageService>("smsService").Is<SmsService>() | |
.Ctor<IIdentityMessageService>("emailService").Is<EmailService>() | |
.Setter<IIdentityMessageService>(userManager => userManager.SmsService).Is<SmsService>() | |
.Setter<IIdentityMessageService>(userManager => userManager.EmailService).Is<EmailService>(); | |
ioc.For<ApplicationUserManager>().HybridHttpOrThreadLocalScoped() | |
.Use(context => (ApplicationUserManager)context.GetInstance<IApplicationUserManager>()); | |
ioc.For<ICustomRoleStore>() | |
.HybridHttpOrThreadLocalScoped() | |
.Use<CustomRoleStore>(); | |
ioc.For<ICustomUserStore>() | |
.HybridHttpOrThreadLocalScoped() | |
.Use<CustomUserStore>(); | |
//config.For<IDataProtectionProvider>().Use(()=> app.GetDataProtectionProvider()); // In Startup class | |
ioc.For<ITagService>().Use<TagService>(); | |
ioc.For<IAdvertismentService>().Use<AdvertismentService>(); | |
// other configs | |
}); | |
configureAutoMapper(container); | |
return container; | |
} | |
private static void configureAutoMapper(IContainer container) | |
{ | |
var configuration = container.TryGetInstance<IConfiguration>(); | |
if (configuration == null) return; | |
//saying AutoMapper how to resolve services | |
configuration.ConstructServicesUsing(container.GetInstance); | |
foreach (var profile in container.GetAllInstances<Profile>()) | |
{ | |
configuration.AddProfile(profile); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment