开发者

.Net Refactoring application to use Dependency Injection

开发者 https://www.devze.com 2022-12-14 06:56 出处:网络
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.

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 DataSou开发者_如何学编程rceService(_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?


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


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)


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消