构造函数注入以及何时使用服务定位器

Sim*_*mon 1 structuremap dependency-injection service-locator constructor-injection

我正在努力理解StructureMap的部分用法.特别是,在文档中有一个关于常见反模式的声明,仅使用StructureMap作为服务定位器而不是构造函数注入(直接来自Structuremap文档的代码示例):

 public ShippingScreenPresenter()
    {
        _service = ObjectFactory.GetInstance<IShippingService>();
        _repository = ObjectFactory.GetInstance<IRepository>();
    }
Run Code Online (Sandbox Code Playgroud)

代替:

public ShippingScreenPresenter(IShippingService service, IRepository repository)
    {
        _service = service;
        _repository = repository;
    }
Run Code Online (Sandbox Code Playgroud)

这对于一个非常短的对象图很好,但是当处理很多级别的对象时,这是否意味着你应该从顶部向下传递更深层对象所需的所有依赖项?当然,这会破坏封装并暴露有关更深层对象实现的过多信息.

假设我正在使用Active Record模式,因此我的记录需要访问数据存储库才能保存和加载自身.如果此记录加载到对象内,该对象是否调用ObjectFactory.CreateInstance()并将其传递给活动记录的构造函数?如果该对象在另一个对象内部怎么办?是否将IRepository作为自己的参数进一步向上?这将向父对象公开我们此时访问数据存储库的事实,外部对象可能不应该知道.

public class OuterClass
{
    public OuterClass(IRepository repository)
    {
        // Why should I know that ThingThatNeedsRecord needs a repository?
        // that smells like exposed implementation to me, especially since
        // ThingThatNeedsRecord doesn't use the repo itself, but passes it 
        // to the record.
        // Also where do I create repository? Have to instantiate it somewhere
        // up the chain of objects
        ThingThatNeedsRecord thing = new ThingThatNeedsRecord(repository);
        thing.GetAnswer("question");
    }
}

public class ThingThatNeedsRecord
{
    public ThingThatNeedsRecord(IRepository repository)
    {
        this.repository = repository;
    }

    public string GetAnswer(string someParam)
    {
        // create activeRecord(s) and process, returning some result
        // part of which contains:
        ActiveRecord record = new ActiveRecord(repository, key);
    }

    private IRepository repository;
}

public class ActiveRecord
{
    public ActiveRecord(IRepository repository)
    {
        this.repository = repository;
    }

    public ActiveRecord(IRepository repository, int primaryKey);
    {
        this.repositry = repository;
        Load(primaryKey);
    }

    public void Save();

    private void Load(int primaryKey)
    {
        this.primaryKey = primaryKey;
        // access the database via the repository and set someData
    }

    private IRepository repository;
    private int primaryKey;
    private string someData;
}
Run Code Online (Sandbox Code Playgroud)

任何想法将不胜感激.

西蒙

编辑: 意见似乎是注入应从顶层开始.ActiveRecord将注入到ThingThatNeedsRecord中,并注入到OuterClass中.这样的问题是,如果ActiveRecord需要使用运行时参数(例如要检索的记录的id)进行实例化.如果我在顶部注入ActiveRecord到ThingThatNeedsRecord,我不得不弄清楚在那一点需要什么id(它将顶层暴露给它不应该实现的实现)或者我必须有一个部分构造的ActiveRecord并稍后设置ID.如果我需要N条记录并且在ThingThatNeedsRecord中执行逻辑之前不会知道,这会变得更加复杂.

Dan*_*att 6

控制倒置就像暴力一样.如果它没有解决你的问题,你就没有使用它.或类似的东西.

更重要的是,我认为你OuterClass应该ThingThatNeedsRecord通过构造函数注入注入它.同样ThingThatNeedsRecord应该ActiveRecord注入它.这不仅可以解决您的直接问题,还可以使您的代码更加模块化和可测试.