0
votes

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.

1
Why the Model can not be referenced by all projects. Is it because you are defining it in the MVC Web Application? Can you separate your Model objects into an Assembly containing Plain Old .Net Calsses referenced by MVC and Layers?n00b
@Braim Correct, I am defining the model in the Models folder of the MVC Web Application. I suppose I could define the models in a separate Class Library, but is that the standard convention?Matthew
I have also seen people using a 'Data Mapper' which is introduced between the controller and the business logic. But that is not making it much better, because either the mapping has to be done based on Conventions (names)/XSDs/Mapping Configuration or a similar form of dependency will be formed.n00b

1 Answers

1
votes

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) :)