使用静态类和属性进行依赖注入

发布于 2024-12-04 12:26:33 字数 899 浏览 0 评论 0原文

我设计了一个多层解决方案并创建了一堆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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2024-12-11 12:26:33

您是否需要您的管理器类是静态的,或者只是您希望它们是单例?

您是否正在寻找一种方法来拥有具有瞬态依赖性的单例类?

在这种情况下,我会回答单例管理器类应该依赖于工厂,该工厂可以根据需要创建存储库的实例。

温莎提供了一个类型化的工厂设施,可以帮助创建这样的工厂:
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

菩提树下叶撕阳。 2024-12-11 12:26:33

这里有几个选项:

1)

 public class BaseManager
    {  
        protected IRepository repository;
        public BaseManager(IRepository _repository)
        {
            repository = _repository 
        }
        // Some shared methods among manager classes.
    }  

实例化您的BaseManager

IoCLocator.Resolve<BaseManager>();

这需要您使用或类似的东西

,具体取决于具体的IoCLocator(MEF,Unity,Ninject或您正在使用的任何东西) 2)
几乎相同:

 public class BaseManager
     {  
        protected IRepository repository;
        public BaseManager(IoCLocator locator)
        {
            repository = locator.Resolve<IRepository>();
        }
        // Some shared methods among manager classes.
     } 

3)或者,直接注入属性

 public class BaseManager
 {  
    [InjectFromContainer]
    protected IRepository repository {get;set;}

    public BaseManager()
    {
        repository = locator.Resolve<IRepository>();
    }
    // Some shared methods among manager classes.
 }  

我无法准确命名您需要的属性,因为它取决于您的容器。
无论如何,您必须在 BootStrapper 中实例化 IoC 容器,向其注册 BaseManager,然后从容器中解析其实例。

希望这会有所帮助,伊利亚

更新:

你的概念只是一团糟。您想要继承的静态类,IoC 容器与静态的主应用程序类一起工作。我建议您选择其中之一,问题就会自行解决。

You have several options here:

1)

 public class BaseManager
    {  
        protected IRepository repository;
        public BaseManager(IRepository _repository)
        {
            repository = _repository 
        }
        // Some shared methods among manager classes.
    }  

This requires you to instantiate your BaseManager using

IoCLocator.Resolve<BaseManager>();

or something like that, depending on concrete IoCLocator (MEF, Unity, Ninject or whatever you are using)

2)
Pretty much the same:

 public class BaseManager
     {  
        protected IRepository repository;
        public BaseManager(IoCLocator locator)
        {
            repository = locator.Resolve<IRepository>();
        }
        // Some shared methods among manager classes.
     } 

3) Or, inject the property directly

 public class BaseManager
 {  
    [InjectFromContainer]
    protected IRepository repository {get;set;}

    public BaseManager()
    {
        repository = locator.Resolve<IRepository>();
    }
    // Some shared methods among manager classes.
 }  

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文