Exemplo n.º 1
0
        public static IContainer Initialize()
        {
            var builder  = new ContainerBuilder();
            var assembly = typeof(Startup).Assembly;

            builder.RegisterApiControllers(assembly);

            builder.RegisterType <ApplicationDbContext>().As <IAppContext>();
            builder.RegisterType <AppConfig>().As <IAppConfig>();
            builder.RegisterType <CacheService>().As <ICacheService>();
            builder.RegisterType <JsonConfigSerializer>().As <IConfigSerializer>();
            builder.RegisterType <DefaultServiceSettings>().As <IServiceSettings>();
            builder.RegisterType <ApplicationOAuthProvider>().AsSelf().SingleInstance();

            builder.RegisterInstance(LogManager.GetLogger(typeof(Startup))).As <ILog>();

            //Mapper dependencies
            builder.RegisterGeneric(typeof(PagedListConverter <,>));

            // Global items registrations
            MapperRegistration.Register(builder);

            //// Register class by Name convention
            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()).Where(x => x.Name.EndsWith("Service")).AsImplementedInterfaces();

            builder.RegisterAssemblyTypes(assembly)
            .Where(x => x.Name.EndsWith("Repository"))
            .InstancePerRequest();

            return(builder.Build());
        }
Exemplo n.º 2
0
        public static void Initialize()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
            builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly).AsImplementedInterfaces();
            builder.RegisterSource(new ViewRegistrationSource());
            builder.RegisterModule(new AutofacWebTypesModule());
            builder.RegisterFilterProvider();

            builder.RegisterType <ApplicationDbContext>().As <IContext>().InstancePerRequest();

            builder.RegisterType <EditablePageRepository>().AsSelf();
            builder.RegisterType <AppConfig>().As <IAppConfig>().InstancePerRequest();
            builder.RegisterType <CacheService>().As <ICacheService>().InstancePerRequest();

            builder.RegisterType <JsonConfigSerializer>().As <IConfigSerializer>().InstancePerRequest();
            builder.RegisterType <CacheService>().As <ICacheService>().InstancePerRequest();


            MapperRegistration.Register(builder);

            var logger = LogManager.GetLogger(typeof(MvcApplication));

            builder.Register(c => logger).As <ILog>().SingleInstance();

            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
            .Where(x => x.Name.EndsWith("Processor"))
            .AsImplementedInterfaces()
            .InstancePerRequest();

            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
            .Where(x => x.Name.EndsWith("Service"))
            .AsImplementedInterfaces()
            .InstancePerRequest();

            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
            .Where(x => x.Name.EndsWith("Repository"))
            .AsSelf()
            .InstancePerRequest();

            var container = builder.Build();

            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }