3
votes

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
You shouldnt be using Injection on entities (have a look around in the Ninject tag or for articles in general and you'll quickly be convinced). You haven't really said what you're doing and what you're expecting to happen that isn't. I'm guessing you're not actually asking Ninject for your Entities / using Kernel.Inject - remember a DI Container doesnt go jumping in replacing news in the IL with magic interception (you may know that, but please edit the question to make stuff like this clear)Ruben Bartelink
sorry if it's wired. but im not using Injection on Entities im using in Entities. so if i cant use injection how can i resolve the dependency i mean when i use the Interface class it must know which class implemented it. if i instantiate like this: IVillageRepository villageRepo=new VillageRepository(); i will break the whole pattern right? all i want is to use Repository classes not just in Controller classes through Constructor Injection because its obvious that i won't be needed to work with database just in my Controllers. so how? sorry for weak English.Jalali Shakib
You shouldn't be doing that either. If you need to pass around a way to generate injected instances you can use Ninject.Extensions.Factory to generate Func<T> methods. I'd also recommend manning.com/seemann as an excellent investment for understanding the best approaches around this top to bottom. Your next step while the book is on the way is to go reading Mark Seemann's top answers on SO, and some others re injecting Entities.Ruben Bartelink
Your question finally deserves a +1. Do you really not agree that the answer does too?Ruben Bartelink
@RubenBartelink: actually i want to test the answer and after that, accept and plus one it. but what the hell :) accept and +1.Jalali Shakib

1 Answers

3
votes

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