I’d like to open the floor for discussion on various ORMs, non-ORMs, and their relative advantages and disadvantages.
This idea stemmed from a recent project, which was done code-first as a proof of concept. This means that all it had was controllers, views, and viewmodels. No domain, service, or persistence layers existed yet.
It occurred to me, while looking at several of the controller actions, that I had all that I needed to save the data, without the need to first translate it into a domain representation.
In a typical DDD application, when adding an order to a customer, the customer is first fetched (if not saved in session), the order is created and added to the customer, then the customer is saved.
But why couldn’t I just take the incoming viewmodel and persist that using an SqlClient or Odbc call? The reality is, I could.
So what I was hoping to discuss here is why so many people nowadays are flocking to ORMs? Is it because they remove the requirement of understanding SQL? Or is it the opimization of said SQL (something any decent DBA should know anyway), or the oop nature of them?
Consider the two actions below. Both are object oriented, perform validation, and use repositories. Which one is better (in your opinion) and why?
OrderController.cs
[HttpPost]
public ActionResult Add(AddOrderViewModel model)
{
if (ModelState.IsValid)
{
Customer customer = Session[“customer”];
Order order = model.ToOrder();
customer.AddOrder(order);
customerRepository.Save(customer);
return RedirectToAction(“Overview”);
}
return View(model);
}
vs
[HttpPost]
public ActionResult Add(AddOrderViewModel model)
{
if (ModelState.IsValid)
{
orderRepository.Save(model);
return RedirectToAction(“Overview”);
}
return View(model);
}
In the same line of reasoning, selecting records could be done a lot easier by having the repository assemble and return view models rather than domain objects as well.
var models = orderRepository.GetForCustomer(customerId);
Of course this would make view models behave more like entities.
Feel free to drop your thoughts here. I’d really like to hear them.