예제 #1
0
        /// <summary>
        /// 初始化Ioc容器
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        private IServiceProvider InitIoc(IServiceCollection services)
        {
            var connectionString = Configuration.GetConnectionString("MsSqlServer");
            //var connectionString = Configuration.GetConnectionString("MySql");
            var dbContextOption = new DbContextOption
            {
                ConnectionString = connectionString,
                DbType           = DbTypeEnum.MSSQLSERVER,
                //DbType = DbTypeEnum.MYSQL,
                ModelAssemblyName = "Ses.AspNetCore.Entities",
            };

            //var codeGenerateOption = new CodeGenerateOption
            //{
            //    ModelsNamespace = "Zxw.Framework.Website.Models",
            //    IRepositoriesNamespace = "Zxw.Framework.Website.IRepositories",
            //    RepositoriesNamespace = "Zxw.Framework.Website.Repositories",
            //    IServicsNamespace = "Zxw.Framework.Website.IServices",
            //    ServicesNamespace = "Zxw.Framework.Website.Services"
            //};
            AutofacContainer.Register(Configuration);            //注册配置
            AutofacContainer.Register(dbContextOption);          //注册数据库配置信息
            //AutofacContainer.Register(codeGenerateOption);//注册代码生成器相关配置信息
            AutofacContainer.Register(typeof(DefaultDbContext)); //注册EF上下文
            AutofacContainer.RegisterGeneric(typeof(IRepository <,>), typeof(BaseRepository <,>));
            AutofacContainer.RegisterGeneric(typeof(IBaseService <,>), typeof(BaseService <,>));
            AutofacContainer.Register("Ses.AspNetCore.Services", "Ses.AspNetCore.IServices");//注册service

            return(AutofacContainer.Build(services));
        }