1

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!

moribvndvs
  • 40,946
  • 9
  • 129
  • 143
lexeme
  • 2,902
  • 9
  • 51
  • 112

2 Answers2

2

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

Community
  • 1
  • 1
archil
  • 37,513
  • 7
  • 61
  • 81
1

It depends on how the default scope in ninject works (I'm not a ninject user).

It will however work if you specify InRequestScope on the repository mapping.

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

Singleton scope will work as long as the connection to the database is not closed. Your application will stop work when it does since all requests would still try to use the same repository object.

That's why Request scope is better. If the repos fail, it will only fail for one request (unless it's a problem with the db).

I've written a set of best practices: http://blog.gauffin.org/2011/09/inversion-of-control-containers-best-practices/

jgauffin
  • 95,399
  • 41
  • 227
  • 352