Hi guys,

Today I’m going to share with you if you get stuck trying to learn IoC and implement it into your own ASP.NET MVC application.

I’ll not going to write a deep content about IoC but somehow I will share the short brief of the concept of IoC and how you can easily implement IoC in your ASP.NET MVC application using MVCContrib.

First of all, let’s share together what is IoC? and what it is for?

IoC stands for “Inversion of Control” which sometime it had been named as DI or “Dependency Injection”.

Actually, both is the same, just a different name.

IoC or DI is the concept that looks like how you code your application to make it support for plug-in architecture.

Such as the old days WinAmp which it can output multiple result from audio files such as…

  • Output a sound to speaker (playing music)
  • Output a sound to MP3 (encoding)
  • Output a sound to WAV (decoding)

and blah blah blah…

There are several plug-ins for WinAmp as a DLL, when you need some new to make WinAmp to output to, you just coding a new DLL and implement what you want there.

This way, when you code the main app, you will never know or never care of what the output will do, it (main app) just output the sound stream to the output plug-in.

This is a good pattern and fit for ASP.NET MVC approach as it’s a real separation-of-concern.

And here is a snippet code of how you can implement this type of application. (plug-in enabled)

- Create an Interface for the output such as …

public interface IPlayerOutput
{
void Play(byte[] soundStream);

void Pause();

void Resume();

void Stop();
}

 

- In the main app, you should add reference to IPlayerOutput interface and using it like this…

public void btnPlayClicked(object sender)

{

IPlayerOutput *output = new XXX; //Replace XXX with the code to create an instance of the chosen plug-in – usually populate from .DLL files within the output plug-in folder

output.Play(YYY); //Replace YYY with a stream of audio file

}

 

- This way, the main app will never care of what the output plug-in will do, it just send the audio stream to.

- When you want to code for a send-to-speaker plug-in, you just implement the IPlayerOutput interface and write code to send-to-speak there such as…

public class SpeakerOutput : IPlayerOutput

{

 

public void Play(byte[] stream)

{

// 1. Decode the audio stream

// 2. Send RAW to speaker

}

 

}

 

- If you want to code for WAV output plug-in, just do the same thing but different in the Play function such as…

public class WavOutput : IPlayerOutput

{

 

public void Play(byte[] stream)

{

// 1. Decode the audio stream as WAV format

// 2. Save WAV data to disk

}

 

}

 

- Then, you can let user choose which DLL to be used and load it into the IPlayerOutput interface within the main app. Voila!

 

So… How it is related to IoC?

Actually, it is the same concept as the plug-in architecture.

The main point is to make each components of application loosely coupled.

To make it simple, IoC is the concept to make your application’s layers independent as much as possible.

You can do IoC by coding like the above set of code in your application. However, it will take you sometime to do yourself.

And that’s why Castle Windsor come into place… They already created this type of things for you! :)

Anyway, I will not show here how it is being used by generic application. Instead of, I will use Castle Windsor in combination with MVCContrib to do IoC in ASP.NET MVC application.

All I can say is… “It is very easy to do, not that hard like what the concept did”.

To be continued on Part #2…