接口和实例强耦合
Interface = new 实例名称,ioc实现接口和是咧
mef ioc
直接引用dotnet的Managed Extensibility Framework
代码语言:javascript复制参考:https://www.cnblogs.com/yunfeifei/p/3922668.html
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