Can someone guide me to some basic tutorials on inversion of control (IoC)? (preferably .net/c#).
I need some hands on code to wrap my head around it :)
解决方案
IOC, or inversion of control, is, in most apps, inverting the route through the code from UI to database. This si not a complete answer, but it is one of the easiest ways to get your head around the concept.
If you want to learn IOC, get into TDD, as you will find setting up tests a lot easier once you invert.
Example:
Typical flow of most .NET apps I have seen is something like this:
UserCollection col = BusinessLayer.Class.GetLoggedInUsers();
//Business logic
return col;
then business is like this:
UserTable table = DataLayer.Class.GetLoggedInUsers();
return table;
etc. This is all pseudocode. To use IOC in this example, you add an interface for the data layer class, like IUserRepository. You can use generics, and I would recommend underneath the hood.
You can then do something like this:
IUserRepository repository = SetUpRepository();
UserCollection col = BusinessLayer.Class.GetUsers(repository);
Why is this important? For testing, you can create a mock repository and feed it to the business class. The mock contains data that is always the same, which means you are exercising your code, not testing end to end.
If you want C#, here is a basic example on weblogs.asp.net :