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. 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. 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.
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. Then your free from having to pass it to the constructor or the method.