-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
Source/BSN.Commons.AutoMapper/BSN.Commons.AutoMapper.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AutoMapper" Version="13.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\BSN.Commons.PresentationInfrastructure\BSN.Commons.PresentationInfrastructure.csproj" /> | ||
<ProjectReference Include="..\BSN.Commons\BSN.Commons.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using AutoMapper; | ||
using BSN.Commons.Responses; | ||
|
||
namespace BSN.Commons.AutoMapper | ||
{ | ||
public class CommonMapperProfile : Profile | ||
{ | ||
public CommonMapperProfile() | ||
{ | ||
CreateMap(typeof(PagedEntityCollection<>), typeof(PaginationMetadata)).ConvertUsing(typeof(PagedEntityCollectionToMetaDataConverter<>)); | ||
|
||
CreateMap(typeof(IEnumerable<>), typeof(CollectionViewModel<>)).ConvertUsing(typeof(GenericIEnumerableToCollectionViewModelConverter<,>)); | ||
} | ||
|
||
private class PagedEntityCollectionToMetaDataConverter<TDomain> : ITypeConverter<PagedEntityCollection<TDomain>, PaginationMetadata> | ||
{ | ||
public PaginationMetadata Convert(PagedEntityCollection<TDomain> source, PaginationMetadata destination, ResolutionContext context) | ||
{ | ||
return new PaginationMetadata() | ||
{ | ||
Page = source.CurrentPage, | ||
PageCount = source.PageSize, | ||
PageSize = source.PageSize, | ||
RecordCount = source.RecordCount | ||
}; | ||
} | ||
} | ||
|
||
private class GenericIEnumerableToCollectionViewModelConverter<TDomain, TViewModel> : ITypeConverter<IEnumerable<TDomain>, CollectionViewModel<TViewModel>> | ||
{ | ||
public CollectionViewModel<TViewModel> Convert(IEnumerable<TDomain> source, CollectionViewModel<TViewModel> destination, ResolutionContext context) | ||
{ | ||
return new CollectionViewModel<TViewModel> | ||
{ | ||
Items = context.Mapper.Map<IEnumerable<TViewModel>>(source) | ||
}; | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Source/BSN.Commons.AutoMapper/Extensions/IServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using AutoMapper; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace BSN.Commons.AutoMapper.Extensions | ||
{ | ||
public static class IServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddAutoMapper(this IServiceCollection services, Action<IMapperConfigurationExpression> configure) | ||
{ | ||
var mappingConfig = new MapperConfiguration(config => | ||
{ | ||
MapperConfigurationExpression mapperConfigurationExpression = new MapperConfigurationExpression(); | ||
configure(mapperConfigurationExpression); | ||
configure(config); | ||
config.AddProfile(new CommonMapperProfile()); | ||
}); | ||
|
||
IMapper mapper = mappingConfig.CreateMapper(); | ||
|
||
services.AddSingleton(mapper); | ||
|
||
return services; | ||
} | ||
} | ||
} |