Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8782785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:39:36+00:00 2026-06-13T20:39:36+00:00

I using Ninject 3 in Repository pattern in mvc 3 (steven sanderson Scaffolder). and

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:39:37+00:00Added an answer on June 13, 2026 at 8:39 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Ninject in my ASP MVC 3 project, I have modified the
I'm using Ninject to do dependency injection. I have a userService in which I
I'm using Ninject.MVC3. private static void RegisterServices(IKernel kernel) { kernel.Bind<Repository>().To<Repository>(); } Registering them like
I am using: Asp.net MVC3 Ninject Repository pattern with Entity Framework 4.0 On an
I am currently creating an MVC3 application using Ninject. I have two controllers which
I have successfully setup a simple mvc application that lists teams. I'm using Ninject
I have started using Ninject 2 (downloaded from Github yesterday including the MVC extension
I have an MVC application using Ninject to connect to a single database. Now
I have an asp.net-mvc website and i am using ninject for IOC and nhibernate
I am using Ninject, NHibernate, ASP.NET MVC3 and repository pattern. The module binding in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.