Skip to content

Commit 5b18304

Browse files
committed
added automapperadapter implementation of iresourcemapper using automapper injection
1 parent 49127e5 commit 5b18304

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using AutoMapper;
2+
using JsonApiDotNetCore.Models;
3+
4+
namespace ResourceEntitySeparationExample.Models
5+
{
6+
public class AutoMapperAdapter : IResourceMapper
7+
{
8+
private readonly IMapper _mapper;
9+
10+
public AutoMapperAdapter(IMapper mapper)
11+
{
12+
_mapper = mapper;
13+
}
14+
15+
public TDestination Map<TDestination>(object source)
16+
{
17+
return _mapper.Map<TDestination>(source);
18+
}
19+
20+
public TDestination Map<TSource, TDestination>(TSource source)
21+
{
22+
return _mapper.Map<TSource, TDestination>(source);
23+
}
24+
}
25+
}

src/Examples/ResourceEntitySeparationExample/ResourceEntitySeparationExample.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@
99
<ProjectReference Include="..\JsonApiDotNetCoreExample\JsonApiDotNetCoreExample.csproj" />
1010
</ItemGroup>
1111

12+
<ItemGroup>
13+
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="4.0.1" />
14+
</ItemGroup>
15+
1216
</Project>

src/Examples/ResourceEntitySeparationExample/Startup.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using AutoMapper;
33
using JsonApiDotNetCore.Data;
44
using JsonApiDotNetCore.Extensions;
5+
using JsonApiDotNetCore.Models;
56
using JsonApiDotNetCore.Services;
67
using JsonApiDotNetCoreExample.Data;
78
using JsonApiDotNetCoreExample.Models;
@@ -13,6 +14,8 @@
1314
using Microsoft.Extensions.Configuration;
1415
using Microsoft.Extensions.DependencyInjection;
1516
using Microsoft.Extensions.Logging;
17+
using ResourceEntitySeparationExample.Models;
18+
using ResourceEntitySeparationExample.Profiles;
1619

1720
namespace ResourceEntitySeparationExample
1821
{
@@ -55,8 +58,9 @@ public virtual IServiceProvider ConfigureServices(IServiceCollection services)
5558
});
5659
}, mvcBuilder);
5760

58-
// inject automapper and mapping resources
5961
services.AddAutoMapper();
62+
services.AddScoped<IResourceMapper, AutoMapperAdapter>();
63+
6064
services.AddScoped<IResourceService<CourseResource, int>, DefaultResourceService<CourseResource, CourseEntity, int>>();
6165
services.AddScoped<IResourceService<DepartmentResource, int>, DefaultResourceService<DepartmentResource, DepartmentEntity, int>>();
6266
services.AddScoped<IResourceService<StudentResource, int>, DefaultResourceService<StudentResource, StudentEntity, int>>();

0 commit comments

Comments
 (0)