1

Our app basically is a few forms that are filled out by people.. Then these forms are validated and reviewed and reports are created..

So I was thinking about DI and messing with a simple example of Unity + MVC

http://netmvc.blogspot.com/2012/04/dependency-injection-in-aspnet-mvc-4.html

So could it be useful for something or overkill ? It seems to me the idea behind DI is to dynamically grab objects of a different type.. So in my case maybe those can be a set of rules for validation of a form... Something like SyntaxRules1.dll, BusinessRules1.dll, CalculationRules1.dll... So then as time goes by maybe additional sets of rules would be created so I can drop another DLL like BusinessRules2.dll in a folder and DI would pick it up in the boot strapper..

But then again if it changes very little then using DI might be pointless and just be a layer of abstraction that is not needed..

Do I have the right idea? Is there any other ideas how DI could be useful for this type of app ? Could I make the entire view/controller/model or each form its own DLL ? That make sense ?

I just read alot of theory and struggling to find some concrete examples of what DI can do.

2
  • Dependency injection is very simple and it doesn't require any additional tools. There is a good example here: github.com/ninject/ninject/wiki/Dependency-Injection-By-Hand I guess that you are referring to IoC (Inversion of Control) frameworks such as Ninject and StructureMap.
    – CodeART
    Commented Apr 25, 2012 at 19:37
  • 1
    Dino Esposito just wrote an article about DI on Simple Talk.
    – Oded
    Commented Apr 27, 2012 at 9:08

2 Answers 2

4

It seems to me the idea behind DI is to dynamically grab objects of a different type

No, it isn't. DI stands for Dependency Injection. The idea is that instead of classes instantiating the classes/services that they need in order to operate, these are passed in to them (i.e. injected) - this is normally done either on the constructor or through public properties.

Writing classes in such a way allows you do have a more modular structure to your code and means that the user of a class (the calling code) needs to pass in the dependencies.

This concept is that of Inversion of Control (IoC), which uses DI to achieve this goal.

Where an IoC container like Unity comes into play is that it allows you to determine in configuration (which can be strongly typed fluent code configuration) how to provide a class with its dependencies.


Now, I would argue that DI is a useful technique in any application. It allows you to write more testable code and allows you to test your code in isolation (meaning without having to draw in the dependencies). Mocking frameworks help in this respect.

An IoC container, on the other hand, is probably overkill for a small and simple application. It is handy in cases where you have chains of dependencies or many dependencies in the codebase.


The example you have given - picking up rules dynamically sounds like something that would be better achieved using a plugin architecture, possibly using MEF. It is not something directly related to DI or IoC.

8
  • I still get kinda of lost reading the definition.. As far as testing.. Using the typical 'Testing reading objects in a DB' example. Right now the way to do it seems to be you create an interface and one implementation uses live data and the other uses test data.. Can you explain how that relates to unity ..or maybe is another way to think about it is unity is the example I gave is one level of decoupling.. but using unity is a new level of decoupling
    – punkouter
    Commented Apr 25, 2012 at 19:12
  • I get MED and Unity confused .. But when I say container.RegisterType<IValuesService, ValuesService>(); Arent I saying 'Give me all things that implement IValuesService' ? So thats picking up instances of a class dynamically right ?
    – punkouter
    Commented Apr 25, 2012 at 19:14
  • @punkouter - Unity would help with managing the dependencies. A better example would be a codebase that can use Oracle, MS Sql, PostgresSQL and other databases using a unified interface. Unity can help with managing which database specific providers and classes are passed to the generic classes that use them (the generic classes would be created with DI in mind).
    – Oded
    Commented Apr 25, 2012 at 19:16
  • @punkouter - What is MED?
    – Oded
    Commented Apr 25, 2012 at 19:17
  • @punkouter - What you write container.RegisterType<IValuesService, ValuesService>();, I see it to mean - when you see IValuesService give me the implementation ValuesService.
    – Oded
    Commented Apr 25, 2012 at 19:18
1

I have done what you're talking about using Unity with a slight change in your perception of Dependency Injection (DI). All dependency injection means is that you pass in an instantiated class type into your class's constructor rather than having your class instantiate that type.

As one previous poster stated, this is good for testing purposes as you can write test code for your class and pass in different instantiated types to test your class functionality.

However, to do what you're asking in regards of passing in BLL, you would use DI but instead of passing in your BLL class, you would pass in a service/controller object that would return an appropriate BLL. That service object would internally know which BLL to return based on functionality that you created. That service object could be an Interface type with a function like getBLL(myCustomerNumber);

The dynamic part comes in as Inversion of Control (IOC). So, in terms of IOC, you would register your service (the class that knows which BLL to return) to that interface type. Therefore, every time that service is referenced in a class constructor, unity would use the service (class) that was registered to pass into the constructor.

Please take note: With unity, you can register/create a class/service with/without a life time manager. This is important to research because if you don't have a LifeTimeManager, the registered class will be created every time it is referenced. With a LifeTimeManager, it will be created once in the system and used throughout the system. You may want this in certain circumstances and others you may not.

3
  • Bye the way, to clarify, I'm using MVVM and I pass my BLL service getter into my ViewModel class because we have different business logic depending on the customer.
    – Brian
    Commented Apr 25, 2012 at 20:05
  • with MVC you recreate everything each postback right? So that would be without a lifeTimeManager? I think I need to make sure I understand the difference between DI and IoC.. right now its blurry... Know any good simple examples out there? espically if its mvc ?
    – punkouter
    Commented Apr 26, 2012 at 15:58
  • I don't have an example specifically for MVC but this is a good explanation: codeproject.com/Articles/29271/…
    – Brian
    Commented Apr 26, 2012 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.