IOC控制反转

2020-02-14 17:45:03 浏览数 (1)

接口和实例强耦合

Interface = new 实例名称,ioc实现接口和是咧

mef ioc

直接引用dotnet的Managed Extensibility Framework

参考:https://www.cnblogs.com/yunfeifei/p/3922668.html

代码语言:javascript复制
public interface IDataBase
{
    void Add<T>(T data);
    IQueryable<T> Get<T>();
}
[Export(typeof(IDataBase))]     //导出
public class GWDataBase : DbContext, IDataBase
{
}
代码语言:javascript复制
AssemblyCatalog catalog = new AssemblyCatalog(Assembly.Load("DataBaseMap"));
CompositionContainer container = new CompositionContainer(catalog);
return container.GetExport<IDataBase>().Value;

unity ioc

nuget安装unityioc

代码语言:javascript复制
UnityContainer container = new UnityContainer();//创建容器
container.RegisterType<Test01.IWaterTool, Test01.PressWater>();//注册依赖对象
IWaterTool obj = container.Resolve<IWaterTool>();

或者使用

代码语言:javascript复制
[Dependency

0 人点赞