从Entity Framework中检索对象的依赖注入

时间:2015-03-10 14:18:59

标签: c# entity-framework dependency-injection ninject

假设我有以下型号:

public class Dojo
{
    public int DojoId {get;set;}
    public DojoProperty PropOne{get;protected set;}
    public DojoProperty PropTwo{ get;protected set; }
    public virtual ICollection<ISamurai> Warriors { get; set; }
    private IDojoService _dojoService;
    public Dojo(IDojoService dojoService)
    {
        _dojoService = dojoService;
    }
}

我正在使用DI的最新ninject,并希望拥有一个工厂方法,并且还为ORM实现了最新版本的Entity Framework,因此我按如下方式重构了该类:

public class Dojo
{
    public int DojoId {get;set;}
    public DojoProperty PropOne{get;protected set;}
    public DojoProperty PropTwo{ get;protected set; }
    public virtual ICollection<ISamurai> Warriors { get; set; }
    private IDojoService _dojoService;
    private Dojo(IDojoService dojoService)
    {
        _dojoService = dojoService;
    }
    public static Dojo Create(IDojoService dojoService, IDojoConfig dojoConfig)
    {
        return new Dojo(dojoService) {PropOne = dojoConfig.PropOne, PropTwo = dojoConfig.PropTwo};
    }
}

我的问题是,当我从数据库中检索Dojo时,如何让Entity Framework使用IoC容器注入IDojoService,即:

context.Dojos.Find(dojoId);

1 个答案:

答案 0 :(得分:0)

所以史蒂文的评论让我做了很多有用的阅读,特别是他的这篇文章(我假设?) https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=91

起初我误解了他的方法,原来的答案如下。

根据他最近的评论我的新答案是:
用法优先:

    public void AddWarrior(int dojoId, ISamurai warrior)
    {
         var command = new AddWarriorCommand {DojoId = dojoId, Warrior = warrior};
         handler.Handle(command);
    }

更深入AddWarriorCommandHandler

public class AddWarriorCommandHandler : ICommandHandler<AddWarriorCommand>
{
    //ommitted ctor bringing in dependencies (e.g. Repository)
    public void Handle(AddWarriorCommand command)
    {
            var dojo = context.Dojos.Find(command.dojoId);
            dojo.Warriors.Add(command.Warrior);
            context.SaveChanges();
    }
}

新模型:

public class Dojo
{
    public int DojoId { get; set;}
    public DojoProperty PropOne { get; protected set;}
    public DojoProperty PropTwo{ get; protected set; }
    public virtual ICollection<ISamurai> Warriors { get; set; }

    private Dojo(){}

    public static Dojo Create(IDojoConfig dojoConfig)
    {
        return new Dojo() {PropOne = dojoConfig.PropOne, PropTwo = dojoConfig.PropTwo};
    }
}

后代的老答案: <击> 这使我解释了以下方法,这有助于防止我试图避免的贫血领域模型,保持IoC容器和ORM的快乐,实际上提高了整体的SOLIDness,可测试性和他的文章中概述的其他好处。 < / p> <击>

public class Dojo
{
    public int DojoId { get; set;}
    public DojoProperty PropOne { get; protected set;}
    public DojoProperty PropTwo{ get; protected set; }
    public virtual ICollection<ISamurai> Warriors { get; set; }

    private Dojo(){}

    public static Dojo Create(IDojoConfig dojoConfig)
    {
        return new Dojo() {PropOne = dojoConfig.PropOne, PropTwo = dojoConfig.PropTwo};
    }

    public void AddWarrior(ICommandHandler<AddWarriorCommand> handler, ISamurai warrior)
    {
         var command = new AddWarriorCommand {Warrior = warrior};
         handler.Handle(command);
    }
}

然后用法:

var dojo = context.Dojos.Find(dojoId);
dojo.AddWarior(handler, new Warrior());

根据文章定义AddWarriorCommand : ICommandICommandHandler,后者使用ninject注入依赖类