17

I have a web solution (in VS2010) with two sub-projects:

  1. Domain which holds the Model classes (mapped to database tables via Entity Framework) and Services which (besides other stuff) are responsible for CRUD operations

  2. WebUI which references the Domain project

For the first pages I've created I have used the Model classes from the Domain project directly as Model in my strongly typed Views because the classes were small and I wanted to display and modify all properties.

Now I have a page which should only work with a small part of all properties of the corresponding Domain Model. I retrieve those properties by using a projection of the query result in my Service class. But I need to project into a type - and here come my questions about the solutions I can think of:

  1. I introduce ViewModels which live in the WebUI project and expose IQueryables and the EF data context from the service to the WebUI project. Then I could directly project into those ViewModels.

  2. If I don't want to expose IQueryables and the EF data context I put the ViewModel classes in the Domain project, then I can return the ViewModels directly as result of the queries and projections from the Service classes.

  3. In addition to the ViewModels in the WebUI project I introduce Data transfer objects which move the data from the queries in the Service classes to the ViewModels.

Solution 1 and 2 look like the same amount of work and I am inclined to prefer solution 2 to keep all the database concerns in a separate project. But somehow it sounds wrong to have View-Models in the Domain project.

Solution 3 sounds like a lot more work since I have more classes to create and to care about the Model-DTO-ViewModel mapping. I also don't understand what would be the difference between the DTOs and the ViewModels. Aren't the ViewModels exactly the collection of the selected properties of my Model class which I want to display? Wouldn't they contain the same members as the DTOs? Why would I want to differentiate between ViewModels and DTO?

Which of these three solutions is preferable and what are the benefits and downsides? Are there other options?

Thank you for feedback in advance!

Edit (because I had perhaps a too long wall of text and have been asked for code)

Example: I have a Customer Entity ...

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public City { get; set; }
    // ... and many more properties
}

... and want to create a View which only shows (and perhaps allows to edit) the Name of customers in a list. In a Service class I extract the data I need for the View via a projection:

public class CustomerService
{
    public List<SomeClass1> GetCustomerNameList()
    {
        using (var dbContext = new MyDbContext())
        {
            return dbContext.Customers
                .Select(c => new SomeClass1
                             {
                                 ID = c.ID,
                                 Name = c.Name
                             })
                .ToList();
        }
    }
}

Then there is a CustomerController with an action method. How should this look like?

Either this way (a) ...

public ActionResult Index()
{
    List<SomeClass1> list = _service.GetCustomerNameList();
    return View(list);
}

... or better this way (b):

public ActionResult Index()
{
    List<SomeClass1> list = _service.GetCustomerNameList();

    List<SomeClass2> newList = CreateNewList(list);

    return View(newList);
}

With respect to option 3 above I'd say: SomeClass1 (lives in Domain project) is a DTO and SomeClass2 (lives in WebUI project) is a ViewModel.

I am wondering if it ever makes sense to distinguish the two classes. Why wouldn't I always choose option (a) for the controller action (because it's easier)? Are there reasons to introduce the ViewModel (SomeClass2) in addition to the DTO (SomeClass1)?

Slauma
  • 175,098
  • 59
  • 401
  • 420
  • @jfar: Now there is an example with code. – Slauma May 13 '11 at 18:33
  • I'm curious - how did your chosen approach work out for you?? – kenwarner Jan 24 '12 at 18:55
  • 1
    @qntmfred: I'm quite satisfied. I'm using now the approach in the accepted answer for GET requests (extension methods with projections from EF model directly into ViewModel, projection happens in DB, no DTO involved). For POST requests I map the ViewModel coming into the POST action into a DTO and then call service methods with that DTO. (I don't map directly into the EF model because there is sometimes business logic involved and I don't want to have that in the presentation layer and I can't use the ViewModel for the service because the service layer doesn't know the ViewModel classes.) – Slauma Jan 25 '12 at 15:09
  • Great question, great discussion in general. As if you read my mind and posted the question. Thanks! – Liviu Mandras Jun 28 '13 at 10:08

