Home > database >  DDD pass filters through multiple layers
DDD pass filters through multiple layers

Time:11-19

In my onion architecture I've my PresentationLayer who contains a class named LogRabbitMQFilters with differents properties to filter search.

LogRabbitMQFilters

I pass LogRabbitMQFilters in ApplicationLayer by mapper :

    public RabbitMQController(IELKService iELKService, IMapper mapper)
    {
        _iELKService = iELKService;
        _mapper = mapper;
    }

    public async Task<IActionResult> Index(int? id, LogRabbitMQFilters filters)
    {
        var filtersMapped = _mapper.Map<LogRabbitMQFilters>(filters);

        var response = await _iELKService.GetLog(filtersMapped);

        /*some code.....*/
    }

In ApplicationLayer I map my logRabbitMQFilters to RabbitMQFilters which is declare in Persistance layer and I call my repository like this :

    public ELKService(IELKRepository iELKRepository, IMapper mapper)
    {
        _iELKRepository = iELKRepository;
        _mapper = mapper;
    }

    public async Task<LogResult> GetLog(LogRabbitMQFilters  logRabbitMQFilters)
    {
        var filterMapped = _mapper.Map<RabbitMQFilters>(logRabbitMQFilters);

        return await _iELKRepository.GetLogs(filterMapped); 
    }

It's best approch to do this ? Is there another way to pass my filters class to the repository ? I thought of specification pattern but I don't now if it's a good solution.

CodePudding user response:

In regards of complexity it always great to break up the application according to its responsibilities or concerns.

Considering your code, it seems you are following the industry specified standard. For better clarity I am also adding here a sample code snippet of a standard project architecture.

Controller:

[Route("api/User")]
[ApiController]
public class UserController : ControllerBase
{
    private readonly IUserService _userService;

    public UserController(IUserService userService)
    {
        _userService = userService;
    }
    [HttpGet]
    [Authorize]
    [Route("users")]
    public async Task<ActionResult> Users()
    {
        var users = await _userService.GetAllUsers();
        return Ok(new ResponseViewModel { output = "success", msg = "request successful", returnvalue = users });
    }
    
}

Service Interface:

public interface IUserService
    {
        Task<List<UserViewModel>> GetAllUsers();
    }

Service Implementation:

public  class UserService : IUserService
    {
        private readonly IUserRepository _userRepository;

        public UserService(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        public async Task<List<UserViewModel>> GetAllUsers()
        {
            return await _userRepository.GetAllUsers();
        }
    }

Repository Interface:

public interface IUserRepository
    {
        Task<List<UserViewModel>> GetAllUsers();
    }

Repository Implementation:

public class UserRepository : IUserRepository
    {
        private readonly AppDbContext _dbContext;
        private readonly IMapper _mapper;
        public UserRepository(AppDbContext dbContext, IMapper mapper)
        {
            _dbContext = dbContext;
            _mapper = mapper;

        }

        public async Task<List<UserViewModel>> GetAllUsers()
        {
            var users = await _dbContext.Users.ToListAsync();
            var userViewModel = _mapper.Map<List<UserViewModel>>(users);
            return userViewModel;

        }
    }

Model:

public class UserViewModel
    {
        public long user_id { get; set; }
        public string full_name { get; set; }
    }

Note: Hope above steps guided you accordingly. Additionally, you could also have a look our official document for more information regarding industry practices.

  • Related