English 中文(简体)
Good practices for handling multiple config files with DI frameworks
原标题:

In my current solution I have 18 projects and most of them have their own configuration files (app.config or web.config). Each project uses single shared BLL assembly. I m using Autofac to handle dependencies but haven t come with a decent way of managing my configuration. Configuration entries are roughly the same, but values are different. Some projects use custom config secions and some are not.

I ended up with:

  1. Create single autofac bootstrapper class to register all dependencies except configuration file wrappers.
  2. Create separate assembly (referenced by all projects) with IConfiguration interface.
  3. Create each project s own implementation of IConfiguration.
  4. Bootstrap dependencies in each project s appropriate place via shared bootstrapper.
  5. Register project s own IConfiguration implenentation separately after bootstrap registration.

I m very new to Autofac and DI in general and striving to find a good balance between complexity and extensibility.

Are there better ways to manage configuration files?

Thank you.

最佳回答

In Autofac you use Modules for this purpose. Groups of related components are encapsulated in a module, which is configured by the programmatic API.

Autofac s XML configuration has support for modules, so once you ve decided to use one in an applicaiton, you can register the module (rather than all the components it contains) in the config file.

Modules support parameters that can be forwarded to the components inside, e.g. connection strings, URIs, etc.

The documentation here should get you started: http://code.google.com/p/autofac/wiki/StructuringWithModules

HTH

Nick

问题回答

As a rule of thumb I only put dependency configuration in .config files if they represent something I want to be able to change without recompiling the app. By default, I have none such configurations.

I don t (yet) know about AutoFac, but in Castle Windsor, you can mix .config and programmatic configuration of the container, and that s what I usually do: I have a few dependencies configured in .config because I want to be able to change them without recompilation, but the rest is registered in code (often by convention).

The way I ve approached a problem similar to yours is by making a separate library that contains a specialized container - this sounds a lot like your approach. This specialized container encapsulates all the common configuration of dependencies.

In each application I have an even more specialized container that derives from the shared container and overrides whatever configuration it needs to override.

The way I understand your description, that approach seems to be not too far apart from yours, but do yourself a favor and move as much configuration as possible out of XML and into code - it actually becomes more manageable that way.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签