0

Possible Duplicate:
What should be injected as C'tor paramter under DI principles ?

I looking into using Castle Windsor for my next project and I trying to understand what objects should go into a dependency container. My initial thoughts were if an object accessed an exernal resource, email server, database, file system, etc, then that is a good candidate for going into the container. But, then I thought that any object that makes "use" of an external resource should also go into the container as well.

Would this be a valid approach to take when working with a dependency container?

Community
  • 1
  • 1
MotoSV
  • 2,348
  • 17
  • 27
  • 1
    Duplicate: http://stackoverflow.com/questions/3361958/what-should-be-injected-as-ctor-paramter-under-di-principles – Mark Seemann Mar 19 '11 at 09:12

2 Answers2

0

Basically any object which has dependencies (in the constructor or via properties) should go in there and also the dependencies themself. You should consider creating interfaces or abstract base classes for the classes you want put in the container so you can mock them out for testing.

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
0

a dependency injection container is an objects that holds implementations of certain types (mainly implementing a certain interface or base class), which are used to Resolve the dependencies of an object.

There are 3 phases for using a DIC: you have objects that have some "dependencies" (preferably on Interfaces) which they declare either through a constructor (ex: using the InjectionConstructorAttribute in Unity) or property (ex: using the DependencyAttribute in Unity); The next step is registering implementations for those dependencies, and this can be achieved via code or a configuration file, ex (unity):

 container.RegisterType<IMyInterface>(MyImplementation);

the third phase is actually resolving a certain type from the container, which resolves it's dependencies (and their dependencies if they have any, and so on), ex. (unity):

var obj = container.Resolve<IMyInterface>();

So, in answer to your question, what should go into the dependency injection container is any type that's necessary for resolving other types, or any type that has dependencies of its own (which have to be registered with the container at some point).

I hope this helps :)

AbdouMoumen
  • 3,814
  • 1
  • 19
  • 28