Пример #1
0
        public static void Builder()
        {
            var builder = new ContainerBuilder();
            var cfg     = CreateCfg();

            builder.RegisterInstance(cfg).SingleInstance();

            #region 注册freesql

            var build = new FreeSql.FreeSqlBuilder();
            if (cfg.DbType == ConstCfg.DbType_MSsql)
            {
                build = build.UseConnectionString(FreeSql.DataType.SqlServer, cfg.AccurateConn);
            }
            else if (cfg.DbType == ConstCfg.DbType_Sqlite)
            {
                build = build.UseConnectionString(FreeSql.DataType.Sqlite, cfg.AccurateConn);
            }
            else if (cfg.DbType == ConstCfg.DbType_Mysql)
            {
                build = build.UseConnectionString(FreeSql.DataType.MySql, cfg.AccurateConn);
            }
            IFreeSql freeSql = build.UseAutoSyncStructure(false)
                               .Build();
            builder.RegisterInstance(freeSql).SingleInstance();
            BAccurateContext.Fluent(freeSql);
            builder.RegisterType <BAccurateContext>().InstancePerLifetimeScope();

            #endregion

            #region 注册服务
            var allAssemblies = BuildManager.GetReferencedAssemblies().Cast <Assembly>();
            var assembiles    = allAssemblies.Where(x => x.ManifestModule.Name.Contains("BAccurate")).ToArray();
            builder.RegisterAssemblyTypes(assembiles)
            .Where(type => typeof(ITransient).IsAssignableFrom(type) && !type.IsAbstract)
            .AsSelf()
            .AsImplementedInterfaces()
            .InstancePerDependency();
            #endregion


            //注册controller
            builder.RegisterApiControllers(assembiles);

            // 注册AutoMapper
            builder.Register(c => DtoMapper.CreateConfig()).SingleInstance();
            builder.RegisterType <Mapper>().As <IMapper>().InstancePerLifetimeScope();
            builder.RegisterType <DtoMapper>().As <IFMapper>().InstancePerLifetimeScope();

            // 注册核心业务
            builder.RegisterType <OnlineUserMgr>().As <IOnlineUserMgr>().SingleInstance();



            IOCContainer = builder.Build();

            // webApi部分注册
            HttpConfiguration config = GlobalConfiguration.Configuration;
            config.DependencyResolver = new AutofacWebApiDependencyResolver(IOCContainer);
        }
        private BAccurateContext BuildFreesql()
        {
            string   curDir  = AppDomain.CurrentDomain.BaseDirectory + "/test.db";
            string   conn    = "Data Source=" + curDir + "; Pooling=true;Min Pool Size=3;Journal Mode=WAL";
            IFreeSql freeSql = new FreeSql.FreeSqlBuilder()
                               .UseConnectionString(FreeSql.DataType.Sqlite, conn)
                               .UseAutoSyncStructure(false)
                               .Build();

            BAccurateContext.Fluent(freeSql);
            freeSql.CodeFirst.SyncStructure <BAccurate.Repository.Freesql.Entities.UserRoleRelationEntity>();
            freeSql.CodeFirst.SyncStructure <BAccurate.Repository.Freesql.Entities.UserEntity>();
            freeSql.CodeFirst.SyncStructure <BAccurate.Repository.Freesql.Entities.RoleEntity>();
            freeSql.CodeFirst.SyncStructure <BAccurate.Repository.Freesql.Entities.RoleResourceRelationEntity>();
            freeSql.CodeFirst.SyncStructure <BAccurate.Repository.Freesql.Entities.RoleEntity>();
            freeSql.CodeFirst.SyncStructure <BAccurate.Repository.Freesql.Entities.ResourceEntity>();
            return(new BAccurateContext(freeSql, this.BuildMapper()));
        }