3

I'm fairly across the concept of IOC containers, however I am having a little bit of difficulty in understanding how I would use one in the context of my application.

I am building an application that will have an MVC web front end and also a WCF service for outside applications to be built upon.

My question is, do I have to setup an IOC such as Windsor for both, or is there a way to setup them up so that they share the same container?

tereško
  • 58,060
  • 25
  • 98
  • 150
Chris
  • 7,996
  • 11
  • 66
  • 98
  • Are you deploying your WCF service as a standalone executable or is it IIS deployable? – Andy S Nov 19 '11 at 00:30
  • 1
    Just in case, here's a writeup on how to enable DI in WCF: http://stackoverflow.com/questions/2042609/injecting-data-to-a-wcf-service/2042858#2042858 – Mark Seemann Nov 19 '11 at 08:42
  • 1
    Thanks Mark. I've seen you are one of the most active participants on the topic of DI here on Stack Overflow. Will be grabbing a copy of your book! – Chris Nov 19 '11 at 09:03

2 Answers2

1

I'm assuming the MVC application and WCF services are separate applications. That is to say, they are a separate codebase, hosted in separate IIS sites, etc. If that is the case, then you will likely need separate containers unless you do something like putting your container in a shared assembly that both the WCF and MVC applications reference. This would really only be advantagous if the two applications both have the same dependencies.

chris.house.00
  • 3,273
  • 1
  • 27
  • 36
  • 1
    Correct, but just in case this isn't obvious I'd like to add that even if one shares the container *definition*, there will still be two separate *instances* - one in each application. So the bottom line is that sharing container definitions should be a *very rare* occurrence. – Mark Seemann Nov 19 '11 at 08:40
  • Good call, the fact that there would be two instances is an important point. – chris.house.00 Nov 19 '11 at 11:36
0

Depending on the size of the site and service, you might have IOC in both projects, but they can't really share the same container. Adding IOC to MVC is fairly straight forward, but it's considerable more tricky to add to WCF. If you'd like to add IOC to your WCF service, I'd suggest using something like CommonServiceFactory, which handles all of the plumbing for you. It can be a bit tricky to setup, because it relies on the CommonServiceLibrary abstraction, rather than on Windsor itself - but once you get up and rolling it's really easy to use.

Andy S
  • 8,641
  • 6
  • 36
  • 40
  • I'll try to put together some sample code tonight showing the setup of a MVC app and a WCF service using two separate IOC containers. It's a bit involved, but I think seeing a compilable project would really help you out. I'll update my answer accordingly. – Andy S Nov 19 '11 at 00:28