3

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.

Jalali Shakib
  • 616
  • 8
  • 17
  • 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 `new`s in the IL with magic interception (you may know that, but please edit the question to make stuff like this clear) – Ruben Bartelink Nov 04 '12 at 01:34
  • 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 Nov 04 '12 at 02:21
  • 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](https://github.com/ninject/ninject.extensions.factory/wiki) to generate `Func` methods. I'd also recommend http://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 Nov 04 '12 at 09:50
  • Your question finally deserves a +1. Do you really not agree that the answer does too? – Ruben Bartelink Nov 07 '12 at 00:23
  • @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 Nov 07 '12 at 02:28
  • Ya +1 it if it teaches you something or is gives you enough food for thought to be able to make a jump in your question. As my own record shows, whether and when you accept is far more debatable - I generally only accept answers that meet the terms of my question in full -- i.e., not just picking the best of a bad lot. – Ruben Bartelink Nov 07 '12 at 08:55

1 Answers1

3

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

Community
  • 1
  • 1
TDaver
  • 7,164
  • 5
  • 47
  • 94
  • thanks. but i think there is disparagement here. my problem is not expecting magic, my problem is lack of knowledge. i wrote the question this way to make my question more obvious so its obvious is impossible and you did get that fast and gaved me the solution. ill check this out. – Jalali Shakib Nov 05 '12 at 21:05
  • @JalaliShakib: I didn't mean that you expected magic on purpose :) That's why I highlighted the new vs Kernel.Get issue here. Your code (and some factory code) can Get from Ninject. EF will never do. – TDaver Nov 05 '12 at 21:13
  • by the way you may correct `Factories.Instance.VillageRepo().Find(...);` because VillageRepo is a property not a method. thanks. – Jalali Shakib Nov 11 '12 at 13:42
  • `VillageRepo` is a property, which returns a `Func`. The `()` is there to execute that `Func` to get an instance of `IVillageRepository`. And before you ask, it's very unwise to store a singleton instance of your repository, as it can lead to lifetime problems. Much better to leave the lifetime handling to the IoC container, and just request an instance from the factory (which may or may not be a new instance) every time. – TDaver Nov 11 '12 at 16:00
  • oh my god, how didn't i noticed about the "Func"??!!. yeah you got it. – Jalali Shakib Nov 16 '12 at 20:51
  • but when i used Func, it gives me the "Error activating IntPtr No matching bindings are available, and the type is not self-bindable." error. whats going on? i put this line of code `Kernel.Inject(Factories.Instance);` for injecting dependencies to Factories class in RegisterServices Method where i bind IVillageRepository. and it throw error on this line. – Jalali Shakib Nov 16 '12 at 21:08
  • 1
    you may need the Ninject.Extensions.Factory module. IIRC that brings in the support for Func-based factories... – TDaver Nov 17 '12 at 10:12
  • ITIMRTNOS (i thought it may related to Ninject or something) ;) – Jalali Shakib Nov 18 '12 at 12:06