Question

I am currently implementing AutoFac in a multiple project .net solution which houses multiple layers of an application, MVC on the UI, a middle layer of business logic and a back end repository layer that communicates with my database.

I will be using AutoFac for dependency and to resolve the dependencies between the layers. I have a question regarding the architecture of it though.

I had planned to setup the AutoFac modules in the UI layer (MVC web app), but this would result in the web app adding references to all projects in my solution which I am not keen on as I believe the UI layer should have no knowledge of the repository project.

If I farm out the AutoFac setup to an intermediary project and that housing a reference to all projects, that would abstract out the logic. But I have the issue here where I can't fire the initial startup or even where do I put the xml configuration if we have it.

What are the views of others here, where is the best place to house my AutoFac setup?

Was it helpful?

Solution

In my opinion, there is no problem referecing the projects on your UI Layer, beucase you have to register all types on the container. The problems here is the dependency of what each layer has, I mean, the coupling types between layer. Since you are working with abstractions between the layers, no problem to reference.

We have a project that has 3 projects that need to use everything (Asp.Net MVC, some Windows Services and a WPF project), I keep the initialization of my Container (I use MS Unity) in another class library and reference everything there, because we need all types to do the register of each dependency. I referece this project in the projects (Asp.Net Mvc, Windows Services, WPF) and call where it should be (application_start event in Global.asax for Web, OnStart event on Windows Service, program file in WPF).

Awnsering your question in the comment.. You have keep something acessible in the another class library (let's call it as Project.Starter) and you reference this project (starter) on the projects you are going to use (asp.net mvc, console app, winforms, wpf, windows services, wcf, etc...). You don't need to reference the UI projects in the Starter.

Call the acessible method to init everything (container, sessionFactory if you are using NHibernate) and make sure you are doing it on the right place for each type of project as I said before (application_start event in Global.asax for Web, OnStart event on Windows Service, program file in WPF, etc...).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top