Forums

Inversion of Control

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Inversion of Control

Postby torial » Tue Mar 08, 2011 7:13 am

I was thinking it might be good to make a thread for more general programming questions. If it doesn't take, that is ok. I've got a question, I'd appreciate some help with.

I've heard a lot about Inversion of Control, and as far as I can tell it seems like just a heavy use of interfaces and factories. I'm sure I'm missing some key point, or understanding of it -- and would appreciate any feedback on how it is not how I characterized it. Also, some useful / simple examples would be appreciated.
torial
 
Posts: 229
Location: IA

Re: General Programming Question(s)

Postby Charles » Wed Mar 09, 2011 5:19 pm

General questions are welcome, but is there any reason not to post each one under its own topic? Seems like that would be more modular than mashing up multiple questions into one thread.

Regarding your question, there are relevant wikipedia pages. Let's start with:
http://en.wikipedia.org/wiki/Inversion_of_control

I think it's notable that the "Background" section of that page says "It is still undecided if Inversion of Control is a design pattern, an architectural principle, or both." followed by some additional notes which do little to clarify things. I'm doubtful that learning all the nuances of what terms people have used and why is useful in day to day development. I think it's much easier to come in via the "dependency injection" term:
http://en.wikipedia.org/wiki/Dependency_Injection

Although then you have to deal with the fact that supporting libraries are called "IoC containers". One that looks interesting to me is Autofac:
http://code.google.com/p/autofac/

Essentially instead of hard coding class instantiation like so:
Code: Select all
// C#
var taskController = new TaskController();

You abstract the creation of objects like so:
Code: Select all
var taskController = container.Resolve<IController>();

And now you can configure what is created for resolving an IController. This is useful for managing multiple environments (local, dev, test, production) as well as unit testing (think "mock objects").

In some notes from 2009-09, I have this about Autofac:
-- When instantiating classes, Autofac will choose the constructor with the most resolvable parameters.
-- Autofac will recursively resolve dependencies.
-- If dependencies cannot be resolved, a useful error message is provided.
(These are all good things.)

That being said, I haven't played with it in practice and haven't used dependency injection much in my enterprise apps.

Looking at the Autofac project page today, it appears that the project is still alive and healthy. Maybe you'd like to play with it from C# and Cobra and share your findings?
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: General Programming Question(s)

Postby torial » Thu Mar 10, 2011 7:03 am

Thanks for the links / etc.

I will see if I can rename the forum topic. I was just not wanting to add a ton of topics to the main thread... that might be one-off in nature.

I think I have a decent understanding of DI, but probably not too much working knowledge. I err toward the interface approach, and occasionally use the property / constructor approach. BUT I just don't see why a framework is needed. Probably a blindspot for me.
torial
 
Posts: 229
Location: IA

Re: General Programming Question(s)

Postby Charles » Fri Mar 11, 2011 2:19 am

Eh, bring em on. Or group them if you want. Like if you have 4 questions about syntax, that could be "Syntax questions".

I'll see if I can rename this one. I have an admin privs.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 38 guests