日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

ASP.net?core使用Autofac實現泛型依賴注入_實用技巧

作者:騙你學計算機 ? 更新時間: 2022-06-27 編程語言

什么是泛型依賴注入

創建兩個帶泛型的類,并配置兩者的依賴關系,對于繼承這兩個類的子類,如果泛型相同,則會繼承這種依賴關系:

如上圖:

定義了兩個泛型base類:BaseService和BaseRepository

對于UserService和UserRpository分別繼承兩個base類,泛型都是User,則他們倆繼承了父類的依賴關系。

.net core里實現泛型依賴注入

安裝Autofac

先看項目結構

IMyRepository定義倉儲接口

    public interface IMyRepository<T> where T: class
    {
        string GetTypeof();
    }

MyRepositoryBase倉儲實現

    public class MyRepositoryBase<T> : IMyRepository<T> where T : class
    {
        public string GetTypeof()
        {
            return typeof(T).Name; //通過typeof可以知道泛型的名字
        }
    }

CustomAutofacModule 公共的依賴注入類

    public class CustomAutofacModule : Module
    {
        public CustomAutofacModule(ContainerBuilder builder) {
        }
        /// <summary>
        /// AutoFac注冊類
        /// </summary>
        /// <param name="builder"></param>
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterGeneric(typeof(MyRepositoryBase<>)).As(typeof(IMyRepository<>)).InstancePerDependency();//注冊倉儲泛型
//builder.RegisterGeneric(typeof(MyRepositoryBase<,>)).As(typeof(IMyRepository<,>)).InstancePerDependency();//注冊倉儲泛型 2個以上的泛型參數
         //  builder.RegisterType<myAssembly>().As<ImyAssembly>();   //普通依賴注入
        }
    }

在Program聲明實現依賴注入

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                //改用Autofac來實現依賴注入
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

修改Startup

運行時候觸發CustomAutofacModule

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }
        //autofac 新增
        public ILifetimeScope AutofacContainer { get; private set; }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }
        public void ConfigureContainer(ContainerBuilder builder)
        {
            // 直接用Autofac注冊我們自定義的 
            builder.RegisterModule(new CustomAutofacModule(builder));
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            //autofac 新增 
            this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

在Home控制器中使用

    [ApiController]
    [Route("[controller]")]
    public class HomeController : ControllerBase
    {
        //public IMyRepository<User> _UserServer { get; set; }
        private readonly IMyRepository<User> _UserServer;
        private readonly IMyRepository<Role> _RoleServer;
        public HomeController(IMyRepository<User> UserServer, IMyRepository<Role> RoleServer)
        {
            _UserServer = UserServer;
            _RoleServer = RoleServer;
        }
        [Route("Get")]
        public string Get() {
            return _UserServer.GetTypeof();//"user"; //
        }
        [Route("GetRole")]
        public string GetRole()
        {
            return _RoleServer.GetTypeof();//"role"; //
        }
    }

可以看到 不同的地方實現不同的對象

番外:

我是因為看到ABP框架的IRepository的實現才研究泛型依賴注入的用法的。

ABP框架吧Autofac已經 封裝為IocManager 了

所以ABP框架不需要 引入Autofac框架。只需要在對應的XXXCoreModule 中的Initialize()方法聲明依賴注入便可

IocManager.Register(typeof(IMyRepository<>), typeof(MyRepositoryBase<>), DependencyLifeStyle.Transient);

如果是2個以上的泛型寫法是

 IocManager.Register(typeof(IAmbientScopeProvider<,>), typeof(DataContextAmbientScopeProvider<,>), DependencyLifeStyle.Transient);

DependencyLifeStyle.Transient 的作用

Transient :瞬態,要么作用域是整個進程,要么作用域是一個請求,而這里的 Transient 就沒有作用域概念了,注入一次 實例化一次 最明顯的區別,屬性注入是不可用的,只能構造函數注入

Singleton:可以在你的進程中保持著一個實例,也就是說僅有一次實例化 最明顯的區別,屬性注入是可用的

番外2:

看到了很多教程是不用聲明CustomAutofacModule類的直接在Startup聲明依賴注入就可以的。但是那是core 2.0的寫法。core 3.0 下面的寫法是會報錯的

public static IContainer AutofacContainer;
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
    //注冊服務進 IServiceCollection
    services.AddMvc();
    ContainerBuilder builder = new ContainerBuilder();
    //將services中的服務填充到Autofac中.
    builder.Populate(services);
    //新模塊組件注冊
    builder.RegisterModule<DefaultModuleRegister>();
    //創建容器.
    AutofacContainer = builder.Build();
    //使用容器創建 AutofacServiceProvider 
    return new AutofacServiceProvider(AutofacContainer);
}

原文鏈接:https://blog.csdn.net/wangwengrui40/article/details/115024997

欄目分類
最近更新