使用ninject注入依赖关系模型类或非控制器类 [英] using ninject to inject dependency to The Model classes or non-controller classes

查看:122
本文介绍了使用ninject注入依赖关系模型类或非控制器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC 3(史蒂芬·桑德森棚架工)Repository模式使用Ninject 3。结果
在ninject我有类NinjectWebCommon,这在RegisterServices的方法解决我的依赖,我想我已经准备好去。

I using Ninject 3 in Repository pattern in mvc 3 (steven sanderson Scaffolder).
and in ninject i have the class "NinjectWebCommon" which in the "RegisterServices" method i resolved the dependencies and i think im ready to go.

  private static void RegisterServices(IKernel kernel)
  {
     kernel.Bind<ICityRepository>().To<CityRepository>();
     kernel.Bind<IVillageRepository >().To<VillageRepository>();
  }

我用我的仓库使用构造器注入控制器和一切都很好。

i using my repositories in controllers using constructor injection and everything is fine.

public class CityController : Controller
{
   private readonly ICityRepository cityRepository;

   // If you are using Dependency Injection, you can delete the following constructor
   //public CityController() : this(new CityRepository())
   //{
   //}

   public CityController(ICityRepository cityRepository)
   {
      this.cityRepository = cityRepository;
   }

  // .........
}

但是当我使用属性注射或字段注入其他类中使用这个库像模型(实体)班的依赖性不解决,我得到我的属性或字段空引用异常。

but when i use this repositories in other classes like Model(Entity) classes using property injection or field injection the dependency doesn't resolved and i get null reference exception on my Property or field.

[MetadataType(typeof(CityMetadata))]
public partial class City : IValidatableObject
{
   [Inject]
   public IVillageRepository VillageRepo { get; set; }

   public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
   {
      var village = VillageRepo.Find(5); // will throw null reference exception on "VillageRepo"
   }
}

public partial class CityMetadata
{
   [ScaffoldColumn(false)]
   public int ID { get; set; }

   [Required(ErrorMessage = MetadataErrorMessages.Required)]
   [StringLength(50, ErrorMessage = MetadataErrorMessages.ExceedMaxLength)]
   public string Name { get; set; }
}

我不知道为什么这种情况发生。有啥问题,我怎么可以使用非控制器类存储库?结果
提前致谢。

i don't know why this happening. so whats the problem and how can i use the repositories in non-controller classes?
thanks in advance.

推荐答案

您的问题是,你希望魔术。你注入一个实现你的资料库,然后你期望通过信息库中创建的数据对象与创建资源库的引用注入。

Your problem is, you're expecting magic. You inject an implementation for your repository, and then you expect that data objects created by that repository are injected with references of the creating repository.

首先,不喜欢的工作。仓库的实施,将新的()(或Activator.CreateInstance)在您的实体打电话,不能要求从Ninject内核或工厂的实例。您的可能的改写库(如果你使用EF那里......它会得到麻烦),但它可能不值得的麻烦。

First of all, that doesn't work like that. The implementation of the repository will call new() (or Activator.CreateInstance) on your entities, not ask for an instance from a Ninject Kernel or Factory. You could rewrite the repository (it'll get trickier if you're using EF there...) but it's probably not worth the hassle.

在这一切之上,你不应该需要,在所有。 实体不应该依赖于库,恕我直言,甚至没有自己的界面。

On the top of it all, you shouldn't be needing that at all. Entities shouldn't depend on repositories, imho not even their interfaces.

编辑:现在我明白你为什么要看到模型中的回购。我推荐的是一个静态的工厂也许吧。

now I see why you want to see a repo in your model. What I recommend is a static factory maybe.

public class Factories
{
public static readonly Instance = new Factories();
[Inject]
public Func<IVillageRepository> VillageRepo  {get; set;}
}

然后调用 Kernel.Inject(Factories.Instance); 从Ninject初始化code(你绑定IVillageRepository)。然后修改您的可验证实施 Factories.Instance.VillageRepo()找到(...);

Then call Kernel.Inject(Factories.Instance); from your Ninject initialization code (where you bind IVillageRepository). Then modify your validatable implementation to Factories.Instance.VillageRepo().Find(...);

这篇关于使用ninject注入依赖关系模型类或非控制器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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