mardi 24 avril 2018

Update Model in Service Layer without reading the whole entity in MVVM

As known, the service layer is responsible for updating (and of course reading, writing and deleting) the models (or I may call them entities). Let us ignore Repository layer for now, because I do not like it, it just hides a lot of Entity Framework features.

The flow of data in a well designed system should be:

Service (model) <<<-->>> Controller (mapping Model <-> ViewModel)

To be able to update the Model in the database by the above data flow, I have to read the whole model from the database to the service to the controller, and use AutoMapper (for example) to map the data I got from the ViewModel to the Model (now we have the old model with some changed values). Now I can pass that edited Model back to the Service, and execute DbContext.Update(Model).

The cons of that is:

  • We had to issue additional read query to read the whole model (We had to do that, otherwise, DbContext.Update(Model) will leave the none mapped Property to default).

  • Also, Update query has been generated for the whole model (although I might only changed small part of the Model.)

I sometimes find that design patterns forces to hide a lot of feature which may make the program more efficient.

Is there any approach (or let us say a design pattern or any editing to service pattern) where I can map ViewModel to Model, and pass the Model to the Service, and update only the mapped Properties (so there is no need to read the whole entity before mapping the properties)?

Aucun commentaire:

Enregistrer un commentaire