0
比如我有依賴性:如何更好地解決工廠創建的對象中的依賴關係?
public interface IMyDependency
{
}
public class MyDependency : IMyDependency
{
}
內噴射在MyClass的對象:
public interface IMyInterface
{
}
public class MyClass : IMyInterface
{
[Dependency]
public IMyDependency MyDependency { get; set; }
}
另外我有一個工廠,創建MyClass的實例:
public interface IFactory
{
IMyInterface CreateMyObject();
}
public class Factory : IFactory
{
public IMyInterface CreateMyObject()
{
// some checks before creation
// for. ex check if this type of object supported by OS and thow exception if not
return new MyClass();
}
}
...
container.RegisterType<IFactory, Factory>();
如果我創建MyClass的帶有「新」關鍵字的實例將無法解析。或者我應該逃離Factory並將邏輯移入MyClass的構造函數中?並將其註冊到容器中後,如container.RegisterType()?