如何在组成根部之外访问DI框架统一容器
我已经为SNAPP集成了Unity容器,其中有两个名为Commerce and Order的文件夹。我已经为Commerce文件夹中的类创建了构造函数依赖注入,例如它具有控制器,经理和存储类(经理& storage -singleton类)。商业文件夹中的所有类的实例已经在Unity容器内解决。
在订单的文件夹类中,它不遵循依赖注入模式,因此尚未在Unity容器内注册。
这里有几个问题 -
我们如何访问ordermanager中的commercemanager类实例?我已经阅读了有关维修器以获取统一容器的信息,但它被称为反图案。请在此处解释正确访问实例的正确方法。
event event commercemanager班级是单身班,但我们从该课程中揭露了公共构造师,以通过Unity容器来解决实例。我们如何确保其他使用该参考的项目维护此类的单身属性?
I have integrated Unity container for my project snapp which has two folders named Commerce and Order. I have created constructor dependency injection for classes inside Commerce folder, say it has controller, manager and storage class (manager & storage - singleton class). The instance of all the classes inside commerce folder has been resolved inside unity container.
In order folder classes inside it doesnt follow dependency injection pattern and hence they have not been registered inside unity container.
Couple of questions here -
how do we access instance of CommerceManager class in OrderManager? I have read about servicelocator to get the unity container but it is mentioned as anti-pattern. please explain the right way of accessing instance here.
Eventhough CommerceManager class is a singleton class but we expose public constructor from this class for resolving instances by unity container. How do we ensure that other projects using this as a reference maintains the singleton property of this class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看到三个选项:
commerceManager
作为OrderManager
或类似长后注入的构造函数中的输入(例如,propterty注入)。这具有简化解决方案并使您的课程更易于单元测试的好处。commerceManager
被实现为单例,您可以直接访问该实例,或者不需要是SingletonOrderManager
可以实例化一个。好处是它很容易。但是,即使不是不可能,单元测试将变得非常棘手,因为您无法模拟commerceManager
,因为ordermanager
现在对其具有硬编码的依赖性。唯一可能的方法是将
CommerceManager
构建为真正的Singleton类(没有公共构造函数)。现在,听起来您让DI容器控制着课堂的生命周期。这意味着,只有当您通过DI容器访问CommerceManager
的实例,才会使用Singleton实例。如果其他项目是 通过DI容器访问它,则需要通过类本身(或某些“访问”类作为中级类)来控制它:
I see three options:
CommerceManager
as input in the constructor ofOrderManager
or similar depency injection (e.g. method, propterty injection). This has the benefit of streamlining your solution and making your classes easier to unit test.CommerceManager
is implemented as a singleton you could access the instance directly or if it doesn't need to be a singletonOrderManager
can instantiate one itself. The benefit is that it's easy. However, unit testing becomes quite tricky if not impossible, because you cannot mockCommerceManager
asOrderManager
now has a hard-coded dependency to it.The only way that's possible is to build
CommerceManager
as a real singleton class (without public constructor). Right now, it sounds like you let your DI container control the lifecycle of the class. This means, only if you access an instance ofCommerceManager
through your DI container will you use the singleton instance.If other projects are not accessing it through the DI container, you need to control it through the class itself (or some "accessor" class as an intermediate class):