猴哥猴哥 发表于 2023-8-3 02:09:01

.Net Core Autofac


目录

[*]Autofac
[*]Program.cs
[*]AutofacConfig.cs
[*]测试接口+实现

[*]IStudent.cs
[*]Student.cs


Autofac

Autofac 是一款超赞的.NET IoC 容器 . 它管理类之间的依赖关系, 从而使 应用在规模及复杂性增长的情况下依然可以轻易地修改 . 它的实现方式是将常规的.net类当做 组件 处理.
控制反转背后的核心思想是, 我们不再将应用中的类捆绑在一起, 让类自己去 “new up” 他们的依赖, 而是反过来在类的构造方法中将依赖传递进去.
中文文档
引用
<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>Program.cs

//添加 autofac
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
{
    builder.RegisterModule<AutofacConfig>();
});AutofacConfig.cs

using Autofac;using Autofac.Extras.DynamicProxy;using Cnpc.Com.Ioc.Bll;using Cnpc.Com.Ioc.Dal;using Cnpc.Com.Ioc.IBll;using Cnpc.Com.Ioc.IDal;using Cnpc.Com.Ioc.Tools;namespace Cnpc.Com.AutofacTest.Autofac{    public class AutofacConfig : Module    {<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>protected override void Load(ContainerBuilder builder)<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>{<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    //ioc<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    builder.RegisterType().As(); //构造函数注入<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    builder.RegisterType().As().PropertiesAutowired(new PropertySelection()).OnActivated(it =><ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    {<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup><ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>it.Instance.SetIWriten(it.Context.Resolve());<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    }).EnableInterfaceInterceptors(); //属性注入 + 指定属性注入 + 指定方法注入 + 开启Aop<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>   //aop<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    builder.RegisterType();<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    builder.RegisterType();<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>}    }}Aop AutofacInterceptor.cs
using Castle.DynamicProxy;using Microsoft.Extensions.Logging;using NLog;namespace Cnpc.Com.Ioc.Tools{    public class AutofacInterceptor : IInterceptor    {<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>//支持NOP<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    ILogger logger;<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public AutofacInterceptor(ILogger logger)<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>{<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    this.logger = logger;<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>}<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public void Intercept(IInvocation invocation)<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>{<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup><ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup><ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>{<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup><ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>logger.LogInformation("执行前");<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup><ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>Console.WriteLine("执行前: " + DateTime.Now.ToString() + "此消息来自:" + this.GetType().FullName) ;<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    invocation.Proceed();<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    {<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup><ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>Console.WriteLine("执行后: " + DateTime.Now.ToString() + "此消息来自:" + this.GetType().FullName);<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>}    }}测试接口+实现

IStudent.cs

using Autofac.Extras.DynamicProxy;using Cnpc.Com.Ioc.IDal;using Cnpc.Com.Ioc.Tools;using Microsoft.Extensions.Logging;namespace Cnpc.Com.Ioc.IBll{        public interface IStudent    {<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>IWrite Write { get; set; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>IWrite Write1 { get; set; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>IWrite Write2 { get; set; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>IWrite Write3 { get; set; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>IWrite Write4 { get; set; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>IWrite Write5 { get; set; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>ILogger logger { get; set; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>IWrite Write6 { get; set; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>IWrite WriteContainer { get; set; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>void SetIWriten(IWrite write);<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>void DoHomeWork(IWrite writen);<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>void Calligraphy(IWrite writen);    }}Student.cs

using Autofac.Extras.DynamicProxy;using Cnpc.Com.Ioc.IBll;using Cnpc.Com.Ioc.IDal;using Cnpc.Com.Ioc.Tools;using Microsoft.Extensions.Logging;using NLog;namespace Cnpc.Com.Ioc.Bll{        public class Student : IStudent    {<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup><ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public IWrite Write { get; set ; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public IWrite WriteContainer { get ; set ; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public IWrite Write1 { get ; set ; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public IWrite Write2 { get ; set ; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public IWrite Write3 { get ; set ; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public IWrite Write4 { get ; set ; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public IWrite Write5 { get ; set ; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public IWrite Write6 { get ; set ; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup><ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup> //支持Nlog 属性注入<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup><ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public ILogger logger { get ; set ; }<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public void Calligraphy(IWrite writen)<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>{<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    writen.Write();<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>}<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public void DoHomeWork(IWrite writen)<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>{<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    logger.LogInformation("wow");<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    writen.Write();<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>}<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>public void SetIWriten(IWrite write)<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>{<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>    this.WriteContainer = write;<ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Castle.Core" Version="5.1.1" />
</ItemGroup>}    }}
来源:https://www.cnblogs.com/qfccc/archive/2023/08/03/17602224.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: .Net Core Autofac