Problem with multiple mappers #278
Answered
by
alirezanet
sh-sharifjonov
asked this question in
Q&A
-
namespace Application.Admin.Mappers.ProductApplications.GetProductApplications;
public class GetProductApplicationsGridifyMapper : GridifyMapper<ProductApplication>
{
public GetProductApplicationsGridifyMapper(IAdminAuthenticatedService authenticatedService)
{
AddMap("Id", o => o.Id);
AddMap("ProductName", o => o.Product!.Name);
AddMap("ProductApplicationType", o => o.ProductApplicationType);
AddMap("AdminUserId", o => authenticatedService.AdminUserId);
}
} namespace Application.Client.Mappers.ProductApplications.GetProductApplications;
public class GetProductApplicationsGridifyMapper : GridifyMapper<ProductApplication>
{
public GetProductApplicationsGridifyMapper(IClientAuthenticatedService authenticatedService)
{
AddMap("Id", o => o.Id);
AddMap("ProductName", o => o.Product!.Name);
AddMap("Description", o => o.Product!.Description);
AddMap("ClientUserId", o => authenticatedService.ClientUserId);
}
} namespace Presentation.Controllers;
public class AdminController : Controller
{
private readonly IDbContext _dbContext;
private readonly IGridifyMapper<ProductApplication> _mapper;
public AdminController(IDbContext _dbContext, IGridifyMapper<ProductApplication> mapper)
{
_mapper = mapper;
_dbContext = _dbContext;
}
public IActionResult Index(GridifyQuery request)
{
var result = _dbContext.ProductApplications.AsNoTracking().ApplyFilteringAndOrdering(request, _gridifyMapper);
return Ok(result);
}
} During the process of obtaining an instance of IGridifyMapper, two mapper objects will be available: one for administrative access (admin) and one for client access (client). It is necessary to ensure that when calling the function, only the mapper object is available for administrative access. How do we do this? |
Beta Was this translation helpful? Give feedback.
Answered by
alirezanet
Jul 4, 2025
Replies: 1 comment 1 reply
-
Hey @sh-sharifjonov, // 1) Marker interface
public interface IAdminProductApplicationsMapper
: IGridifyMapper<ProductApplication> {}
// 2) Mapper implementation
public class GetAdminProductApplicationsGridifyMapper
: GridifyMapper<ProductApplication>, IAdminProductApplicationsMapper
{
public GetAdminProductApplicationsGridifyMapper(IAdminAuthenticatedService svc)
{
AddMap("Id", o => o.Id);
AddMap("ProductName", o => o.Product!.Name);
AddMap("ProductApplicationType", o => o.ProductApplicationType);
AddMap("AdminUserId", o => svc.AdminUserId);
}
}
// 3) DI registration
services
.AddTransient<IAdminProductApplicationsMapper, GetAdminProductApplicationsGridifyMapper>()
.AddTransient<IClientProductApplicationsMapper, GetClientProductApplicationsGridifyMapper>();
// 4) Inject in AdminController
public class AdminController : ControllerBase
{
private readonly IAdminProductApplicationsMapper _mapper;
public AdminController(
IDbContext db,
IAdminProductApplicationsMapper mapper)
{
_dbContext = db;
_mapper = mapper;
}
public IActionResult Index(GridifyQuery q)
{
var result = _dbContext.ProductApplications
.AsNoTracking()
.ApplyFilteringAndOrdering(q, _mapper);
return Ok(result);
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
sh-sharifjonov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @sh-sharifjonov,
I think you have to do it manually, the trick is to give your admin mapper its own interface so your AdminController can only ever see that one. For example: