简体   繁体   中英

Onion Architeture - ASP.NET MVC Need to reference Infrstructure?

I'm not sure if I did a good job of searching the topics but I can't seem to find answers to my questions. Based from my understanding, Onion Architecture has the UI and Infrastructure on the same layer. Let say I have my UI in ASP.NET MVC project and I have DAL project which is my Infrastructure. Lets assume my code is like the one below:

Core:

//IOrderSaver interface was also defined on the core

public class OrderService
{
    private IOrderSaver orderSaver;

    public OrderService(IOrderSaver orderSaver)
    {
        this.orderSaver = orderSaver;
    }

    public void AcceptOrder(Order order)
    {
        orderSaver.SaveOrder(order);
    }
}

Infrastructure (DAL):

public class OrderSaverDAL : IOrderSaver
{
    //implementation goes here
}

Now, in my UI (ASP.NET MVC) I would like to instantiate OrderService class so that it can accept orders. For the UI to do that, it has to pass IOrderSaver to the constructor. It needs to pass OrderSaverDAL.

Questions :

  1. Does the UI (ASP.NET MVC) needs to reference OrderSaverDAL? From my understanding of Onion Architecture (if I understand it correctly), the UI should have no reference to the DAL. Can someone please explain?

  2. If I don't need to reference OrderSaverDAL from my ASP.NET MVC project, how will I construct OrderService within ASP.NET MVC? Can you please guide me by giving sample code on how to achieve this?

Many thanks in advance for the help!

You need an additional configuration module/layer that will wire up ui and dal. If you implement this yourself without reflection then this configuration moudul needs all reference.

Usually the onion-architecture works together with an dependency injection container that can resolve the references at runtime through configuration files or inspection of local assemblies.

  1. Does the UI (ASP.NET MVC) needs to reference OrderSaverDAL? From my understanding of Onion Architecture (if I understand it correctly), the UI should have no reference to the DAL. Can someone please explain?

You're right to say that the UI should logically not have a dependency to the DAL.

However, as k3b points out, a common way of doing dependency inversion (upon which Onion Architecture relies heavily) is through a DI container. You then need some kind of Composition Root , a place where your application's object graphs are composed using the container. The Composition Root has to be "omniscient" and needs a reference to every project in order to wire things up.

It turns out that the UI is often where the Composition Root is hosted, as the UI is a natural starting point in your application and a perfect fit to do the composition at the right time. You could have a separate project for your Composition Root, but this would be less convenient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM