Introduction
In the previous article we discussed about the fundamentals of IOC and DI design patterns. In case you have missed it you can read more about it, by clicking here
In the same article we also discussed about how Windsor can be used to solve this problem. In this article we will take up a simple example and try to implement DI using unity application blocks thus resulting in loosely coupled architecture.
Any good architecture always stands on the base that components are loosely coupled... let me stress the point truly loosely coupled. Let's try to understand this with a scenario and how we normally approach this problem using pure object oriented way.
Consider a scenario where we have a customer class which needs to perform insert operation on database. The customer class should be customizable to save in either Sql server or oracle database. In future we should also be able to add new databases with out disturbing the customer class.
Below is what we will do. Define two classes one for SQL server and other for Oracle. Both these classes inherit from some kind of same interface 'Idatabase'. The customer class points to this interface. So the customer class is theoretically shielded from the concrete implementation of SQL Server or oracle. But because the object creational activity needs to be done by the customer class it still needs to be aware of the concrete classes.

Figure: - General loosely coupled thinking
This is what will happen in actual implementation as shown in figure 'Concrete classes'. The customer class will be creating objects of either oracle or SQL server depending on condition. That means the customer class is exposed to the concrete classes which defeats the purpose of interface. Any change in the database classes will lead to compiling of the customer class.

Figure :- Concrete classes
Creational patterns
The next thing comes in mind is creation patterns. If we can introduce a factory who takes the creational aspect of the concrete classes thus isolating the concrete classes from the customer class.
Here are the issues with factory which makes us force to think about some other solutions:-
-
Everything is hardcoded: - The biggest issues with factory are it can not be reused across applications. All the options are hardcoded in the factory itself which makes the factory stringent to particular implementation.
-
Interface dependent: - The base on which factories stands are common interfaces. Interfaces decouple the implementation and the object creation procedure. But then all the classes should implement a common interface. This is a limitation by itself again.
-
Factories are custom: - They are very much custom to a particular implementation.
-
Everything is compile time: - All dependent objects for an object in factory have to be known at compile time.
I will not be explaining factory pattern in case you are not aware of you can read the same in my previous article SoftArchInter1.aspx
Ok, I will show you a magic...Rather than writing those huge line of code for factory...lets go the DI way (Dependency injection).
Dependency Injection
Rather than writing factory pattern, how about injecting the object directly in to the customer class. So let the customer class references the interface and we should be able to inject the concrete type in to the customer class. With this the customer class does not need to use the new keyword and is complete decoupled from the concrete classes.

Figure: - Dependency injection
Injection can be done using containers. So basically you need to put both the classes in to the container and the container will create object and inject the same in to the other class.

Figure: - Containers
With this your customer class does not need to worry about creating objects and knowing the concrete objects.
Solving problem using UNITY block
Now that we know the benefit of containers we will explore unity application block which helps us to achieve dependency injection.










Yan FengPosted Jul 22, 2010, 5:05 AM
It is very helpful. Thanks for giving such clear explaination.