简体   繁体   English

IOC和动态参数

[英]IOC and dynamic parameters

I have read some posts on here about not mixing parameters when passing into a constructor, but have a question. 我读过一些关于在传递到构造函数时不混合参数的文章,但是有一个问题。 I have classes and they all depend on a company parameter, all of the methods are dependent on the company. 我有类,它们都取决于公司参数,所有方法都取决于公司。 I do not want to have to pass the company in on every method call, I would like to pass that into the constructor. 我不想在每次方法调用时都将公司传递给我,我想将其传递给构造函数。 This does make it difficult on the classes because I have to pretty much make sure the constructors of the concrete classes take in certain parameteres, which I am not a fan of (cannot put that in the contract that is the interface). 这确实使类变得很困难,因为我必须确保具体类的构造函数接受某些参数,而我并不喜欢这些参数(不能将其放在作为接口的协定中)。 Recommend just passing the company into every method call in the class??? 建议仅将公司传递到类中的每个方法调用中???

If all the methods require a company name then it is perfectly reasonable from a OOP design point of view to pass this company name into the constructor of your class. 如果所有方法都需要一个公司名称,那么从OOP设计的角度来看,将这个公司名称传递到您的类的构造函数中是完全合理的。 This means that your class cannot function correctly without a company name so any consumer of the class will be forced to supply required dependency. 这意味着如果没有公司名称,您的类将无法正常运行,因此该类的任何使用者都将被迫提供所需的依赖关系。

Assuming you aren't working with a known IoC framework which already handles this for you, you could always implement this as property injection. 假设您未使用已经为您处理的已知IoC框架,则始终可以将其实现为属性注入。 Not that I see a problem with doing this at the constructor level either - you haven't really said why you feel that's a problem. 并不是说我在构造函数级别上也遇到了问题-您还没有真正说出为什么会觉得有问题。

Certainly wouldn't recommend passing it to every method (clearly redundant). 当然,不建议将其传递给所有方法(显然是多余的)。

Aye, in the IoC world, most tools (Spring.Net, Castle Windsor, even LinFu) have methods that can take care of that for you so you define it one in a config and every time you get a copy (from the container or whatever) it comes preconfigured. 是的,在IoC世界中,大多数工具(Spring.Net,Castle Windsor甚至LinFu)都具有可以为您解决这些问题的方法,因此您可以在配置中定义它,并在每次获取副本时(从容器或随便什么)它都是预先配置的

You can wrap your 'container' 您可以包装“容器”

IWindsorContainer _ConfiguredContainer = null;

public IWindsorContainer GetContainer()
{
   if (LoggedIn == false)
      throw new InvalidOperationException("Cannot be called before a user logs in");

   if (_ConfiguredContainer == null)
   {
      _ConfiguredContainer = new WindsorContainer(new XmlInterpreter());

      // Do your 'extra' config here.
      _ConfiguredContainer.AddComponentWithProperties(/*blah blah blah*/);
   }

   return _ConfiguredContainer;
} 

If the behaviour of your class depends on said company name, what is wrong with passing it in with the constructor? 如果您的类的行为取决于所说的公司名称,那么将其传递给构造函数有什么问题? A constructor is a contract all by itself - you will have to instantiate it with something. 构造函数本身就是一个契约-您将不得不用某种东西实例化它。

You could have an ICommonSettings (almost like a global static class) that gets injected into your login manager (to set the comany name) and into the object your talking about. 您可能有一个ICommonSettings(几乎类似于全局静态类),该ICommonSettings被注入您的登录管理器(以设置comany名称)和您要讨论的对象中。 Then your free from having to pass it to the constructor or the method. 然后,您不必将其传递给构造函数或方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM