views:

47

answers:

2

I am trying to use structure map for the first time. I have used ioc containers before, but always with xml config. As structure map uses config through code (I know it can be done in xml as well, but most examples are using the config through code) I am running into some issues with references.

Let's work with the following example (not a real one):

3 assemblies.

DataLayer BusinessLayer Services

The BusinessLayer references the DataLayer (so it can retrieve data) Both the DataLayer and the BusinessLayer references the Services assembly because it contains code for logging.

I want the Log component to be managed by StructureMap so I can change the log component.

In the business layer there is also some components being served by StructureMap.

So my problem is now.

Where do I put the structure map config?

If I place it in the BusinessLayer, the DataLayer cannot use the Log component (because a reference back to the business layer would resolve in a cyclic reference). I cannot put it in the Services project because there is also some components in the BusinessLayer (and the business layer has a reference to the service assembly).

I hope I've explained myself well enough. The main issue here is that StructureMap needs a reference to an assembly before it can be configured, which I find a bit problematic to work with. Am I doing it all wrong?

+3  A: 

The container configuration has to be in the top level, at presentation layer: web site, web service, console application, windows application or windows service. So you can inject everything you need wherever you want.

onof
Sound advice, but what if I have multiple "top layers"? E.g. in my project I both have some workflows running in SharePoint (with no UI) and some WebParts in a web project (it is a sharepoint project).
It dependes on how these workflows run. You could need more than a container. I have a web project containing some web services. I have a container for the web application and two containers for the web services. These containers are children (Unity allows children containers, I don't know about StructrureMap) of a common container containing common things (logging etc).
onof
Ok thanks. I guess that an implicit question in my "main" question was if it is "nice" to have multiple containers. I can always use Registers to contain common config.
A: 

I place programmatic configuration (IoC or any other) in a bootstrap method at the process level. Therefore in a client/server system, the client process will have it's own bootstrap for any config it needs and the server process will have a separate bootstrap method for any config it needs (this may be reused code, or they may be very different requirements). For example, the client bootstrap doesn't need to know about the DAL configuration and the server bootstrap doesn't need to know about any UI configuration.

Dr Herbie