views:

113

answers:

5

I am currently into the testing stage of a project I am working on and am having trouble bringing the main service under test in anything other than the most basic default test cases.

The Service is currently responsible for the creation of all sub-components, which I would like to change to use an IoC container.

What container should I use to refactor quickly to a simple IoC setup, all I need is to inject the componets lsited in the following constructor:

  public DataService(string applicationFolder, string pluginFolder, 
         string persistantCacheDirectory, string settingsFolder, 
         string colorsFolder, string templatesFolder) 
 {

  _DataSourceLoaderPlugins = new DataSourceLoaderPlugins(pluginFolder, 
                                     applicationFolder, defaultConnectionString);
  _DataSourcesService = new DataSourceService(_DataSourceLoaderPlugins);

  _ChartPlugins = new ChartPlugins(pluginFolder);
  //... and about 10 more dependencies

}

I am new to IoC containers, and I'm not quite sure what the best framework for my basic needs is.

The component constructors do need some parameters from the application settings in the web.config, that will be as complicated as it gets for this project.

This service also needs to have singleton scope.

What suggestions do people have? what framework is simple, easy to setup and get under way?

A: 

Unity framework from Microsoft is good! Not hard to use!

Tony
no, it is not good, and if you say it's not hard to use, you haven't used any of the other ones.
Krzysztof Koźmic
+1  A: 

I'm a big fan of StructureMap - I didn't find it took a huge amount of effort to get up to speed on using it and the community is very active, with the creator Jeremy Miller being particularly helpful. StructureMap can certainly do what you require and it is all achievable within "standard" usage patterns - the basic tutorials should see you able to do everything you want.

The StructureMap site is a very good place to start, with lots of tutorials and code examples.

That said I imaging that most of the main stream IoC containers would meet your needs, it really comes down to personal preference, things like:

  1. What do people you know use? (For me Ninject, StructureMap and Unity)
  2. What open source projects do you admire and what do they use? (For me the Alt.Net seem to favour StructureMap)
  3. Are you a strict Microsoft shop? (Then Unity is the way to go - I'm going to be using it more since some of my client's a Microsoft only sort of companies)

If you want an overview of what is out there to help you make up your mind, there is a great blog post here that looks at all the main options.

David Hall
+2  A: 

Castle Windsor is very quick and easy to set up, here's a great tutorial with some examples.

cxfx
+1  A: 

There is a comparison of IoC librtaries here:

http://elegantcode.com/2009/01/07/ioc-libraries-compared/

I would look into frameworks that allow you to configure your app by using both config file and programatically - to aid unit testing.

Igor Zevaka
isn't this comparison outdated? ninject seems to be different nowadays, it has support to attributes and maybe its even more practical than StructureMap
Victor Rodrigues
+1  A: 

Autofac would be a great choice here - it is lightweight and very easy to get up and running.

You would register your structure like this:

var builder = new ContainerBuilder();

// Use the most resolvable constructor
builder.Register<DataSourceLoaderPlugins>().As<IDataSourceLoaderPlugins>().SingletonScoped();

// Use your own instance
builder.Register(new DataSourcesService("some path")).As<IDataSourcesService>().SingletonScoped();

// Reference another component
builder.Register(c => new ChartPlugins(c.Resolve<IDataSourcesService>(), "another path")).As<IChartPlugins>().SingletonScoped();

// ...other ~10 dependencies...

builder.Register<DataService>().SingletonScoped();

// How to resolve an instance

var container = builder.Build();

DataService dataService = container.Resolve<DataService>();

(there is XML configuration support if you prefer)

Bryan Watts