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 7656251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:43:10+00:00 2026-05-31T12:43:10+00:00

If I have two controllers: public class PrimaryController : Controller { private IRepository<Primaries> repository;

  • 0

If I have two controllers:

public class PrimaryController : Controller
{
    private IRepository<Primaries> repository;

    public PrimaryController(IRepository<Primaries> repository)
    {
        this.repository = repository;
    }

    // CRUD operations
}

and

public class AuxiliaryController : Controller
{
    private IRepository<Primaries> repository;

    public AuxiliaryController(IRepository<Primaries> repository)
    {
        this.repository = repository;
    }

    // CRUD operations
    public ActionResult CreateSomethingAuxiliary(Guid id, AuxiliaryThing auxiliary)
    {
        var a = repository.Get(id);
        a.Auxiliaries.Add(auxiliary);
        repository.Save(a);

        return RedirectToAction("Details", "Primary", new { id = id });
    }
}

and DI is implemented like (code is from a Ninject module)

this.Bind<ISessionFactory>()
    .ToMethod(c => new Configuration().Configure().BuildSessionFactory())
    .InSingletonScope();

this.Bind<ISession>()
    .ToMethod(ctx => ctx.Kernel.TryGet<ISessionFactory>().OpenSession())
    .InRequestScope();

this.Bind(typeof(IRepository<>)).To(typeof(Repository<>));

will this work properly? I mean will controllers use the same repository instance?

Thanks!

  • 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-05-31T12:43:11+00:00Added an answer on May 31, 2026 at 12:43 pm

    Simple answer – yes! Code will use same implementation for all controllers unless you explicitly configure otherwise, using When... methods.

    If you want to reuse not implementation, but same instance of object, you could configure that using methods like InScope, InRequestScope, InSingletonScope as you already do for ISession and ISessionFactory.

    From documentation:

    // Summary:
    //     Indicates that instances activated via the binding should be re-used within
    //     the same HTTP request.
    IBindingNamedWithOrOnSyntax<T> InRequestScope();
    
    
    //
    // Summary:
    //     Indicates that only a single instance of the binding should be created, and
    //     then should be re-used for all subsequent requests.
    IBindingNamedWithOrOnSyntax<T> InSingletonScope();
    

    Using Repository in singleton is not a good Idea. I use InRequestScope to make one instance serve just one request. If using entity framework, you could check out this answer for details

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

Sidebar

Related Questions

I have two classes. UserModel and UserController. file: localhost/controllers/user.controller.php <?php class UserController { public
Let me have two very basic objects like: public class View { public View(Controller
I have this code that works. public class HelloWorldController : Controller { UAStagingEntities db
I have written the following code : @Controller @RequestMapping(/test) public class Home { @RequestMapping(value
I have this simple MVC 3 application which simply has two controllers(Home and Edit),
I have two threads running from a controller class. The first thread receives SMS
I have two controllers which share most of their code (but must be, nonetheless,
I have two view controllers in a tabbar which can both edit data. Therefore,
I have two view controllers that allow changes to the Address Book. The first
I have two view controllers name RootViewController and SecondViewController. In the FirstViewController I have

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.