public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            var zergDataProvider = new ZergDataProvider();
            zergDataProvider.InitDatabase();

            var dataSettingsManager = new DataSettingsManager();
            var dataProviderSettings = dataSettingsManager.LoadSettings();

            //不同数据库对应不同的仓库
            if (dataProviderSettings != null && dataProviderSettings.IsValid())
            {
                builder.Register<IDbContext>(c => new CMSDbContext(dataProviderSettings.RawDataSettings["CMSConnection"])).Named<IDbContext>("CMS").InstancePerRequest();
                builder.Register<IDbContext>(c => new CRMDbContext(dataProviderSettings.RawDataSettings["CRMConnection"])).Named<IDbContext>("CRM").InstancePerRequest();
                builder.Register<IDbContext>(c => new TradingDbContext(dataProviderSettings.RawDataSettings["TradingConnection"])).Named<IDbContext>("Trading").InstancePerRequest();
                builder.Register<IDbContext>(c => new EventDbContext(dataProviderSettings.RawDataSettings["EventConnection"])).Named<IDbContext>("Event").InstancePerRequest();
                builder.Register<IDbContext>(c => new CommunityDbContext(dataProviderSettings.RawDataSettings["CommunityConnection"])).Named<IDbContext>("Community").InstancePerRequest();
            }

            #region 不同的数据库对应不同仓库
            var cmsParameter = new ResolvedParameter((pi, ctx) => pi.Name == "context",
                                            (pi, ctx) => ctx.ResolveNamed<IDbContext>("CMS"));
            var crmParameter = new ResolvedParameter((pi, ctx) => pi.Name == "context",
                                            (pi, ctx) => ctx.ResolveNamed<IDbContext>("CRM"));
            var tradingParameter = new ResolvedParameter((pi, ctx) => pi.Name == "context",
                                            (pi, ctx) => ctx.ResolveNamed<IDbContext>("Trading"));
            var eventPrameter = new ResolvedParameter((pi, ctx) => pi.Name == "context",
                                            (pi, ctx) => ctx.ResolveNamed<IDbContext>("Event"));
            var communityPrameter = new ResolvedParameter((pi, ctx) => pi.Name == "context",
                                            (pi, ctx) => ctx.ResolveNamed<IDbContext>("Community"));

            builder.RegisterGeneric(typeof(CMSRepository<>))
                .WithParameter(cmsParameter)
                .As(typeof(ICMSRepository<>))
                .InstancePerRequest();

            builder.RegisterGeneric(typeof(CRMRepository<>))
                .WithParameter(crmParameter)
                .As(typeof(ICRMRepository<>))
                .InstancePerRequest();

            builder.RegisterGeneric(typeof(TradingRepository<>))
                .WithParameter(tradingParameter)
                .As(typeof(ITradingRepository<>))
                .InstancePerRequest();

            builder.RegisterGeneric(typeof(EventRepository<>))
                .WithParameter(eventPrameter)
                .As(typeof(IEventRepository<>))
                .InstancePerRequest();


            builder.RegisterGeneric(typeof(CommunityRepository<>))
                .WithParameter(communityPrameter)
                .As(typeof(ICommunityRepository<>))
                .InstancePerRequest();
            #endregion
            //            builder.RegisterAssemblyTypes(typeFinder.GetAssemblies().ToArray())
            //                .Where(t =>!String.IsNullOrEmpty(t.Namespace)&& t.Namespace.StartsWith("CMS") && t.Name.Contains("Service"))
            //                .WithParameter(repositeryParameter);
        }
