项目作者: arttonoyan

项目描述 :
A convention object to object or DataReader to object mapper in .NET Core 2.0.
高级语言: C#
项目地址: git://github.com/arttonoyan/Mapper.git
创建时间: 2017-09-11T07:50:08Z
项目社区:https://github.com/arttonoyan/Mapper

开源协议:MIT License

下载


New Version: MupStruct

Artnix.MapperFramework

Build status
GitHub
Nuget
Nuget

How do I get started?

First, configure MapperFramework to know what types you want to map in the startup of your application:

  1. Mapper.MapConfiguration(cfg =>
  2. {
  3. cfg.CreateMap<StudentModel, Dto.StudentModel()
  4. .Property(wm => wm.CityName, m => m.CityModel == null ? null : m.CityModel.Name)
  5. // You can use "IfNotNull" extensions.
  6. //.Property(wm => wm.CityName, m => m.CityModel.IfNotNull(p => p.Name))
  7. .Property(wm => wm.Fullname, m => $"{m.Family} {m.Name}");
  8. });

You can use “Ignore” functions:

  1. Mapper.MapConfiguration(cfg =>
  2. {
  3. cfg.CreateMap<StudentModel, Dto.StudentModel>()
  4. .Ignore(nameof(Mock.MapModels.Base.StudentModelMock.CityName))
  5. // Or You can use this style.
  6. .Ignore(m => m.CityName)
  7. });

Then in your application code, execute the mappings:

  1. var dtoModel = Mapper.Convert<Dto.StudentModel>(student);

Datareader Configuration

  1. Mapper.MapConfiguration(cfg =>
  2. {
  3. cfg.CreateDataReaderMap<Student>()
  4. .Ignore(p => p.Birthday)
  5. //// Or
  6. //.Ignore(nameof(Student.Birthday))
  7. .Property(p => p.FirstName, "Name")
  8. .Property(p => p.LastName, "Family");
  9. });

If you have standart SQL Style Guide (first name becomes first_name)
you can use “UseStandardCodeStyleForMembers()” function.

  1. Mapper.MapConfiguration(cfg =>
  2. {
  3. cfg.CreateDataReaderMap<Student>()
  4. .UseStandardCodeStyleForMembers();
  5. });

Or you can do it manually

  1. Mapper.MapConfiguration(cfg =>
  2. {
  3. cfg.CreateDataReaderMap<Student>()
  4. .Property(p => p.FirstName, "first_name")
  5. .Property(p => p.LastName, "last_name");
  6. });