views:

520

answers:

6

Simply put, if you had to use a IoC/DI container, thats easy to deploy (not a big fan of using config/xml file for everything), stable, good documentation and supports .net, which one would it be?

It could just be me but too many xml files like one for the app, a few for ORM, a few for another component kinda gets overwhelming at times. Also, the way I see it, if there is one way of doing things (convention) lesser the chances of messing things up.

Looking forward at your suggestions and if possible experiences. My main concern is maintainability and ease of deployment.

+4  A: 

I think you'll will find this feature in most of the containers out there. Take a look at this blog post. It's slightly aged but it will give you an idea. By now the active containers probably have better support.

Cristian Libardo
Great link. Thanks!
Perpetualcoder
+2  A: 

I use Ninject and StructureMap -- both allow you to wire up your configuration without XML.

Not to be shamelessly promoting my site but I wrote a tutorial on using Ninject available here.

Ryan Lanciaux
NHibernate allows it too - http://code.google.com/p/fluent-nhibernate/
Matt Briggs
I like it when people explain software using cars in their examples :)
Perpetualcoder
+2  A: 

Perhaps you can use DI without a container?

Here is an example that uses constructor injection:

public static void Main() {
  ILogger logger = new FileLogger();
  ISession session = new ConcreteSession();
  IRepository repository = new MyDataRepositoryImpl(session, logger);

  IApplication app = new MyApplication();
  app.AddModule(new DataSelector(repository));
  app.AddModule(new Editor(repository, new MyEditorFactory(session)));
  app.AddModule(new LdapAuthenticator(session, logger));
  // ...
  app.Run();
}

If you do it this way, your executable module will have dependencies to everything, so you should limit its responsibility to just plugging things together. You can keep the bulk of your application's code clean in other modules.

Often you don't need advanced frameworks to manage your dependencies with injection.

Hallgrim
A: 

The latest version of StructureMap, 2.5.2, not only lets you configure your container without XML as Ryan said but it also has the ability to AutoWire things according to a convention, which seems to be exactly what you're looking for.

ObjectFactory.Initialize(x => x.Scan(scanner =>
                                        {
                                           scanner.TheCallingAssembly();
                                           scanner.WithDefaultConventions();
                                        }));;

From the docs on IAssemblyScanner.WithDefaultConvents():

Adds the DefaultConventionScanner to the scanning operation. i.e., a concrete class named "Something" that implements "ISomething" will be automatically added to the PluginType "ISomething"

I've not personally done much with it as I already had the existing configuration that used the Fluent Interface. But it looks promising.

EDIT: Jeremy Miller just put up a post on how to create your OWN Conventions...

Mark G
+1  A: 

Try LinFu. It's one of the most easiest and flexible containers out there and requires the least amount of code, and it's the only container in that IOC container comparison to pass ALL of the tests. Enjoy :)

plaureano
interesting !! What do u mean pass all tests ??
Perpetualcoder
The author did an exhaustive comparison of all the IOC frameworks and he even listed some features that would make an IOC framework even more flexible. LinFu was the only one on the comparison chart that implemented every "required" and optional feature on his wish list.
plaureano
Here's the updated comparison: http://code.google.com/p/net-ioc-frameworks/wiki/Charts
plaureano
Full Disclosure: You are the creator of LinFu, right? :)
Ryan Lanciaux
Yep, thats me. I'm biased, but I think the lack of setup code speaks for itself.
plaureano
A: 

Castle Windsor allows this through the Register calls. A simple scenario is, let's say you have a lot of Controllers which implement an IController interface:

container.Register(AllTypes.FromAssembly(assemblyA).BasedOn(typeof(IController));

You can simplify this further by creating an IService interface (no members) and adding it to the above. This way when you create a service, it's automatically registered.

Paul Stovell