0

I have a very large entity with a few hundred properties. I have a repository that I use to select this all of the entities and returns an IEnumerable of the entity.

In my controller I then use automapper to map to an index ViewModel of this entity and it only uses two of the properties of the entity in the ViewModel. It takes quite a long time to return compared to a select of the two properties. It would seem that it is selecting all of properties of the entity and then just using two of them.

What would be the recommended way of doing this. Do I need to create the view model in the repository?

Graeme

GraemeMiller
  • 11,973
  • 8
  • 57
  • 111

1 Answers1

2

You can pass a DTO or different model.

public class LargeEntityDto
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

Then in your repository

public IEnumerable<LargeEntityDto> GetLargeEntityDtos()
{
   return context.LargeEntities
      .Select(e => new LargeEntityDto { Foo = e.Foo, Bar = e.Bar});
}
Eranga
  • 32,181
  • 5
  • 97
  • 96
  • Yeah that was my thinking. I didn't really want to have to add additional DTOs etc. Just somehow hoped I could cause EF to return the same entity but with maybe some of the other fields not selected out of the DB etc. – GraemeMiller Oct 14 '11 at 02:03
  • +1: This is probably the easiest way. You could also set up an entity in your EF context that just maps to have those few properties you really need. – StriplingWarrior Oct 14 '11 at 02:05