简体   繁体   中英

IOC framework (.net) for beginners?

I just started learning Unity yesterday, gave up on it b/c of the lack of examples. I moved onto Castle Windsor because they seemed to have the most documentation/activity. however, I reached a point on their tutorial which they don't explain. To me, this means they've assumed that I have this knowledge. Not only that, but their downloadable example project wasn't compatible with VS2010 - I had to hack the .csproj file just to get it working.

I've seen the many articles on .net IOC containers, but my question is 'which is best for the beginner?'. I want to learn this, but find it frustratingly difficult when a thorough tutorial doesn't seem to exist.

To throw another frustrating aspect out there - I've only worked with vb.net, and all the examples out there use c#. I know they are basically the same language, but it is none-the-less annoying to have to keep using the code-converter, and then figure stuff out when it doesn't work. I can't use c# b/c of co-workers who might in the future have to change the code.

Edit: I would define 'easy to learn' as, in order of preference: tutorial/documentation/mailing list or forum

Seems like this question always comes up... its a matter of preference, unity is quick, dirty, but documentation is thin. But then again most MS documentation is thin.

I have used Unity in the past and it has pretty much done everything I needed it to.

If you are looking for alternatives Scott Hanselman has a blog that goes through them

It might help if you explain what your problem is; it's easier for the community to resolve specific problems than answer "is there an easier solution that doesn't require me to understand something that I am not articulating". However, it would be a good idea to make sure you're comfortable with Fowler's articles on the subject ( http://martinfowler.com/articles/injection.html and http://martinfowler.com/bliki/InversionOfControl.html ). If you understand those articles, you'll be in a better position to ask questions that are specific to your problem domain.

To directly answer your question, however, I found Ninject to be the most intuitive dependency injection framework, thanks mostly to a fairly pleasant fluent interface for binding. StructureMap was almost as pleasant. I have limited experience with Castle Windsor, usually only incidental use in other projects. I worked from the SharpArchitecture project sample and replaced Windsor with Ninject on previous projects.

As for use of a DI library, the most important thing, in my experience, is to limit the span of control of your DI framework to a relatively small footprint. For example, I find that, in web applications, DI containers are generally best when they are only invoked by your ControllerFactory.

The most tedious DI container that I've worked with was in Spring (though I'm sure it's better than when I last used it), but there are some really excellent examples on how to use it since it has been so influential in both the Java and .Net community.

Edit: Presuming your goal is to learn how DI can be applied in a not-completely-trivial sample project, consider looking at the example project that ships with SharpArchitecture (probably after reading the tutorial on SharpArchitecture's web site). See http://www.sharparchitecture.net/

The sample project is in C#, and SharpArchitecture's toolkit encompasses a lot more than dependency injection, but that should take you a lot further than most of the trivial sample projects I've seen online.

I think the thing you need to grok first is the concept of DI and what it gives you. Once you understand that learning the different implementations is much easier.

So my answer to your question is: "Your own" that is the easiest DI framework to learn.

Learn the basics of DI and then build your own simple container and it will make things clearer IMO. You can build your own DI framework in an hour or two.

James Kovacs has a great intro to DI http://msdn.microsoft.com/en-ca/magazine/cc337885.aspx

Martin Fowler has two great articles explaining the concepts http://martinfowler.com/bliki/InversionOfControl.html

http://martinfowler.com/articles/injection.html

I've used Unity which is from Microsoft and part of the Enterprise Library. It provides both configuration-based and code-based mappings.

There's a good intro here:

http://msdn.microsoft.com/en-us/library/ff650320.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM