Business rule check inside DAL service At now I have such code, creating or updating some entity by dto items. 

public void Upsert(List&lt;Object&gt; items)
{
    var existings = _db.Get().Where().ToList();
    foreach (var item in items)
    {
        var existing = existings.FirstOrDefault(x =&gt; x.Id == item.SomeId);
        if (existing == null)
        {
            if (item.Property != 645)
            {
                throw new EntityNotFoundException();
            }

            // create new
        }
        else
        {
            // update existing
        }
    }
    _db.Save();
}


I want to move if (item.Property logic out of this layer. I cannot add some method to Object, it is simply dto. I also cannot subclass UpsertService.

What I can do is to pass Action&lt;Object&gt; checkWhenNotFound to Upsert method. Disadvantage - I need to write all those mocking It.IsAny&lt;Action&lt;Object, NotOnlyObjectInReal, AndMaybeMore&gt;&gt; in tests.

Other way is to inject checker inside Upsert caller ctor:

interface IUpsertService {
    void Upsert(List&lt;Object&gt; items);
    void SetChecker(Action&lt;Object&gt;);
}
public Owner(IUpsertService service)
{
    _service = service;
    service.SetChecker(CheckWhenNotFound)
}
private CheckWhenNotFound(Object item)
{
    if (item.Property != 645)
    {
        throw new EntityNotFoundException();
    }
}


Is there commmon solution for such problem?
Can't add a comment, so I'm going to try to answer the question.

If I'm understanding the question correctly, you don't want the responsibility of validation to be in the Upsert method. If that is the case then you can invert the control to another class by passing a 'Validator'. Which I think is what you are alluding to in the second part of the question but I would recommend the following implementation.

In a nutshell what you do is create a class which is responsible for validation which is injected into the main class. This makes this testable as well.

private readonly IValidator _validator;

public Constructor(IValidator validator)
{
  _validator = validator;
}

public void Upsert(List&lt;Object&gt; items)
{
    var existings = _db.Get().Where().ToList();
    foreach (var item in items)
    {
        var existing = existings.FirstOrDefault(x =&gt; x.Id == item.SomeId);
        if (existing == null)
        {
            _validator.ValidateEntity(existing);
        }
        else
        {
            // update existing
        }
    }
    _db.Save();
}


Example Implementation of the IValidator interface would be

public Validator : IValidator
{
  void ValidateEntity(object existing)
  {
    if (item.Property != 645)
    {
      throw new EntityNotFoundException();
    }
  }
}


You could take this further and use generics and allow the Validator class to decide what the object is and what needs validating.