Tuesday, October 16, 2007

Ioc is very simple and important.

I attended the CMAP code camp this past weekend and sat in on Michael Pastore's excellent presentation on Dependency Injection and Inversion of Control.

Although I was already familiar with DI/IOC his presentation managed to sharpen my understanding of why Ioc is important.

I don't have Mike's exact definition of Ioc but in my words:

"Ioc is where code surrenders control or configuration to external code"

This is very common in most Frameworks including the the .NET framework.

Consider the TrueForAll method from List from reflector:
public static bool TrueForAll<T>(T[] array, Predicate<T> match)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
if ( match == null)
{
throw new ArgumentNullException("match");
}
for (int i = 0; i < array.Length; i++)
{
if (!match(array[i]))
{
return false;
}
}
return true;
}
This method delegates control to the Predicate inverting the control to the caller. These delegate / callback methods appear all over the .NET framework.

So in my opinion Ioc is a very important concept that every developer should understand to effectively implement that Single Responsibility Principle.

No comments: