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 8242923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:20:05+00:00 2026-06-07T21:20:05+00:00

My repository layer used to return a ViewModel directly e.g. public IEnumerable<CommentViewModel> GetComments() {

  • 0

My repository layer used to return a ViewModel directly e.g.

public IEnumerable<CommentViewModel> GetComments()
{
   return from c in context.Comments
                 select new CommentViewModel
                 {
                    FirstName = c.FirstName,
                    Comment = c.Comment
                 };
}

I then read that the repository should not return view models so I changed it to:

public IEnumerable<Comment> GetComments()
{
   return from c in context.Comments;
}

In the service layer I then changed:

public IEnumerable<CommentViewModel> GetComments(int postId)
{
   return _repository.GetComments();
}

To:

public IEnumerable<CommentViewModel> GetComments()
{
   var comments = _repository.GetComments();

   return Mapper.Map<IEnumerable<Comment>, IEnumerable<CommentViewModel>>(comments);
}

The problem is the queries used to take 7ms and now take 85ms!

Where am I going wrong?

The query generated used to be (top 5 and order by is done in controller):

SELECT TOP (5) [t0].[FirstName], [t0].[Comment]
FROM [dbo].[Comment] AS [t0]
ORDER BY [t0].[CreateDate] DESC 

It’s now returning all columns:

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[FirstName] AS [FirstName], 
[Extent1].[LastName] AS [LastName], 
[Extent1].[DatePosted] AS [DatePosted], 
[Extent1].[Comment] AS [Comment],
[Extent1].[IPAddress] AS [IPAddress]
FROM [dbo].[Comment] AS [Extent1]
  • 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-06-07T21:20:06+00:00Added an answer on June 7, 2026 at 9:20 pm

    You need to shape the query results in your repository layer otherwise AutoMapper will pull all the records from database to map to view models.

    public IEnumerable<Comment> GetComments(int skip, int count)
    {
       return context.Comments.OrderByDescending(c => c.CreateDate).Skip(skip).Take(count);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Repository pattern is used to abstract from particular database and object-relation-mapping technology(like EF) used.
I have a service layer that retrieves a DTO from a repository. Depending on
I am busy with a new project and am working on my repository layer.
So for namespacing my repository layer, it's easy. I do something like this: The
In my repository layer I am mapping Linq-to-Sql objects to my application domain objects.
I am using a repository design with web applications (repository (data layer) exposing model
I have this sort of format asp.net MVC View -> Service Layer -> Repository.
I have the standard repository pattern with a service layer and I need to
I am using a standard repository pattern with a service layer which calls the
@Service @Repository @Transactional public class VideoService { @PersistenceContext EntityManager entityManager; public void save(Video video)

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.