I was recently tasked with learning PRISM… The next few post will document my learning experience! PLEASE NOTE: I have no experience with previous technologies like CAB… This is truly my idiots opinion about what I have learned trying out PRISM!
I have identified some key “things” I MUST know in order to use PRISM (This list might change in the future). As I learn more about each one of these… I will post about it! The first item on my list is Unity!
“The Unity Application Block (Unity) is a lightweight extensible dependency injection container with support for constructor, property, and method call injection.”
If you want to learn more about dependency injection and IoC, here are some resources that helped me:
IoC and Unity - The Basics and Interception
IMHO… Their are 2 key things that unity provide that I should know. Lets look at the following interface
public interface ILogger
{
void LogSomething();
}
And lets create a concrete class implementing this interface
public class ConsoleLogger : ILogger
{
public void LogSomething()
{
Console.WriteLine("ConsoleLogger.LogSomething()");
}
}
This now makes it very simple for me to in the future create a different logger (ie. SqlLogger, TraceLogger, etc) and swap them at my will!
ILogger logger = new ConsoleLogger();
The only problem with the code above is that I am now tightly coupled to the ConsoleLogger… I actually need to reference the namespace in which I created ConsoleLogger!
This is were the IoC/DI stuff helps! Lets create a container
IUnityContainer container = new UnityContainer();
And then I can register types or instances with this container
container.RegisterType<ILogger, ConsoleLogger>();
and now if I ask my container for a ILogger… It will take care of creating the correct concrete class
ILogger logger = container.Resolve<ILogger>();
The code that actually creates the instance of ILogger, doesn’t need any knowledge of the concrete class!!!
It is actually very simple to create a basic IoC container yourself, here are some examples
Understanding IoC Container - sfeldman.NET
Building an IoC container in 15 lines of code
Ken Egozi's IOC in 15 lines
OK, that is the first key “thing”… The next “thing” is the dependency injection… Lets build on our first example that has a logger! Now we also have a CustomerRepository that depends on the ILogger
public class CustomerRepository : ICustomerRepository
{
ILogger _logger;
public CustomerRepository(ILogger logger)
{
_logger = logger;
}
public void GetACustomer()
{
_logger.LogSomething();
}
}
How does unity help us out here? Well, how would this have been done without unit?
ICustomerRepository repository = new CustomerRepository(new ConsoleLogger);
Do you see the tightly coupled stuff? Not so good! Assuming we already have a unity container and a logger registered… here is the unity way
ICustomerRepository repository = container.Resolve<CustomerRepository>();
I supply no parameters… The unit container auto-magically resolves it!!!
This only scratches the surfaces of what IoC/DI is capable of… Also remember that Unity is by no means the only one of its kind… here is a cool list of other containers!!!
And that concludes my first baby steps into the world of loosely coupled applications, Inversion of Control, Dependency Injection and much more…
[UPDATE] More Resources
.NET Hitman has a nice article about IoC & Unity
Andrey Shchekin has a nice 2 part post about comparing popular the IoC containers (Part 1 & Part 2)