Skip to content

Descriptions and Mappings Storage

Dima Bezzubenkov edited this page Sep 4, 2021 · 3 revisions

Before you can start using mappings you need to describe and to register them.

Mappings Descriptions

MappingsList class is used to describe mappings. You can use its Add<TFrom, TTo> method to describe new mapping:

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
});

This mapping describes mapping from Address entity to AddressModel model.

Register Mappings

All mapping descriptions should be located inside of Storage classes. Storage is a class that implements IMappingsStorage interface. This interface is very simple and has only one method Setup:

public interface IMappingsStorage
{
    void Setup(IMappingsList mappings);
}

You can have whatever you want number of IMappingsStorage implementations. Theoretically you can even have only one IMappingsStorage implementation with hundreds of mappings in it, but it will be really difficult to navigate in such a big class. The rule of thumb is to separate your mappings by some logical criteria. In most of the cases you will have more than one mapping for every Entity. So it's a good idea to have a separate Storage for every Entity you have.

    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
            });

            mappings.Add<Address, AddressSummaryModel>(x => new AddressSummaryModel
            {
                Id = x.Id,
                Address = x.BuildingNumber + " " + x.Street + ", " + x.City + ", " + x.State
            });
        }
    }

    public class AppartmentMappings: IMappingsStorage
    {
        public void Setup(IMappingsList mappings)
        {
            mappings.Add<Appartment, AppartmentModel>(args => 
            {
                return x => new AppartmentModel
                {
                    Id = x.Id,
                    Badrooms = x.Badrooms,
                    Bathrooms = x.Bathrooms,
                    Floor = x.Floor,
                    IsLodge = x.IsLodge,
                    Number = x.Number,
                    Size = x.Size.ToString(),
                    Building = new BuildingModel
                    {
                        Id = x.Building.Id,
                        Year = x.Building.Year,
                        Floors = x.Building.Floors,
                        IsLaundry = x.Building.IsLaundry,
                        IsParking = x.Building.IsParking
                    }
                };
            });
        }
    }

You can put Storage classes in any place in your solution. They will be found during registration stage with reflection. It's the only place in the project where reflection is used. But because registration stage is happening during application start it doesn't add any overhead to the application in runtime.

Clone this wiki locally