0

Is it possible to reference an assembly from a shared location or the GAC with Unity?

Assume I have two projects (under the same solution):

  • MyProject.Data (Repository interfaces)
  • MyProject.Data.EntityFramework (Repository Entity Framework Implementation)

I want to use IoC in an ASP.NET MVC application (MyProject.Web) which is also under the same solution, but I don't want to add project references for both the interfaces AND the implementation. Ideally, I want to add a project reference to the interfaces, and load the implementation from a shared location or the GAC. This prevents any of the code in MyProject.Web creating instances of a specific implementation - this should always be done by the IoC provider.

I could decide to create an nHibernate implementation and IoC would make the switch a lot easier. If there was any direct coupling, it'd be a nightmare. And this leads me back to the question. Lets remove the possibility for developers to instantiate concrete implementations without asking the IoC container how to resolve an interface.

Andrew Gunn
  • 95
  • 1
  • 8

1 Answers1

1

Unity can only instantiate types it can get hold of, so your MyProject.Data.EntityFramework assembly has to be in your MVC output directory if you want to use its types. If you don't want to directly reference the project you could copy the assembly in using a build action, but going down that route means you have to configure Unity using XML instead of the API, which can be a bit of a pain and loses you the advantages of type-checking you get with the API.

Ultimately I'd go with using the API and directly referencing the project. This should be accompanied by education so other developers know how the system architecture is supposed to be used, as well as code reviews to help enforce it.

Steve Wilkes
  • 7,085
  • 3
  • 29
  • 32
  • Thanks for the reply. I didn't mind using XML but I see your point. Maybe they'll add this feature in the future. – Andrew Gunn May 31 '11 at 20:04
  • I'm going to create a separate IoC project which will reference the interface and implementation projects. This will contain a container that has been created using the fluent syntax (win). The web project can reference the IoC project and any interface projects, thus decoupling it from the implementations (win win). – Andrew Gunn Jun 01 '11 at 13:13