Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:49:30+00:00 2026-05-22T11:49:30+00:00

I have a web solution (in VS2010) with two sub-projects: Domain which holds the

  • 0

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)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:49:31+00:00Added an answer on May 22, 2026 at 11:49 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a solution that contains two projects. One project is an ASP.NET Web
I have several web application projects in one solution. When I start debugging one
I have a visual studio 2005 solution which has a web application and a
I have a VS solution that contains 6 library projects and 1 web folder
I have a vb.net solution with a web reference to a webservice. Now I
I have a Windows forms project and a Web Service project in my solution,
All, I currently have my solution comprising of 2 Class librarys and a Web
I'm working on a solution with multiple projects (class libraries, interop, web application, etc)
I have a VS2003 solution with 21 ASP.NET 1.1 projects in it. My goal
i have a solution with a web application that i want to creat a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.