0

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!

tereško
  • 58,060
  • 25
  • 98
  • 150
apisip
  • 333
  • 1
  • 4
  • 10

2 Answers2

2

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.

k3b
  • 14,517
  • 7
  • 53
  • 85
  • Is there a built-in dependency injection container API/Class in .NET? I was wondering what is the "DependencyManager" in the link you provided. – apisip Jan 29 '13 at 10:37
  • As far as i Know there is no build in yet. Microsoft uses Unity. See [how-do-the-major-c-sharp-di-ioc-frameworks-compare](http://stackoverflow.com/questions/4581791/how-do-the-major-c-sharp-di-ioc-frameworks-compare) for a list of candidates – k3b Jan 29 '13 at 11:55
0
  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.

guillaume31
  • 13,738
  • 1
  • 32
  • 51