3

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

migrated from stackoverflow.com Dec 3 '14 at 0:31

This question came from our site for professional and enthusiast programmers.

  • 1
    I know it seems redundant, but separation of concerns is an important software design principal, and indeed, like most software design principals, an important "life" principal, if you really think about it. – rory.ap Dec 2 '14 at 23:59
  • I'll keep that in mind thanx :) – Enrique Olvera Dec 3 '14 at 0:56
2

I don't generally expose the DAL directly to the UI.

Normally, I create model classes as close as the final UI representation as possible. And wrappers that translate / transform one or more entities from the DB into those.

This, as you said, might seem a little bit redundant, but it is useful when the viewmodel gets complex enough so that you have different representations for querying multiples objects vs updating / inserting / deleting those.

It is actually inspired on the CQRS design pattern, where different representations are used for queries (the new wrapped classes you were talking) and updates back from the viewmodel to the entities using commands.

Here is a very good link from Martin Fowler's blog explaining CQRS.

I actually use Web API 2 for the commands as the web applications are really client-side intensive (KnockoutJS etc).

  • Ok, thanx ill check that link, umm seo theorically these wrappers Will feed my UI viewmodels right? – Enrique Olvera Dec 3 '14 at 0:54
2

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;}
    }
0

I generally don't expose my poco objects to the UI layer. Create a wrapper/domain objects layer that will transform the data of Dal layer. This way you can control the data going to the UI layer.

  • 1
    Ok i guess i have an idea, for example in my BL service class instead of returning an IEnumerable<Entity objects> i Will be returning a IEnumerable<DomainObject> – Enrique Olvera Dec 3 '14 at 0:50
  • Correct. Your Domain objects can exist in a separate project. So you can use that project in your UI and DAL layers. – Azhar Khorasany Jun 23 '17 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.