I have experience is C#/ASP.NET, and I've done MVC in Ruby On Rails, so I figured making the jump to ASP.NET MVC would be a breeze. Unfortunately, I'm second-guessing my every decision. I'd like some more eyes on this to tell me if my way of doing things is insane.
My main question is around editing an entity.
My GET
looks like this:
public ActionResult Edit(int id) { var viewModel = from c in db.centres // some joins are here where c.id == id select new CentreViewModel { CentreId = c.id, CentreName = c.Name, etc... }; return View( viewModel ); }
And my POST
is something like this:
public ActionResult Edit(CentreViewModel viewModel) { // NOTE - Validation & Error handling exists, but has been omitted for berevity var centre = db.centres.First(c => c.id == viewModel.CentreId); centre = Mapper.Map<CentreViewModel, CentreEntity>(viewModel, centre); db.centres.ApplyCurrentValues( centre ); db.SaveChanges(); }
Is this kinda-sorta right? Are there any red flags here? Is ApplyCurrentValues
correct? I see others using repository.Entry()
, but I don't have a repository for my classes, near as I can tell...
from...where...select
LINQ syntax as it hides what's actually going on in a very unhelpful way. Since LINQ has been out, in 100% of the scenarios that I've seen that using the expression syntax is more powerful, terse, and more likely to convey to developers what's really happening. In your case you should be able to shorten the actual query todb.centres.First(x=>x.id == id)
.. though it is missing the projection part which you have to do separately in code.\$\endgroup\$