Note that there are some explanatory texts on larger screens.

plurals
  1. POPartial Updates for Entities with Repository/DTO patterns in MVC (prepping for API)
    primarykey
    data
    text
    <p>I've built my Domain model layer, my repository layer, and now I'm working on my DTO layer to be used by a webApi project. I'm in the middle of implementing an Update service method, and I'm wondering about partial updates. Here's my DTO class: </p> <pre><code>public class FullPersonDto { public FullPersonDto() { Friends = new List&lt;Person&gt;(); } public FullPersonDto(Person person) { PersonId = person.PersonId; DateCreated = person.DateCreated; Details = person.Details; Friends = new List&lt;Person&gt;(); foreach (Person friend in person.Friends) { Friends.Add(new PersonDto(friend)); } } [Key] public int PersonId { get; set; } [Required] public virtual DateTime DateCreated { get; set; } public virtual string Details { get; set; } public List&lt;Person&gt; Friends { get; set; } public Person ToEntity() { var person = new Person { PersonId = PersonId, DateCreated = (DateTime) DateCreated, Details = Details, Friends = new List&lt;Person&gt;() }; foreach (PersonDto friend in Friends) { person.Friends.Add(friend.ToEntity()); } return person; } } </code></pre> <p>Here's my Update method in my Repository: </p> <pre><code>public Person UpdatePerson(Person person) { var entry = _db.Entry(person); if (entry.State == EntityState.Detached) { var dbSet = _db.Set&lt;Person&gt;(); Person attachedPerson = dbSet.Find(person.PersonId); if (attachedPerson != null) { var attachedEntry = _db.Entry(attachedPerson); attachedEntry.CurrentValues.SetValues(person); // what if values are null, like ID, or DateCreated? } else { entry.State = EntityState.Modified; } } SaveChanges(); return person; } </code></pre> <p>My question is: What if I only need to update the Details of a person via my webAPI? Is the convention to construct an entire PersonDto and Update the entire object using SetValues, or is there any way I can specify that I only want a single field updated so that I don't have to send a ton of data over the wire (that I don't really need)? </p> <p>If it <em>is</em> possible to do partial updates, when is it ever good to update the entire entity? Even if I have to update 5/7 properties, it requires that I send old data for 2/7 to re-write so that SetValues doesn't write nulls into my fields from my DTO. </p> <p>Any help here would be awesome... totally new to this stuff and trying to learn everything right. Thank you. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload