views:

136

answers:

5

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???

+4  A: 

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.

Darin Dimitrov
I am using StructureMap and pretty much made a factory class that takes in the company and created the correct class from what is configured for the interface. The thing I dont like is I have to assume the class has the proper constructor....
CSharpAtl
Well, in my response I was not referring to any DI container. IMHO from OOP point of view you have to supply the company name in the constructor because all your methods require it.
Darin Dimitrov
Seconded .
annakata
+2  A: 

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).

annakata
I am using StructureMap, but I dont know the value of "company" until the user is logged in.
CSharpAtl
A: 

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;
}
JasonRShaver
Cannot have it preconfigured, because I will not know until login.
CSharpAtl
You can always 'add' it to the container by hand after the user logs in. I am editing my answer above to show an example...
JasonRShaver
A: 

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.

flq
A: 

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.

flukus