2

I've been applying for a while DTOs in the Repository (MVC 5 + EF 6.1) because they are the only way for me to extract partial data (selecting certain columns) from the Database. To my surprise, from some recent research, this is bad

The only solution I could think of, is using IQueryable (and let's not start with this) (Plus, it's very probable that I'll get the context disposed before mapping it to a DTO) :

The question would be, how can I retrieve partial data from the database without using IQueryable?

This is how I've been doing it without problems:

  public async Task<List<ParticipantIdsOnly>> GetRegisteredParticipantsIdsOnlyAsync(int tournamentId)
        {

            return await _dbRepositories.TournamentRepository.Where(x => x.TournamentId == tournamentId)
            .SelectMany(y => y.Participants.Where(x => x.Status == ParticipantStatus.Active)
                            .Select(x => new ParticipantIdsOnly { Id = x.Id, ProviderPlayerId = x.ProviderPlayerId }))
            .ToListAsync();

        }

BTW, if I try "newing" the Entity using a select, I get a LINQ to Entities error.

Community
  • 1
  • 1
Jose A
  • 7,443
  • 8
  • 49
  • 77
  • what is the error? – Sampath Oct 01 '16 at 14:51
  • @Sampath: There's no error :) It's working beautifully. It's a conceptual problem. I'm moving my application to a Domain Driven perspective, and I've been investigating for the past week nonstop, and I've come upon this situation. But it doesn't seem that anybody addresses it. – Jose A Oct 01 '16 at 14:58
  • So you have some methods in your repository which return entities with partial data (not all columns). What is a problem with that exactly? – Evk Oct 01 '16 at 15:02
  • @Evk: That there's a belief around the web, that I must not do that. That Repositories must not return DTOs, and I don't know what to do, if that's bad or wrong. But I don't know other way of actually achieving it. – Jose A Oct 01 '16 at 15:04
  • 1
    If you just use that entity with partial columns in the same way you use your model entities, why is it DTO? You don't use it to map or transfer anything. Well in some vague sense it can be called DTO, but not sure it's really that important. – Evk Oct 01 '16 at 15:06
  • 1
    We use DTOs (or ViewModels or Resource Models) all the time. Common pattern. https://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-5 – Steve Greene Oct 01 '16 at 15:11
  • Thanks guys. This question should be closed then. Do you know if there's a certain term for the thing I'm doing? I thought **that** was a DTO – Jose A Oct 01 '16 at 15:13
  • 1
    Also, suppose you have stored procedure mapped in your EF model. This stored procedure returns some results, and those results are represented by some object, like your ParticipantIdsOnly. In this case you won't consider it DTO I suppose, but that's exactly the same situation you have now. – Evk Oct 01 '16 at 15:14
  • Yes. This is a store procedure... I thought these were called DTOs too. Apparently it's me who has been mixed up. Is it possible for you to tell me the term of these "store procedure objects" ? – Jose A Oct 01 '16 at 15:16
  • 1
    Well as I remember, when you map stored procedure in EF, it can return a list of scalars (like ints), complex types (your case) or entities. So I'd say in EF world that is named complex type. I don't really see much difference between data model objects and those complex types - they are all part of the model. – Evk Oct 01 '16 at 15:20

0 Answers0