使用静态类和属性进行依赖注入
我设计了一个多层解决方案并创建了一堆Manager类来实现业务逻辑。所有管理器均派生自BaseManager
类。更清楚地说,这是 UserManager
类:
public class UserManager : BaseManager
{
public void Add(User user)
{
...
}
public void Update(User user)
{
...
}
public void Delete(User user)
{
...
}
public bool GetAccessPermissions(User user)
{
...
}
}
BaseManager
如下:
public class BaseManager
{
protected IRepository repository = IoCLocator.Resolve<IRepository>();
public BaseManager()
{
...
}
// Some shared methods among manager classes.
}
现在我怀疑我的管理器类是否应该全部定义为静态,因为它们获取其相关实体想要操作,作为参数。但正如您所看到的,我有一些共享的私有/受保护的成员,我应该在每次调用时实例化它们。 (例如,repository
应在创建每个管理器类时实例化)。
我可以选择什么将管理器类定义为静态,并且仍然在每次调用管理器方法时实例化受保护的成员?
I have designed a multi-layer solution and created a bunch of Manager classes to implement Business Logic. All the managers are derived from BaseManager
class. To be more clear, here's UserManager
class:
public class UserManager : BaseManager
{
public void Add(User user)
{
...
}
public void Update(User user)
{
...
}
public void Delete(User user)
{
...
}
public bool GetAccessPermissions(User user)
{
...
}
}
BaseManager
is as follows:
public class BaseManager
{
protected IRepository repository = IoCLocator.Resolve<IRepository>();
public BaseManager()
{
...
}
// Some shared methods among manager classes.
}
Now I skeptical that my manager classes should all be defined static, since they get their related entities on which they want to operate, as parameters. But As you see, I have some shared private/protected members which I should instantiate upon every call. (e.g. repository
should be instantiated on each manager class's creation).
What are my choices to define managers classes as static and still have the protected members instantiated on each call to managers' methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否需要您的管理器类是静态的,或者只是您希望它们是单例?
您是否正在寻找一种方法来拥有具有瞬态依赖性的单例类?
在这种情况下,我会回答单例管理器类应该依赖于工厂,该工厂可以根据需要创建存储库的实例。
温莎提供了一个类型化的工厂设施,可以帮助创建这样的工厂:
http://docs.castleproject.org/Windsor.Typed -Factory-Facility-interface-based-factories.ashx
Do you need your manager classes to be static, or is it just that you wish them to be singletons?
Are you seeking a way to have a singleton class with transient dependencies?
In which case I would answer that the singleton manager class should take a dependency on a factory which can create instances of the repositories as required.
Windsor offers a typed factory facility which helps with creating such factories:
http://docs.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx
这里有几个选项:
1)
实例化您的BaseManager
这需要您使用或类似的东西
,具体取决于具体的
IoCLocator
(MEF,Unity,Ninject或您正在使用的任何东西) 2)几乎相同:
3)或者,直接注入属性
我无法准确命名您需要的属性,因为它取决于您的容器。
无论如何,您必须在 BootStrapper 中实例化 IoC 容器,向其注册 BaseManager,然后从容器中解析其实例。
希望这会有所帮助,伊利亚
更新:
你的概念只是一团糟。您想要继承的静态类,IoC 容器与静态的主应用程序类一起工作。我建议您选择其中之一,问题就会自行解决。
You have several options here:
1)
This requires you to instantiate your BaseManager using
or something like that, depending on concrete
IoCLocator
(MEF, Unity, Ninject or whatever you are using)2)
Pretty much the same:
3) Or, inject the property directly
I cannot exactly name the Attribute you need, because it depends on your container.
Anyway, you'll have to instantiate IoC Container in BootStrapper, register
BaseManager
with it and then resolve its instance from the container.Hope this helps, Ilya
UPDATE:
Your concept is just a mess. Static classes which you want to inherit, IoC Container working with main application classes being static. I'd recommend you to choose one of each, and the problem will be gone by itself.