How to use Autofac in a 3 layered web application (ASP.NET MVC)?

The examples I can find use a two layer architecture, where the controllers directly use the repository classes in the data access layer. See here or here.

Where would you register components from the data access layer if you have a service or business logic layer that uses those components? The data access layer should ideally not be referenced from the user interface layer, right? So Application_Start would not be the right place to do it.

What would be a better way?

Thank you!

Answers


Something has to know about which implementations you want to use. There's typically 3 ways to accomplish this:

  1. at compile time which is what Autofac uses
  2. at runtime from predefined config file which Castle Windsor can do
  3. at runtime with a dyanamic configuartion

With Autofac you have a few choices

  1. Wire everything together in Application_Start
  2. Give the responsibility to another component which implements a factory pattern and registers the required components.

For #2 I would implement something like an IContainer interface so that your IoC framework is loosely coupled with your system. Then have your data access implemenations use that interface to registered the required components.


Need Your Help

Tracking the network bandwidth used by a process and its children

python linux process network-programming bandwidth

I'm well aware that variants of this question have been asked before, but I still haven't found a particularly good answer.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.