Search code examples
c#asp.net-mvcdesign-patternsn-tier-architecturebusiness-logic

How do I pass data from presentation layer to business logic layer? (ASP.NET MVC 5)


I'm creating my first web application with ASP.NET MVC 5 and want to do it the right way. I plan on creating an architecture with a Presentation Layer (MVC), a Data Access Layer (DAL), and a Business Logic Layer (BLL). The DAL and BLL will be separate class libraries from the MVC project. The idea is that the MVC project will reference the BLL to perform business logic which will then reference the DAL for interacting with the database. I am employing the repository pattern.

My question is, how is data passed between layers? For example, let's say I have an Entity Framework model in the MVC project for a Student class with properties FirstName and LastName. Then I create a strongly typed view where the user can add a student by filling out a simple form and clicking 'Save'. The Student model will be posted to the corresponding action method in the Controller, correct? Then wouldn't the Controller need to send the Student object to the BLL and from there to the DAL to be inserted into the database? But how can that be when the BLL and DAL don't know anything about the Student class?

I don't understand how this can work without creating a circular dependency. Can someone please explain or provide code examples?

Thank you in advance.


Solution

  • I think your idea of MVC is generally correct although it's a bit confusing as to why you would get a circular dependency. From your explanation this is what I can see:

    M(model) StudentViewModel (lives in MVC app project)

    V(view) Create.cstml (model is StudentViewModel) (lives in MVC app project)

    C(controller) Navigates to the CreatePage using a StudentViewModel (lives in MVC app project)

    When the controller does a Post you get the populated StudentViewModel injected into your method, this is what can happen:

    • you simply call StudentViewModel.Save(...) (MVC app project)
    • the save method within the view model can create an instance of your BLL and do the needful
    • inside the BLL you can create your Student (actual entity) with the information you have and persist the item by calling the DAL

    So you end up with a dependency graph like this

    MVC app references BLL references DAL references entity layer.

    There are lots of other ways to make the above structure better using dependency injection etc but this should at least answer some questions (and create lots of other ones) :)