横切关注点DI

如日志这种横切关注点,就不适合构造注入,而是适合公共属性注入。

public class PersonAppService{

    public ILogger Logger { get; set; }

    private IPersonRepository _personRepository;

public PersonAppService(IPersonRepository personRepository)

    {

        _personRepository = personRepository;

        Logger = NullLogger.Instance;

    }

    public void CreatePerson(string name, int age)

    {

        Logger.Debug("Inserting a new person to database with name = " + name);

        var person = new Person { Name = name, Age = age };

        _personRepository.Insert(person);

        Logger.Debug("Successfully inserted!");

    }

}

你可能感兴趣的:(横切关注点DI)