Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am developing an ASP.NET MVC application, and I have three projects: UI (with

ID: 645822 • Letter: I

Question

I am developing an ASP.NET MVC application, and I have three projects:

UI (with the system.web.mvc reference, BL and DAL references)
BL (business facade and business objects)
DAL (contains my entities/poco classes and connects to the DB using an external assembly)

I am building my viewmodels in the UI layer, using the data contained in my DAL entities, so my UI has reference to BLL and DAL, but I'm not sure if a DAL reference should exist in the UI project.

I was thinking about creating a class in the BL layer That uses the data contained in the DAL entities, and then creating my viewmodels using the new class, but it seems very repeatitive and redundant.

What do you guys think? What I want to do is to build my viewmodels using data contained in data base

Explanation / Answer

You are right, DAL reference should NOT exist in the UI project. You should instead create DTO objects for sending/recieving data to your BLL. It can be a separate project called DTOs or can be included in the BLL by creating specific folders like Customer and placing its facade classes along with the DTOs in that folder. BLL will be responsible for mapping the DAL objects to BLL DTOs back and forth (I would use AutoMaper for this purpose).

So you should create TWO DTOs for each DAL object. Like if we have a Customer class in DAL, then I would create TWO DTOs for it as follows:

public class CustomerBrief
{
public string Name {get; set;}
public string Address {get; set;}
public string Email {get; set;}
}

//excuse my naming conventions :)
public class CustomerFull : CustomerBrief
{
public string SomeOtherFieldThatYouNeedInDetail_1 {get; set;}
public string SomeOtherFieldThatYouNeedInDetail_2 {get; set;}
public string SomeOtherFieldThatYouNeedInDetail_3 {get; set;}
}
So when you need a list of Customers, you can return List<CustomerBrief>. And when you need a customer's full detail, you can return CustomerFull. This way only the data that is actually needed is passed around the web.

Now, you would reference only the BLL in your UI project and now your Customer ViewModel may look like following:

public class CustomerVM
{
public CustomerFull Customer {get; set;}
public List<CountryBrief> Countries {get; set;}
}