public void Register(ContainerBuilder builder)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();

            ProxyGenerationOptions ProxyOption = new ProxyGenerationOptions()
            {
                Selector = new InterceptorSelector()
            };
            //注册所有服务
            builder.RegisterAssemblyTypes(assembly)
               .Where(type => type.FullName.EndsWith("ApplicationService") && type.IsClass)
               .AsImplementedInterfaces()
               .EnableInterfaceInterceptors(ProxyOption)
               ;//.InterceptedBy(typeof(MethodInvacationValidateInterceptor), typeof(UnitOfWorkInterceptor));

            //注册DbContext
            builder.RegisterType<SampleEntities>().As<DbContext>();

            //注册所有泛型仓储
            var finder = new WebAppTypeFinder();
            var allEntityTypes = finder.FindClassOfType(typeof(IEntity));
            foreach (var entityType in allEntityTypes)
            {
                var genericRepositoryType = typeof(IRepository<>).MakeGenericType(entityType);
                var implType = typeof(EfRepository<,>).MakeGenericType(typeof(DbContext), entityType);
                builder.RegisterType(implType).As(genericRepositoryType);
            }

            builder.RegisterType<CallCurrentUnitOfWorkProvider>().As<ICurrentUnitOfWorkProvider>();
            builder.RegisterType<UnitOfWorkDbContextProvider<DbContext>>().As<IDbContextProvider<DbContext>>();
            builder.RegisterType<EfDbContextProvider<DbContext>>().Keyed<IDbContextProvider<DbContext>>("Default");
            builder.RegisterType<UnitOfWorkManager>().As<IUnitOfWorkManager>();
            builder.RegisterType<EfUnitOfWork>().As<IUnitOfWork>();
        }
        public void MapAllDtoWithMapAttribute()
        {
            var finder = new WebAppTypeFinder();
            var allDto = finder.FindClassOfType(typeof(IDto));
            foreach (var dto in allDto)
            {
                var mapAttribute = dto.GetCustomAttributes(false).OfType<MapToAttribute>().FirstOrDefault();
                //var mapAttribute = dto.CustomAttributes.FirstOrDefault(attr => attr.AttributeType.IsAssignableFrom(typeof(MapToAttribute)));

                if (mapAttribute != null)
                {
                    var mapTo = mapAttribute.MapToType;
                    Mapper.CreateMap(dto, mapTo);
                }
            }
        }
Example #3
0
        //核心部分的注册
        public void Init(IIocManager iocManager)
        {
            var container = iocManager.Container;
            var builder = new ContainerBuilder();

            var assemblies = AppDomain.CurrentDomain.GetAssemblies();

            //所有注册器
            var finder = new WebAppTypeFinder();
            var allRegistrars = finder.FindClassOfType(typeof(IDependencyRegistrar));
            foreach (var registrar in allRegistrars)
                (Activator.CreateInstance(registrar) as IDependencyRegistrar).Register(builder);

            //织入点配置
            var proxyOption = new ProxyGenerationOptions()
            {
                Selector = new InterceptorSelector()
            };

            //注册所有拦截器
            builder.RegisterAssemblyTypes(assemblies)
                .Where(type => type.IsAssignableTo<Castle.DynamicProxy.IInterceptor>());

            //注册单例
            builder.RegisterAssemblyTypes(assemblies)
                .EnableInterfaceInterceptors(proxyOption)
                .Where(type => type.IsAssignableTo<ISingletonDependency>())
                .AsImplementedInterfaces()
                //.Named<ISingletonDependency>(type => type.Name)
                .SingleInstance();

            //注册瞬态
            builder.RegisterAssemblyTypes(assemblies)
                .Where(type => type.IsAssignableTo<ITransientDependency>())
                .AsImplementedInterfaces()
                .InstancePerLifetimeScope()
                .EnableInterfaceInterceptors(proxyOption);

            builder.RegisterType<DefaultEventBus>().Named<IEventBus>(BusEnum.DefaultBus.GetName());
            builder.RegisterType<MsmqEventBus>().Named<IEventBus>(BusEnum.MsmqBus.GetName());

            builder.RegisterInstance<IContainer>(container);
            builder.RegisterInstance<IIocResolver>(iocManager);

            builder.Update(container);
        }