1

Some project has some inversion-of-control components.

These are components working in different conceptual areas - business, graphical user interface, globalization, storage... -.

Which would be your solution?

  • All components in a single container.
  • A container per conceptual area.

I'll appreciate pros and cons.

Thanks for your effort in advance!

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • I found this other question which is related to my one: http://stackoverflow.com/questions/879930/is-it-correct-to-have-many-castle-windsor-containers-per-application-if-those-con – Matías Fidemraizer May 02 '11 at 12:22

1 Answers1

1

Just for consistency i would recommend you use the same implementation (AutoFac,Unity,Spring whaterver) of the container for all the conceptual areas.

If it's not hard to keep one container per conceptual area then do so. You will avoid problems when for the same dependency (ex IEmailService ) you need different implementations for difference conceptual areas (RemoteEmailService, LocalEmailService) . One disadvantage would be that if you have shared components you need to keep track of them and register them in each container for each area.

If your IOC implementation supports parent-child containers (AutoFac) you can have shared components in the parent container and component specific in each child container.

If your IOC implementation makes it hard to use separate containers for each area, don't fight it, either change it or use one global container but be careful when registering components.

Anyway make sure your code is IOC implementation agnostic. This way you can maybe start with a "one container for everything" and later if required move to "container per area".

Iulian Margarintescu
  • 2,656
  • 21
  • 22
  • +1 I'm using Castle Windsor on .NET 4.0. It's your description of separation of conceptual areas what's the motivation of this answer. And I find a good argument the point of having components implementing same interface but in different areas. Ah, I wasn't thinking on using different IoC implementations, but different containers using same IoC API. What's in my head now (and maybe it's not answered in your text)? Performance consideration. Multiple containers can affect application/service startup time. What do you think about this? – Matías Fidemraizer May 02 '11 at 12:19
  • I would not worry about performance until i have some clear profiler results. Can you actually measure the impact of multiple containers with fewer components each over one container with all the components? I have not used windsor yet but a quick google shows it supports parent-child containers - maybe you could also profile a test with these. If startup times are a real performance problem you could maybe start applying other optimization based on profiler output. – Iulian Margarintescu May 02 '11 at 12:43
  • Yeah, I've googled that and I found that child containers support. I can't measure performance impact because the project isn't that large for now, but it'll be a big one in the near future. Startup time is important because it's a Web client application and a WCF service but as you've stated, this can be optimized later. I'm going to mark your answer as the right one because you provided me enough info (child and/or multiple containers approach). Thank you very much. – Matías Fidemraizer May 02 '11 at 12:58