using ninject to inject dependency to The Model cl

2019-07-03 22:14发布

问题:

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.

回答1:

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.

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.

EDIT: 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;}
}

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(...);