3 Answers3

9

I would solve your problem by using an auto-mapping tool (like AutoMapper) to do the mapping for you. In cases where the mapping is easy (for example if all properties from one class should be mapped to properties with the same name on another class) AutoMapper will be able to do all the hook-up work for you, and you'll have to give a couple of lines of code to note that there should be a map between the two at all.

That way, you can have your entities in Domain, and a couple of view model classes in your WebUI, and somewhere (preferrably in WebUI or a sub namespace of the same) define maps between them. Your view models will in effect be DTOs, but you won't have to worry much about the conversion process between the domain and your DTO classes.

Note: I would strongly recommend against giving your Domain entities straight to the views of your MVC web UI. You don't want EF to "stick around" all the way to the front-end layer, in case you later want to use something other than EF.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • "Your view models will in effect be DTOs": Do you mean that I don't need separate DTO classes? Keep the "projection" in mind: I don't query for a full entity but only for a subset of properties. Which type would I project those properties into? If I don't have separate DTOs I would need to project them into the ViewModels directly, wouldn't I? But then my ViewModel classes would be part of the Domain project and not the WebUI. – Slauma May 13 '11 at 17:05
  • 1
    +1 - I asked a [similar question](http://stackoverflow.com/questions/5119349/ef-entities-vs-service-models-vs-view-models-mvc), and got a few good responses. Those may be of help too. – Jerad Rose May 13 '11 at 18:37
  • @Slauma: Since you're using EF, which by default uses lazy loading, you can do the projection in your controller, and map the projection directly into you `ViewModel`s, without getting more data than you need from your database. – Tomas Aschan May 15 '11 at 17:23
6

introduce ViewModels which live in the WebUI project and expose IQueryables and the EF data context from the service to the WebUI project. Then I could directly project into those ViewModels.

The trouble with this is you soon run into problems using EF trying to 'flatten' models. I encountered something similar when I had a CommentViewModel class that looked like this:

public class CommentViewModel
{
    public string Content { get; set; }
    public string DateCreated { get; set; }
}

The following EF4 query projection to the CommentViewModel wouldn't work as the couldn't translate the ToString() method into SQL:

var comments = from c in DbSet where c.PostId == postId 
               select new CommentViewModel() 
               { 
                   Content = c.Content,
                   DateCreated = c.DateCreated.ToShortTimeString() 
               };

Using something like Automapper is a good choice, especially if you have a lot of conversions to make. However, you can also create your own converters that basically convert your domain model to your view model. In my case I created my own extension methods to convert my Comment domain model to my CommentViewModel like this:

public static class ViewModelConverters
{
    public static CommentViewModel ToCommentViewModel(this Comment comment)
    {
        return new CommentViewModel() 
        { 
            Content = comment.Content,
            DateCreated = comment.DateCreated.ToShortDateString() 
        };
    }

    public static IEnumerable<CommentViewModel> ToCommentViewModelList(this IEnumerable<Comment> comments)
    {
        List<CommentViewModel> commentModels = new List<CommentViewModel>(comments.Count());

        foreach (var c in comments)
        {
            commentModels.Add(c.ToCommentViewModel());
        }

        return commentModels;
    }
}

Basically what I do is perform a standard EF query to bring back a domain model and then use the extension methods to convert the results to a view model. For example, the following methods illustrate the usage:

public Comment GetComment(int commentId)
{
    return CommentRepository.GetById(commentId);
}

public CommentViewModel GetCommentViewModel(int commentId)
{
    return CommentRepository.GetById(commentId).ToCommentViewModel();
}

public IEnumerable<Comment> GetCommentsForPost(int postId)
{
    return CommentRepository.GetCommentsForPost(postId);
}

public IEnumerable<CommentViewModel> GetCommentViewModelsForPost(int postId)
{
    return CommentRepository.GetCommentsForPost(postId).ToCommentViewModelList();
}
Community
  • 1
  • 1
Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
  • Hm, but those extension methods would not solve your problem to directly project a DateTime to a string within an EF query, would it? The extension methods work only on a "full" Comment object. But if you want a projection you wouldn't use a `Comment` but some intermediate type. What are you doing then? You cannot use your `ViewModel` (because of the problem your described) and you don't want to load a full `Comment` object in the query. So, which type do you project into? Doesn't this result in a third type inbetween - finally a `DTO`? – Slauma May 13 '11 at 20:33
  • @Slauma I've updated my answer to show how I use them. Basically I return a domain model from EF and then convert it to its view model equivalent. – Dan Diplo May 14 '11 at 13:14
  • @Dan Diplo: I see, then you basically don't have a projection in your query anymore. You load the full object and then "project" in memory to your ViewModel. This might be OK in many cases if the domain object is small. But think of the case where you have a large domain object with let's say 50 properties but you only want to display 10 properties on the webpage. You would have a "query overhead" then and load 40 properties which you don't need. – Slauma May 14 '11 at 13:24
  • @Slauma As always in programming there is a balance to be struck between ease-of-development/maintainability and performance. In most cases the overhead of my method would be minor, but for a high-performance site then, yes, it would add a performance hit. But all abstractions have this problem. I guess you could then project to an intermediary type and convert from that, if you don't mind all the extra typing! However, if performance is really crucial then EF probably isn't the ORM to chose. See Dapper for that - http://code.google.com/p/dapper-dot-net/ – Dan Diplo May 14 '11 at 13:32
  • @Dan Diplo: I absolutely agree. I will probably mix: For small entities I query for the full object and create a ViewModel. For views as described (display only 10 out of 50 properties) I'll introduce an intermediate type. Thanks again for your comprehensive answer! – Slauma May 14 '11 at 13:55
  • I actually like this extension method mapping approach now. I've decided to do it on `IQueryable` which I expose from repos/services to the controller so that I have the projection in the database. I project then from Models directly into ViewModels (and vice versa) without intermediate DTOs. – Slauma May 21 '11 at 12:52
  • Leaving performance issues aside, for GET operations to the Service classes/layer (in your case "CustomerService") I think is important to return the actual entity (or list of entities), in its domain shape because otherwise if you return a view model directly you are implying that the service layer knows how the view looks like. What if you have other types of clients(non MVC)? So I would make the mapping in the controller after the service call. – Liviu Mandras Jun 28 '13 at 10:28
1

Talking about Models, ViewModels and DTOs is confusing, personally I don't like to use these terms. I prefer to talk about Domain Entities, Domain Services, Operation Input/Result (aka DTOs). All of these types live in the Domain layer. Operations is the behavior of Entities and Services. Unless you are building a pure CRUD application the presentation layer only deals with Input/Result types, not Entities. You don't need additional ViewModel types, these are the ViewModels (in other words, the Model of the View). The View is there to translate the Operation Results to HTML, but the same Result could be serialized as XML or JSON. What you use as ViewModel is part of the domain, not the presentation layer.

Max Toro
  • 28,282
  • 11
  • 76
  • 114
  • OK, that would be basically my option (2) (replacing my term "ViewModel" by your "Operation Input/Result"). – Slauma May 13 '11 at 20:24
  • 1
    A bit late here : ) Some would disagree here I think. A ViewModel can indeed contain presentation logic, thus it would seem making it part of the presentation layer. "The ViewModel encapsulates presentation logic and state" http://blogs.msdn.com/b/dphill/archive/2009/01/31/the-viewmodel-pattern.aspx – Adam Tuliper Oct 16 '11 at 05:09
  • @AdamTuliper Yes, you can have ViewModels in the presentation layer, I'm just saying it's not required, you can use the DTOs you get from the domain layer as ViewModel. – Max Toro Oct 16 '11 at 18:26