locked
mapping domain entities to dto's RRS feed

  • Question

  • User65893105 posted

    I have a complex dto that consists of records from 4 tables

    public class AggregateDto
    {
       public OrganisationDto Organisation { get; set; }
       public List<Meter.MeterDto> Meters { get; set; }
       public List<Site.SiteDto> Sites { get; set; }
       public List<Contracts.ContractDto> Contracts { get; set; }
    }

    Im then creating a linq to sql query to return data for a specified organisation using its id, so there will be 1 organisation and 0 or more meters, sites and contracts.  I cant figure out how to 'map' the list domain object to its corresponding list dto, heres the linq code

    var query = await (from organisation in DbContext.Organisations
      where organisation.ID == request.Id
      join site in DbContext.Sites on organisation.Orgid equals site.Orgid
      into sites_joined
      from site_joined in sites_joined.DefaultIfEmpty()
      from meter in DbContext.Meters.Where(var_meter => site_joined == null ? false : var_meter.SiteLocationID == site_joined.SiteLocationID).DefaultIfEmpty()
      join contract in DbContext.Contracts on organisation.Orgid equals contract.orgid
      into contracts_joined
      from contract_joined in contracts_joined.DefaultIfEmpty()
      select new AggregateDto
      {
         Organisation = new OrganisationDto{ ID = organisation.ID, OrganisationName = organisation.OrganisationName },
         Sites = --what goes here ?? ,
         Meters = --and here,
         Contracts = -- and here
      }).FirstOrDefaultAsync();

    the organisation dto was easy enough as its just a single entity, but sites, meters and contracts  are lists of dto objects, how do i map the domain objects in this code above ?  Am I even going about this the right way ?

    Wednesday, March 8, 2017 3:52 PM

All replies

  • User-821857111 posted

    {
         Organisation = new OrganisationDto{ ID = organisation.ID, OrganisationName = organisation.OrganisationName },
         Sites = --what goes here ?? ,

    Sites = context.Sites.Select(s => new SiteDto {...})....ToListAsync(),
    Meters = context.Meters.Select(m => new MeterDto {.....})...ToListAsync()

    etc.

    Basically, you can view them as subqueries.

    Thursday, March 9, 2017 7:53 AM
  • User65893105 posted

    thanks for this, I managed to figure it out myself eventually.

    because im using left outer joins I ended up with this

    Sites = sites_joined.Select(s => new Dto.Site.SiteDto { Address1 = s.Address1, Address2 = s.Address2, Address3 = s.Address3, City = s.City, County = s.County, ID = s.ID, Postcode = s.Postcode, SiteLocation = s.SiteLocation, SiteLocationName = s.SiteLocationName }).ToListAsync(),

    but now Im getting another error

    IEnumerable<SiteDo> does not contain a definition for ToListAsync requires a receiver of type IQueryable linq to sql

    I changed the underlying type in my dto to IQueryable,(Sites) but sites_joined is IEnumerable, How can I make sites_joined as IQueryable ?

    Thursday, March 9, 2017 9:30 AM
  • User-832373396 posted

    Hi misuk11,

    Please add using System.Data.Entity (under the Entity Framework package) to get the QueryableExtensions.

    And to convert it use code below:

     IEnumerable<BookMaster> a=...  ;
    IQueryable<BookMaster> c= a.AsQueryable();

    Best regards,

    Jolie

    Thursday, March 9, 2017 10:20 AM
  • User65893105 posted

    sorry but I dont understand what you mean, how does that relate to my code ?  Ive tried this but it still doesnt work (I already had system.data.entity referenced)

    Sites = sites_joined.Select(s => new Dto.Site.SiteDto { Address1 = s.Address1, Address2 = s.Address2, Address3 = s.Address3, City = s.City, County = s.County, ID = s.ID, Postcode = s.Postcode, LocationID = s.ocationID, LocationName = s.LocationName }).AsQueryable().ToListAsync(),

    Thursday, March 9, 2017 2:11 PM
  • User-832373396 posted

    Hi misuk11,

    The easiest way I suggest you could to use ,is use update EF.

    because you are using EF, so why not to use the way which the ef provided  by itself  ,
    So please refer to my steps below to use View in mvc (For database first):

    1: Click .edmx file in vs
    2: Right click and choose the 'Update Model from Database'
    3: Select your View and click ok 
    4: Ctrl + S ,save it

    Then get all value like code below:

     context.YourView.ToList

    And model named :

    YourView_Result

    Best regards,
    Jolie

    Monday, March 13, 2017 7:51 AM