0
//This represents my rows in my database
public class PersonModel
{
    public int Id{get;set;}
    public string FirstName{get;set;}
    public string LastName{get;set;}
    public DateTime CreatedBy{get;set;}

}


//This is what the client receives
public class PersonDto
{
    public int Id{get;set;}
    public string FirstName { get; set; }
    public string LastName { get; set; }    
}

//I have this DTO for other Views that 
//I only need to show FirstName

public class PersonFirstNameOnlyDto
{
    public int Id {get;set;}
    public string FirstName{get;set;}   
}

public class Person
{
    IPerson person;
    public Person(IPerson person)
    {
        this.person=person;
    }
    public void SavePerson(PersonDto person)
    {
        this.person.SavePerson(person); 
    }
}


public interface IPerson
{
     void SavePerson(PersonDto person);
}

When saving a Person, I need to pass in PersonDTO to the SavePerson method but how if i want to pass the PersonFirstNameOnlyDto?. What I have in mind is to convert the PersonFirstNameOnlyDto into PersonDto(I can use AutoMapper here) or add another method to the Person class that accepts a PersonFirstNameOnlyDto. I am just thinking if there's a better way to do this, maybe using Generics but I really don't know how I can achieve it.

Regards

  • Why don't you just use PersonDto for full and partial data? Why a dedicated Dto for just a single field? – Laiv Oct 29 '18 at 7:41
  • That's because on my SELECT query, i am just selecting specific columns from my database and map it straight to a DTO using Dapper. – Denden Oct 29 '18 at 7:46
  • And Dapper, doesn't allow you to use PersonDTO for mapping only firstName? I ask this because What I have in mind is to convert the PersonFirstNameOnlyDto into PersonDto . Basically you are thinking to use PersonDTO to hold full or partial data. So, what's the point on implementing DTO per query-or-fields? – Laiv Oct 29 '18 at 8:00
  • @Laiv: Why don't you just use PersonDto for full and partial data? Why a dedicated Dto for just a single field? While I'm not a strong proponent of this, I've heard several arguments (from lead/senior developers) that the DTO's should be separated per view so that your views are as independent from one another as they can be. – Flater Oct 29 '18 at 8:21
  • that the DTO's should be separated per view So, OP is conditioning and coupling view with DB row mappers. Not a big deal better improvement. DTOs and ORM row mappers have nothing to do with each other, however, your argument points out to the contrary. – Laiv Oct 29 '18 at 8:40
1

I would suggest that creating all those DTOs leads to: 1. Extra code to have to understand in that there are multiple classes for the same thing, eg a person, 2. The need for something like Dapper to handle the conversion between all these classes, 3. Problems such as you describe in that different parts of the system need different versions of a person and tying it all together can be different.

So I'd take another approach. Just have the one class, PersonModel that is saved and loaded from the database. Then use interfaces to expose just the parts of that class to the views, eg:

public interface IPersonFirstNameOnlyDto
{
    public int Id {get;set;}
    public string FirstName{get;set;}   
}

public interface IPersonDto
{
    public int Id{get;set;}
    public string FirstName { get; set; }
    public string LastName { get; set; }    
}

public class PersonModel : IPersonFirstNameOnlyDto, IPersonDto
{
    public int Id{get;set;}
    public string FirstName{get;set;}
    public string LastName{get;set;}
    public DateTime CreatedBy{get;set;}
}

Then those views that only need the first name just refer to IPersonFirstNameOnlyDto etc, so they only have the properties they need exposed to them.

  • Then use interfaces to expose just the parts of that class to the views aka, interface segregation. I don't know why, but I see it kinda "overkill". Ideally, you don't want to mix view layer components with persistence and domain layer components. So, what about implementing 2 different data models? PersonModel <- AutoMapper -> DTOs ? – Laiv Oct 29 '18 at 11:32
  • @Laiv, that mixing of view layer components with persistence and domain layer components is an implementation detail. All the view layer knows is that it'll receive an implementation of IPersonFirstNameOnlyDto etc. Where that implementation came from (persistence layer, test mock etc) are unknown to it. – David Arno Oct 29 '18 at 11:44

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.