哪种模式用于记录?依赖注入或服务定位器? [英] Which pattern to use for logging? Dependency Injection or Service Locator?

查看:144
本文介绍了哪种模式用于记录?依赖注入或服务定位器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这种情况。我有一些业务逻辑,现在然后将被要求写一个日志。

Consider this scenario. I have some business logic that now and then will be required to write to a log.

interface ILogger
{
    void Log(string stuff);
}

interface IDependency
{
    string GetInfo();
}

class MyBusinessObject
{
    private IDependency _dependency;

    public MyBusinessObject(IDependency dependency)
    {
        _dependency = dependency;
    }

    public string DoSomething(string input)
    {
        // Process input
        var info = _dependency.GetInfo();
        var intermediateResult = PerformInterestingStuff(input, info);

        if (intermediateResult== "SomethingWeNeedToLog")
        {
            // How do I get to the ILogger-interface?
        }

        var result = PerformSomethingElse(intermediateResult);

        return result;
    }
}

您将如何获取ILogger界面?我看到两个主要的可能性:

How would you get the ILogger interface? I see two main possibilities;


  1. 使用依赖注入在构造函数上传递。

  2. 获取通过一个单一的服务定位器。

你喜欢哪种方法,为什么?还是有更好的模式?

Which method would you prefer, and why? Or is there an even better pattern?

更新:
请注意,我不需要记录所有方法调用。我只想记录在我的方法中可能发生或可能不会发生的几个(罕见)事件。

Update: Note that I don't need to log ALL method calls. I only want to log a few (rare) events that may or may not occur within my method.

推荐答案

我个人做一个

以下是我的惯例:


  • 从静态上下文 - 服务位置

  • 从实例上下文 - 依赖注入

  • From a static context - Service Location
  • From an instance context - Dependency Injection

我觉得这给了我可靠性的平衡。我发现使用服务位置的类设置测试比使用DI更困难一点,所以这就是为什么服务位置是异常而不是规则。我使用它一致,所以不难记住需要写什么类型的测试。

I feel this gives me the right balance of testability. I find it a little harder to setup tests against classes that use Service Location than use DI, so this is why Service Location ends up being the exception rather than the rule. I'm consistent in its use, though, so it's not hard to remember what type of test I need to write.

有些人提出了DI倾向于混乱的担忧构造函数。我不觉得这是一个问题,但如果你觉得这样,有很多替代方案使用DI,但避免构造函数的参数。以下是Ninject的DI方法列表:
http://ninject.codeplex。 com / wikipage?title =注入%20Patterns

Some have raised the concern that DI tends to clutter constructors. I don't feel this is a problem, but if you feel this way, there are a number of alternatives that use DI, but avoid constructor parameters. Here is a list of Ninject's DI methods: http://ninject.codeplex.com/wikipage?title=Injection%20Patterns

你会发现大多数反转控件容器具有与Ninject相同的功能。我选择显示Ninject,因为他们有最简洁的样本。

You'll find that most Inversion of Control containers have the same features as Ninject. I chose to show Ninject because they have the most concise samples.

希望这是有帮助的。

编辑:要清楚,我使用Unity和公共服务定位器。我有一个用于DI的Unity容器的单例实例,我的IServiceLocator的实现只是一个单一的Unity容器的包装。这样我就不需要做任何类型的映射两次或任何这样的事情。

To be clear, I use Unity and Common Service Locator. I have a singleton instance of my Unity container for DI and my implementation of IServiceLocator is simply a wrapper around that singleton Unity container. This way I don't have to do any type mappings twice or anything like that.

我也没有发现AOP在追踪之外特别有用。我更喜欢手动记录,只是为了清晰起见。我知道大多数AOP日志记录框架都有能力,但是我大部分时间都不需要前者(AOP的面包和黄油)。当然,这只是个人喜好。

I also don't find AOP to be particularly helpful beyond tracing. I like manual logging better simply for its clarity. I know that most AOP logging frameworks are capable of both, but I don't need the former (AOP's bread and butter) most of the time. This is just personal preference, of course.

这篇关于哪种模式用于记录?依赖注入或服务定位器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