public static ILifetimeScope CreateAutofacContainer(this IServiceCollection services, IConfigurationRoot configuration)
        {
            var builder = new ContainerBuilder();

            // Load web specific dependencies
            builder.RegisterType<AuthMessageSender>()
                .As<IEmailSender>().InstancePerLifetimeScope();
            builder.RegisterAssemblyTypes(typeof(Startup).GetTypeInfo().Assembly).AsImplementedInterfaces();
            builder.RegisterAssemblyTypes(typeof (CartCommandService).GetTypeInfo().Assembly, typeof (ICartCommandService).GetTypeInfo().Assembly)
                .AsImplementedInterfaces();

            var databaseInitializer = new MigrateToLatestVersion(new SampleDataSeeder());
            builder.AddDataOnion(new DbContextConfig(configuration.GetConnectionString("DefaultConnection"), typeof(MusicStoreContext), databaseInitializer));

            // Populate the container with services that were previously registered
            builder.Populate(services);

            builder.RegisterType<BaseRepository<Album, MusicStoreContext>>().As<IRepository<Album>>();
            builder.RegisterType<BaseRepository<Artist, MusicStoreContext>>().As<IRepository<Artist>>();
            builder.RegisterType<BaseRepository<Cart, MusicStoreContext>>().As<IRepository<Cart>>();
            builder.RegisterType<BaseRepository<CartItem, MusicStoreContext>>().As<IRepository<CartItem>>();
            builder.RegisterType<BaseRepository<Genre, MusicStoreContext>>().As<IRepository<Genre>>();
            builder.RegisterType<BaseRepository<Order, MusicStoreContext>>().As<IRepository<Order>>();
            builder.RegisterType<BaseRepository<OrderDetail, MusicStoreContext>>().As<IRepository<OrderDetail>>();

            var container = builder.Build();

            return container;
        }
        public static ILifetimeScope CreateAutofacContainer(this IServiceCollection services, IConfigurationRoot configuration)
        {
            var builder = new ContainerBuilder();

            // Load web specific dependencies
            builder.RegisterType <AuthMessageSender>()
            .As <IEmailSender>().InstancePerLifetimeScope();
            builder.RegisterAssemblyTypes(typeof(Startup).GetTypeInfo().Assembly).AsImplementedInterfaces();
            builder.RegisterAssemblyTypes(typeof(CartCommandService).GetTypeInfo().Assembly, typeof(ICartCommandService).GetTypeInfo().Assembly)
            .AsImplementedInterfaces();

            var databaseInitializer = new MigrateToLatestVersion(new SampleDataSeeder());

            builder.AddDataOnion(new DbContextConfig(configuration.GetConnectionString("DefaultConnection"), typeof(MusicStoreContext), databaseInitializer));

            // Populate the container with services that were previously registered
            builder.Populate(services);

            builder.RegisterType <BaseRepository <Album, MusicStoreContext> >().As <IRepository <Album> >();
            builder.RegisterType <BaseRepository <Artist, MusicStoreContext> >().As <IRepository <Artist> >();
            builder.RegisterType <BaseRepository <Cart, MusicStoreContext> >().As <IRepository <Cart> >();
            builder.RegisterType <BaseRepository <CartItem, MusicStoreContext> >().As <IRepository <CartItem> >();
            builder.RegisterType <BaseRepository <Genre, MusicStoreContext> >().As <IRepository <Genre> >();
            builder.RegisterType <BaseRepository <Order, MusicStoreContext> >().As <IRepository <Order> >();
            builder.RegisterType <BaseRepository <OrderDetail, MusicStoreContext> >().As <IRepository <OrderDetail> >();

            var container = builder.Build();

            return(container);
        }
Пример #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            var databaseInitializer = new MigrateToLatestVersion(new SampleDataSeeder());

            services.AddDataOnion(new DbContextConfig(
                                      this.Configuration["Data:DefaultConnection:ConnectionString"],
                                      typeof(SchoolDbContext), databaseInitializer));

            services.AddTransient <IRepository <Address>, BaseRepository <Address, SchoolDbContext> >();
            services.AddTransient <IRepository <School>, BaseRepository <School, SchoolDbContext> >();
            services.AddTransient <IRepository <Student>, BaseRepository <Student, SchoolDbContext> >();
            services.AddTransient <ISchoolQueryService, SchoolQueryService>();
        }