dimanche 18 février 2018

How to communicate two Service Layers

I am using a ServiceLayer Design pattern in my application.I am stuck at a point where one service layers seems to depend on other. Below is my ProductService and it is trying to save a product. Inside my validation logic I need to retrieve the UserPermission. This permission has information about list of Products that User can save. I have a UserGroupService to deal with retrieving the permission. In such case, should I have the reference of UserGroupService inside the ProductService. If yes, ProductService will have to pass all the dependencies to initialize the UserGroupService. Or should I create a utility class or method to just retrieve the UserPermission without using the UserGroupService. It would be a great help to get good feedbacks.

public class ProductService : IProductService
{
    private ModelStateDictionary _modelState;
    private IProductRepository _repository;

    public ProductService(ModelStateDictionary modelState, IProductRepository repository)
    {
        _modelState = modelState;
        _repository = repository;
    }

    protected bool ValidateProduct(Product productToValidate)
    {
        // validation logic.
        // I need to check if the logged in User has permission to the product. 
        // The list of permitted products is stored in the database. 
    }

    public bool CreateProduct(Product productToCreate)
    {
        if (!ValidateProduct(productToCreate))
            return false;
        try
        {
            _repository.CreateProduct(productToCreate);
        }
        catch
        {
            return false;
        }
        return true;
    }
}

public interface IProductService
{
    bool CreateProduct(Product productToCreate);
}

public class UserGroupService : IUserGroupService
{
    private ModelStateDictionary _modelState;
    private IUserGroupRepository _repository;

    public UserGroupService(ModelStateDictionary modelState, IUserGroupRepository repository)
    {
        _modelState = modelState;
        _repository = repository;
    }

    public IList GetUserPermission(UserGroup usergroup)
    {
        try
        {
            return _repository.GerUserPermission(usergroup);
        }
        catch
        {
            return List();
        }
    }
}
public interface IUserGroupService
{
    IList GetUserPermission(UserGroup usergroup);
}

Aucun commentaire:

Enregistrer un commentaire