Example #2
0
 public override void InitDatabase()
 {
     var dataSettingsManager = new DataSettingsManager();
     var dataProviderSettings = dataSettingsManager.LoadSettings();
     //依据model更新数据库
     Database.SetInitializer(new EfInitializer<CMSDbContext, CMSConfiguration>(dataProviderSettings.RawDataSettings["CMSConnection"]));
     Database.SetInitializer(new EfInitializer<CRMDbContext, CRMConfiguration>(dataProviderSettings.RawDataSettings["CRMConnection"]));
     Database.SetInitializer(new EfInitializer<TradingDbContext, TradingConfiguration>(dataProviderSettings.RawDataSettings["TradingConnection"]));
     Database.SetInitializer(new EfInitializer<EventDbContext, EventConfiguration>(dataProviderSettings.RawDataSettings["EventConnection"]));
     Database.SetInitializer(new EfInitializer<CommunityDbContext, CommunityConfiguration>(dataProviderSettings.RawDataSettings["CommunityConnection"]));
 }
        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            //HTTP context and other related stuff
            builder.Register(c =>
                //register FakeHttpContext when HttpContext is not available
                //HttpContext.Current != null ?
                (new HttpContextWrapper(HttpContext.Current) as HttpContextBase)
                //(new FakeHttpContext("~/") as HttpContextBase)
                )
                .As<HttpContextBase>()
                .InstancePerRequest();
            builder.Register(c => c.Resolve<HttpContextBase>().Request)
                .As<HttpRequestBase>()
                .InstancePerRequest();
            builder.Register(c => c.Resolve<HttpContextBase>().Response)
                .As<HttpResponseBase>()
                .InstancePerRequest();
            builder.Register(c => c.Resolve<HttpContextBase>().Server)
                .As<HttpServerUtilityBase>()
                .InstancePerRequest();
            builder.Register(c => c.Resolve<HttpContextBase>().Session)
                .As<HttpSessionStateBase>()
                .InstancePerRequest();

            //web helper
            //builder.RegisterType<WebHelper>().As<IWebHelper>().InstancePerRequest();

            var assemblies = typeFinder.GetAssemblies().ToArray();

            //controllers
            builder.RegisterControllers(assemblies);
            //ApiControllers
            builder.RegisterApiControllers(assemblies);

            //View
            //builder.RegisterSource(new ViewRegistrationSource());

            //data layer
            var dataSettingsManager = new DataSettingsManager();
            var dataProviderSettings = dataSettingsManager.LoadSettings();
            builder.Register(c => dataSettingsManager.LoadSettings()).As<DataSettings>();
            builder.Register(x => new EfDataProviderManager(x.Resolve<DataSettings>())).As<BaseDataProviderManager>().InstancePerDependency();


            builder.Register(x => x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IDataProvider>().InstancePerDependency();

            if (dataProviderSettings != null && dataProviderSettings.IsValid())
            {
                var efDataProviderManager = new EfDataProviderManager(dataSettingsManager.LoadSettings());
                var dataProvider = efDataProviderManager.LoadDataProvider();
                //dataProvider.InitConnectionFactory();
                ((SqlServerDataProvider)dataProvider).InitDatabase();

                builder.Register<IDbContext>(c => new EfDbContext(dataProviderSettings.DataConnectionString)).InstancePerRequest();
            }
            else
            {
                builder.Register<IDbContext>(c => new EfDbContext(dataSettingsManager.LoadSettings().DataConnectionString)).InstancePerRequest();
            }


            builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerRequest();

            builder.RegisterAssemblyTypes(assemblies)
                .Where(t => typeof (IDependency).IsAssignableFrom(t) && t != typeof (IDependency))
                .AsImplementedInterfaces()
                .InstancePerRequest();
            builder.RegisterAssemblyTypes(assemblies)
                .Where(t => typeof (ISingletonDependency).IsAssignableFrom(t) && t != typeof (ISingletonDependency))
                .AsImplementedInterfaces()
                .SingleInstance();

            //IWorkContext
            builder.RegisterType<WorkContext>().As<IWorkContext>().InstancePerRequest();

            //filter
            builder.RegisterFilterProvider(); //todo:使用YpDependencyResolver时此方法会报空,用官方自带的不会
            builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);
            
            //注册YpHnadleError
            //全局注册
            builder.RegisterType<YpHandleErrorAttribute>().AsExceptionFilterFor<Controller>().SingleInstance();
            builder.RegisterType<YpAPIHandleErrorAttribute>().AsWebApiExceptionFilterFor<ApiController>().SingleInstance();
            //单个注册
            //builder.RegisterType<YpHandleErrorAttribute>().SingleInstance();
            //builder.RegisterType<YpAPIHandleErrorAttribute>().SingleInstance();

            //注册YpAuthorizeAttribute
            //全局注册
            builder.RegisterType<YpAPIAuthorizeAttribute>()
                .AsWebApiAuthorizationFilterFor<ApiController>()
                .PropertiesAutowired()
                .InstancePerRequest();
            builder.RegisterType<YpAuthorizeAttribute>()
                .AsAuthorizationFilterFor<Controller>()
                .PropertiesAutowired()
                .InstancePerRequest();
            //单个注册
            //builder.RegisterType<YpAPIAuthorizeAttribute>().PropertiesAutowired().InstancePerRequest();
            //builder.RegisterType<YpAuthorizeAttribute>().PropertiesAutowired().InstancePerRequest();
        }
 /// <summary>
 /// 配置数据库初始化操作
 /// </summary>
 public virtual void SetDatabaseInitializer()
 {
     var dataSettingsManager = new DataSettingsManager();
     var dataProviderSettings = dataSettingsManager.LoadSettings();
     //依据model更新数据库
     Database.SetInitializer(new EfInitializer<EfDbContext, EfConfiguration>(dataProviderSettings.DataConnectionString));
 }