开发者

Reference an assembly from a shared location or the GAC with Unity

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开发者_JAVA百科 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