使用ninject注入依赖关系模型类或非控制器类

编程入门 行业动态 更新时间:2024-10-26 16:23:47
本文介绍了使用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注入依赖关系模型类或非控制器类

本文发布于:2023-11-17 04:25:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608823.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   或非   模型   关系   ninject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!