Complete Guide to use IoC with ASP.NET MVC (Using Castle Windsor and MVCContrib) Part #1
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…
Facebook comments:
3 Responses to Complete Guide to use IoC with ASP.NET MVC (Using Castle Windsor and MVCContrib) Part #1
Leave a Reply Cancel reply
Archives
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- March 2011
- February 2011
- January 2011
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- January 2010
- December 2009
- October 2009
- August 2009
- July 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- June 2007
Tags
.net 2008 asp.net asp.net mvc asus eee c# corona cpa doctrine dom eee eee pc Internet Marketing ios iPad iphone iPhone development iphone sdk javascript make money netbook online marketing pagerank passion php playstation playstation 3 ps3 ranking ror ruby on rails self help Self Improvement self motivation seo sql success symfony take action the secret ultra compact laptop ultra compact notebook vb.net web development wordpress





[...] to VoteComplete Guide to use IoC with ASP.NET MVC (Using Castle Windsor and MVCContrib) Part #1 (12/10/2009…Thursday, December 10, 2009 from SereeHi guys, Today I’m going to share with you if you get stuck [...]
Hello..
When you will post Part 2 of Complete Guide to use IoC with ASP.NET MVC (Using Castle Windsor and MVCContrib).
Thank you so much for part 1
Hi Sonam,
Sorry for long lately on part #2, it is in draft state.
However, the information is outdated now due to the quick advancement of ASP.NET MVC.
Also, I already moved my primary development platform to Ruby on Rails & PHP, so I didn’t have much time to involves on ASP.NET MVC 3.
However, I will try to write a fresh content about IoC on ASP.NET MVC 3 in my future and will be notify here.
Anyway, if you have a question regards to IoC on ASP.NET MVC, you can leave it here and I will try my best to help you sort it out.
Thanks for reading,
Seree