0

The product have some fields that can not be changed, so I want to bind the object with only selected field.

For now I'm doing this(below) way (like binding manually), but I believe there is better and clean way. How to binding Model object to model object with only selected fields?

[HttpPut]
public JsonResult update(Product editedProduct) {

    Product originalProduct = unitOfWork.ProductRepository.Get(filter: q => q.no == editedProduct.no).Single();

    originalProduct.name = editedProduct.name;
    originalProduct.modelNo = editedProduct.modelNo;
    originalProduct.size = editedProduct.size;
    originalProduct.color = editedProduct.color;
    originalProduct.description = editedProduct.description;
    originalProduct.price = editedProduct.price;

    //originalProduct.upc = editedProduct.upc; //UPC can not be changed
    //originalProduct.sku = editedProduct.sku; //SKU can not be changed

    unitOfWork.Save();

    return Json(new { success = true });

}

please advise me,

Expert wanna be
  • 10,218
  • 26
  • 105
  • 158
  • Take a look here https://github.com/AutoMapper/AutoMapper . It might be that you want. – Christos Feb 01 '15 at 15:41
  • Why not use the form collection and a try update model instead – Saravanan Feb 01 '15 at 16:01
  • @saravanan Thank you for your reply, the data(model) is from model form, so the object contain full field information and in frontend, only editable fields can be edited. But I just want to make sure in backend also. – Expert wanna be Feb 01 '15 at 16:09

3 Answers3

1

Well, don't do it.

For this case you should create a separate ViewModel with only necessary fields.

1

In my opinion there is absolutely nothing wrong with this approach. It is possible to do some things differently but it does not mean it is better.

  • Create a DTO/ViewModel class to represent the class you accept and return from the service. This way you can have different shapes of the data if you need it. For example you can skip a security critical field. I think this will be an improvement.
  • Use a framework like AutoMapper to do the mapping between the objects. This is quite popular approach but I personally prefer explicitly copying the fields.
  • You can update the object without retrieving it. I assume you are using Entity Framework. You can refer to this question for details - How to update a record without selecting that record again in ADO.NET Entity Framework? . I personally don't think that this will improve your code. I think you should do it only if you have performance issues with your current approach.
  • You can put the mapping code in your repository in an Update method or something.

BTW it seems like your repository is currently useless. You are just writing a wrapper around your ORM which makes the code more complex and more buggy. Repository is an anti-pattern when you are using an ORM. Your ORM is the repository.

Community
  • 1
  • 1
Stilgar
  • 22,354
  • 14
  • 64
  • 101
0

I would not expose the setter in the class for example.

public class Product{
    public string upc {get;}
}

This will not allow the property to be set.

Rajdeep Dosanjh
  • 1,157
  • 1
  • 9
  • 20