Can someone guide me to some basic tutorials on IOC? (preferably .net/c#).
I need some hands on code to wrap my head around it :)
Is IOC similiar to DDD? or test-driven-design?
Can someone guide me to some basic tutorials on IOC? (preferably .net/c#).
I need some hands on code to wrap my head around it :)
Is IOC similiar to DDD? or test-driven-design?
Inversion of Control is the abstraction of logic's structure. One approach (often used synonymously with IoC) is Dependency Injection, which abstracts references between objects. When objects are freed from an application's implementation details, such as which class implements which service, they are free to focus on their core functions.
For example, let's say you have a class that needs to map
Foo
objects toBar
objects. You might write this:and use it like this:
While valid, this is also rigid.
NeedsToMapFoos
only cares that mapping occurs, not that it occurs in any specific way.We can represent the concept of "operation sans implementation" with an interface:
and declare a dependency on that operation:
Now
NeedsToMapFoos
has less knowledge, which means it is less complex. Instead of performing administrative duties, it is free to focus on the business case.Well-factored objects like this are also more adaptable. Just as diamonds are rigid and clay is malleable, internal structure determines response to change.
Finally, logic written in this style is also flexible. Let's say
FooMapper.Map
is an expensive operation and should be cached. You can use the decorator pattern to wrap the existing implementation and seamlessly pass it toNeedsToMapFoos
: