问题描述
这是我想要的DI容器:
public class Class
{
public Class (ID依赖关系,字符串数据){}
}
var obj = di.Resolve< Class>(()=> new Class(null,test));
兴趣点:
- 可以解析构造函数中的依赖关系和数据。
- 可以使用类型安全的语法来传递构造函数参数(确切的语法可能会有所不同)。是的,我可以通过从(Expression.Body as NewExpression)获取构造函数参数,但我需要一种方法来检测在容器中注册的参数。
$ b另外一个主要的要求就是我希望我的组件可以被自动提取,也就是说我不想注册Class - 我想让IoC来接收它,因为它知道如何解决IDependency
此外,属性注入有时可以使用,但这是可选的。
问题是真的功能的组合 - 具有所有功能 - 类型安全,参数,自动拾取...检查一个功能很容易,但是它们的组合不容易验证,除非熟悉特定的容器,并且知道它特征。因此这个问题。
我认为你会更好的定义一个可以创建你的班级的抽象工厂。 >
public interface IFactory
{
MyClass Create(string data);
}
然后,您可以像这样创建一个IFactory的实现:
public class MyFactory:IFactory
{
private IDependency dependency;
public MyFactory(IDependency dependency)
{
if(dependency == null)
{
throw new ArgumentNullException(dependency);
}
this.dependency =依赖;
}
#region IFactory成员
public MyClass Create(string data)
{
返回新的MyClass(this.dependency,data );
}
#endregion
}
您的容器,您将注册MyFactory和IDependency的实现。
您现在可以使用容器来解析工厂,工厂获取类: p>
var mc = container.Resolve< IFactory>()。Create(data);
这种方法是完全类型安全的,很好地将依赖关系与运行时应用程序数据分离开。 p>
This is what I want from DI container:
public class Class
{
public Class(IDependency dependency, string data) { }
}
var obj = di.Resolve<Class>(() => new Class(null, "test"));
Points of interest:
- Can resolve both dependency and data in constructor.
- Can use type-safe syntax to pass constructor parameters (exact syntax may vary). Yes I can do it myself by getting constructor arguments from (Expression.Body as NewExpression) - but I'll need a way to detect what arguments are registered in the container.
Another major requirements is that I'd like my components to be automatically picked up, i.e. I don't want to register Class - I want IoC to pick it up because it knows how to resolve IDependency.
Also, Property Injection can be useful sometimes, but this is optional.
The question is really about the combination of features - to have all of them - type-safe, parameters, automatic pick-up... It's easy to check one feature, but a combination of them is not easy to verify unless one's familiar with particular container and knows its features. Thus the question.
I think you would be better off by defining an Abstract Factory that can create your class.
public interface IFactory
{
MyClass Create(string data);
}
You could then create an implementation of IFactory like this:
public class MyFactory : IFactory
{
private IDependency dependency;
public MyFactory(IDependency dependency)
{
if (dependency == null)
{
throw new ArgumentNullException("dependency");
}
this.dependency = dependency;
}
#region IFactory Members
public MyClass Create(string data)
{
return new MyClass(this.dependency, data);
}
#endregion
}
In your container, you would register both MyFactory and the implementation of IDependency.
You can now use the container to resolve the Factory, and the Factory to get the class:
var mc = container.Resolve<IFactory>().Create(data);
This approach is completely type-safe and nicely decouples the dependencies from run-time application data.
这篇关于哪个DI容器会满足这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!