jeudi 15 février 2018

How to refactor 2 implementations of interface that vary slightly

I have 2 classes that implement an interface. As you can see the implementation of interface method Evaluate is very similar in both the classes but varies sliggly. How to factor out the duplicate code here

public class Policy1 : IPolicy
{
    private readonly IPolicy _innerPolicy1;
    private readonly IPolicy _innerPolicy2;

    public Task Evaluate(Input input)
    {
        _innerPolicy1.Evaluate(input);
        _innerPolicy2.Evaluate(input);

        return Task.FromResult(input);
    }
}

public class Policy2 : IPolicy
{
    private readonly IPolicy _innerPolicy1;
    private readonly IPolicy _innerPolicy2;
    private readonly IPermissionService _permissionService;

    public Task Evaluate(Input input)
    {
        _innerPolicy1.Evaluate(input);
        if (HasInnerPolicy2Permission(input))
        {
            _innerPolicy2.Evaluate(input);
        }

        return Task.FromResult(input);
    }

    private bool HasInnerPolicy2Permission(Input input)
    {
        return _permissionService.GetInnerPolicy2Permission(input);
    }
}

Aucun commentaire:

Enregistrer un commentaire