Skip to content

Quick Start

Dima Bezzubenkov edited this page Sep 26, 2021 · 16 revisions

There are a few simple steps to start using QueryMappings:

  • Describe mappings by implementing IMappingsStorage interface
  • Register mappings using MappingsConfiguration class
  • Apply mappings using AsQuery extension method

1. Describe mappings by implementing IMappingsStorage interface.

All mappings should be described in Setup method of IMappingsStorage implementation. Just implement Setup method and use MappingsList class to add new mappings. Every mapping is just a simple Expression that describes how we want to map one model to another:

    public class AddressMappings : IMappingsStorage
    {
        public void Setup(IMappingsList mappings)
        {
            mappings.Add<Address, AddressModel>(x => new AddressModel
            {
                Id = x.Id,
                BuildingNumber = x.BuildingNumber,
                City = x.City,
                State = x.State,
                Country = (Countries)x.Country,
                Street = x.Street,
                ZipCode = x.ZipCode
            });
        }
    }

2. Register mappings using MappingsConfiguration class

Just pass list of assemblies where your IMappingStorage implementations are located into Register method of MappingsConfiguration class:

MappingsConfiguration.Register(typeof(AddressMappings).Assembly);

3. Inject IMappingsService into constructor of you class

public MyService(IMappingService qmService) 
{
    _qmService = qmService;
}

4. Apply mappings using AsQuery method

IQueryable<Address> query = ...
List<AddressModel> models = _qmService.AsQuery<Address, AddressModel>(query).ToList();