1

Once again I come to you, to ask some Best practices questions.

I am starting a new project, and I want to be able to test it properly, and therefor I turn to IoC and Dependency Injection. I already know a fair amount about the concept, but there are some small details I want to ask you about.

I will be using a 3 tier application architecture
ASP.NET -> BLL -> DAL

First Question My first question is how do I best go about resolving dependencies. Having them injected into the constructor, seems to me that in some cases, I will have biiig constructors with alot of dependencies, even though in the actual code path I will need only a few of them. Also my concern is instantiating all dependencies that I might need, but properly wont, or maybe need to be able to instantiate something lower(I know you should propably get it from the dependency resolver)? So question: How do you go about injecting multiple dependencies without wasting resources, or making the init slow

Second question The Dependencies will be given to alot of Controllers in my MVC application, and here it is smart to use the dependencies indirectly, same propably goes for the BLL. But how about deep in the DAL? Say I need to use the CustomerDataProvider and the OrderDataProvider in addition to the some other dataproviders. Do I instantiate it directly, or use dependency injection here again?

Third question Last question, how do I manage to incorporate sharding into all of this? All my clients will be run on the same WebServer, but they each have their own DB (sharding), and I will also have a Master DB, how do you accommodate for this in NInject?

André Snede
  • 9,899
  • 7
  • 43
  • 67
  • 1
    Related: http://stackoverflow.com/questions/6025482/strategy-to-refactor-when-too-many-dependencies-injected-into-service-or-control – Steven Nov 26 '13 at 19:53
  • 1
    Related: http://stackoverflow.com/questions/953407/constructor-injection-how-many-dependencies-is-too-many – Steven Nov 26 '13 at 19:54
  • @Steven thanks for the links. I would like some more in-depth answers on the specific questions I have. But your links, is certainly worth a read. – André Snede Nov 26 '13 at 20:17
  • 1
    As for the third question, it doesn't sound related to IoC but rather to the actual data access technology. – Wiktor Zychla Nov 26 '13 at 23:13
  • @WiktorZychla Not directly no, but out of the box, basic "binding and resolving" in NInject, doesn't let you change the object conditionally, (eg. changing the connection string). So I was wondering what the best way to do it is. Some has suggested to Bind a Factory to the IoC Container, and then in the factory create and manage the different connections. – André Snede Nov 27 '13 at 07:32

1 Answers1

2

I would get a copy of Mark Seemann's book on DI. He has a section regarding what, and what not, to inject. One of his recommendations is to only inject 'volatile' depdendencies. This will shorten the parameter list of your constructors. His blog may cover this subject, but I'm not sure.

David Osborne
  • 6,436
  • 1
  • 21
  • 35