LMSPro.AutoMapper
10.4.0
dotnet add package LMSPro.AutoMapper --version 10.4.0
NuGet\Install-Package LMSPro.AutoMapper -Version 10.4.0
<PackageReference Include="LMSPro.AutoMapper" Version="10.4.0" />
<PackageVersion Include="LMSPro.AutoMapper" Version="10.4.0" />
<PackageReference Include="LMSPro.AutoMapper" />
paket add LMSPro.AutoMapper --version 10.4.0
#r "nuget: LMSPro.AutoMapper, 10.4.0"
#:package LMSPro.AutoMapper@10.4.0
#addin nuget:?package=LMSPro.AutoMapper&version=10.4.0
#tool nuget:?package=LMSPro.AutoMapper&version=10.4.0
LMS Framework AutoMapper Integration
LMS Framework AutoMapper Integration provides seamless integration between LMS Framework and AutoMapper, offering powerful object-to-object mapping capabilities with attribute-based configuration and automatic mapping discovery.
🚀 Key Features
🔄 Object Mapping
- Automatic Mapping: Convention-based mapping with minimal configuration
- Attribute-Based Configuration: Use attributes to define mapping relationships
- Bidirectional Mapping: Support for both source-to-destination and destination-to-source mapping
- Collection Mapping: Advanced collection mapping with AutoMapper.Collection
- Projection Support: LINQ projection for efficient database queries
🏷️ Mapping Attributes
[AutoMap]
: Bidirectional mapping between types[AutoMapFrom]
: Map from specified source types[AutoMapTo]
: Map to specified destination types[AutoMapKey]
: Define key properties for collection mapping
🔧 Advanced Features
- Module Integration: Seamless integration with LMS Module System
- IoC Container Integration: Automatic registration with Castle Windsor
- Custom Configurators: Extensible configuration system
- Multi-lingual Support: Built-in support for multi-lingual mapping
📦 Installation
Package Manager Console
Install-Package LMSPro.AutoMapper
.NET CLI
dotnet add package LMSPro.AutoMapper
PackageReference
<PackageReference Include="LMSPro.AutoMapper" Version="10.4.0" />
🏁 Quick Start
1. Add Module Dependency
using Lms.Modules;
using Lms.AutoMapper;
[DependsOn(typeof(LmsAutoMapperModule))]
public class MyApplicationModule : LmsModule
{
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(typeof(MyApplicationModule).Assembly);
}
}
2. Define Your Models
// Domain Entity
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public DateTime CreatedDate { get; set; }
public bool IsActive { get; set; }
}
// DTO with AutoMap attribute
[AutoMapFrom(typeof(User))]
public class UserDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public bool IsActive { get; set; }
}
// Input DTO
[AutoMapTo(typeof(User))]
public class CreateUserInput
{
public string Name { get; set; }
public string Email { get; set; }
}
3. Use Object Mapping
public class UserService
{
private readonly IObjectMapper _objectMapper;
public UserService(IObjectMapper objectMapper)
{
_objectMapper = objectMapper;
}
public UserDto GetUser(User user)
{
// Automatic mapping from User to UserDto
return _objectMapper.Map<UserDto>(user);
}
public User CreateUser(CreateUserInput input)
{
// Automatic mapping from CreateUserInput to User
var user = _objectMapper.Map<User>(input);
user.CreatedDate = DateTime.Now;
user.IsActive = true;
return user;
}
}
🔧 Advanced Usage
Bidirectional Mapping
// Both directions: User <-> UserDto
[AutoMap(typeof(User))]
public class UserDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public bool IsActive { get; set; }
}
Collection Mapping with Keys
[AutoMapFrom(typeof(User))]
public class UserDto
{
[AutoMapKey]
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public List<RoleDto> Roles { get; set; }
}
[AutoMapFrom(typeof(Role))]
public class RoleDto
{
[AutoMapKey]
public int Id { get; set; }
public string Name { get; set; }
}
LINQ Projection
public class UserService
{
private readonly IRepository<User> _userRepository;
private readonly IObjectMapper _objectMapper;
public UserService(IRepository<User> userRepository, IObjectMapper objectMapper)
{
_userRepository = userRepository;
_objectMapper = objectMapper;
}
public async Task<List<UserDto>> GetActiveUsersAsync()
{
// Efficient projection - only required fields are selected from database
return await _userRepository
.GetAll()
.Where(u => u.IsActive)
.ProjectTo<UserDto>(_objectMapper)
.ToListAsync();
}
}
Custom Mapping Configuration
[DependsOn(typeof(LmsAutoMapperModule))]
public class MyApplicationModule : LmsModule
{
public override void PreInitialize()
{
Configuration.Modules.LmsAutoMapper().Configurators.Add(CustomMappingConfiguration);
}
private static void CustomMappingConfiguration(IMapperConfigurationExpression configuration)
{
// Custom mapping configuration
configuration.CreateMap<User, UserDto>()
.ForMember(dest => dest.FullName, opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
.ForMember(dest => dest.Age, opt => opt.MapFrom(src => DateTime.Now.Year - src.BirthYear));
// Conditional mapping
configuration.CreateMap<User, UserSummaryDto>()
.ForMember(dest => dest.Email, opt => opt.Condition(src => !string.IsNullOrEmpty(src.Email)));
}
}
Multi-lingual Mapping
public class ProductDto
{
public int Id { get; set; }
[AutoMapKey]
public string Name { get; set; }
// Multi-lingual properties
public Dictionary<string, string> LocalizedNames { get; set; }
public Dictionary<string, string> LocalizedDescriptions { get; set; }
}
// Usage with multi-lingual context
public class ProductService
{
public ProductDto GetLocalizedProduct(Product product, string culture)
{
var context = new MultiLingualMapContext
{
Culture = culture
};
return _objectMapper.Map<ProductDto>(product, opts => opts.Items["Context"] = context);
}
}
🎯 Mapping Attributes Reference
[AutoMap]
Creates bidirectional mapping between the decorated class and target types.
[AutoMap(typeof(User), typeof(UserEntity))]
public class UserDto { }
[AutoMapFrom]
Creates mapping from target types to the decorated class.
[AutoMapFrom(typeof(User))]
[AutoMapFrom(MemberList.Destination, typeof(UserEntity))]
public class UserDto { }
[AutoMapTo]
Creates mapping from the decorated class to target types.
[AutoMapTo(typeof(User))]
[AutoMapTo(MemberList.Source, typeof(UserEntity))]
public class CreateUserInput { }
[AutoMapKey]
Defines key properties for collection mapping and equivalency.
public class UserDto
{
[AutoMapKey]
public int Id { get; set; }
}
🔧 Configuration Options
Module Configuration
public override void PreInitialize()
{
// Access AutoMapper configuration
var autoMapperConfig = Configuration.Modules.LmsAutoMapper();
// Add custom configurators
autoMapperConfig.Configurators.Add(config => {
// Your custom mapping configuration
});
// Configure validation
autoMapperConfig.UseStaticMapping = false;
autoMapperConfig.ValidateInlineMaps = true;
}
Performance Optimization
// Use static mapping for better performance
Configuration.Modules.LmsAutoMapper().UseStaticMapping = true;
// Disable validation in production
Configuration.Modules.LmsAutoMapper().ValidateInlineMaps = false;
// Configure compilation mode
Configuration.Modules.LmsAutoMapper().CompileMappings = true;
📚 Best Practices
1. Use Specific Attributes
// ✅ Good - Specific direction
[AutoMapFrom(typeof(User))]
public class UserDto { }
// ❌ Avoid - Bidirectional when not needed
[AutoMap(typeof(User))]
public class UserDto { }
2. Define Keys for Collections
// ✅ Good - Key defined for efficient collection mapping
public class UserDto
{
[AutoMapKey]
public int Id { get; set; }
}
3. Use Projection for Queries
// ✅ Good - Efficient database query
var users = await repository.GetAll()
.ProjectTo<UserDto>()
.ToListAsync();
// ❌ Avoid - Loads all data then maps
var users = (await repository.GetAllListAsync())
.Select(u => ObjectMapper.Map<UserDto>(u))
.ToList();
4. Custom Configuration for Complex Mappings
// ✅ Good - Custom configuration for complex scenarios
configuration.CreateMap<User, UserDto>()
.ForMember(dest => dest.FullName, opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
.ForMember(dest => dest.RoleNames, opt => opt.MapFrom(src => src.Roles.Select(r => r.Name)));
🔗 Related Packages
- Lms: Core LMS Framework
- Lms.EntityFrameworkCore: Entity Framework Core integration
- Lms.AspNetCore: ASP.NET Core integration
- AutoMapper: Core AutoMapper library
- AutoMapper.Collection: Collection mapping extensions
🤝 Contributing
We welcome all contributions! Please:
- Fork repository
- Create feature branch (
git checkout -b feature/AmazingFeature
) - Commit changes (
git commit -m 'Add some AmazingFeature'
) - Push to branch (
git push origin feature/AmazingFeature
) - Create Pull Request
📄 License
This project is distributed under MIT License. See LICENSE.md
file for more details.
🆘 Support
- Documentation: LMS Framework Docs
- GitHub Issues: Report bugs
- Stack Overflow: Tag
lms-framework
automapper
📊 Statistics
- Target Framework: .NET 9.0
- Dependencies: AutoMapper 14.0.0, AutoMapper.Collection 11.0.0
- Package Size: ~150KB
- AutoMapper Version: 14.0.0+
- Latest Version: 10.4.0
LMS Framework AutoMapper - Powerful object mapping made simple! 🚀
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- AutoMapper (>= 14.0.0)
- AutoMapper.Collection (>= 11.0.0)
- Lms (>= 10.4.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
10.4.0 | 132 | 8/18/2025 |