Author Top Nguyen
- Extend for AutoMapper, save your time to register profiles to AutoMapper by Scanner.
- Support more utilities for mapping.
.IgnoreAllNonExisting()
: Ignore all non existing between 2 classes..MapTo<T>()
: Create new instance ofT
and map data from source..MapTo()
: Map data from source to existing destination instance..QueryTo<T>()
: SupportIQueryable
map data to destinationT
type, useful for query data in Entity Framework Core.
- Package Manager
PM> Install-Package Elect.Mapper.AutoMapper
- .NET CLI
dotnet add package Elect.Mapper.AutoMapper
-
Startup.cs
: addScanner
- Auto register auto mapper profiles.public void ConfigureServices(IServiceCollection services) { // Add Auto Mapper Services and Auto Register Auto Mapper Profiles services.AddElectAutoMaper(); }
-
Auto Mapper Profile: Create class and inherit
AutoMapper.Profile
public class UserProfile : Profile { public UserProfile() { CreateMap<UserEntity, UserModel>() .IgnoreAllNonExisting() .ForMember(d => d.FullName, o => o.MapFrom(s => s.Profile.FullName)); } }
-
MapTo<T>()
andMapTo()
// Map from UserEntity to UserModel var userEntity = new UserEntity{Id = 1}; // Create new instance of UserModel with data from UserEntity. var userModel = userEntity.MapTo<UserModel>(); var userEntity2 = new UserEntity{Id = 2}; // Update userModel by userEntity data. userEntity2.MapTo(userModel);
-
QueryTo<T>()
to map data fromIQueryable
- useful for query data form Entity Framework Core.var query = _userRepo.Get(x => x.DeletedTime == null); // Work same way with ".Select(x => new UserModel{...})" to select alias from Database. var listUserModel = query.QueryTo<UserModel>().ToList();
Elect.Mapper.AutoMapper is licensed under the MIT License.