简体   繁体   English

通过多个控制器共享数据。 ASP.NET MVC

[英]Sharing data thru several controllers. ASP.NET MVC

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) DI的实现方式(代码来自Ninject模块)

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! 谢谢!

Simple answer - yes! 简单的答案 - 是的! Code will use same implementation for all controllers unless you explicitly configure otherwise, using When... methods. 代码将对所有控制器使用相同的实现 ,除非您使用When...方法显式配置。

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. 如果您想重用不是实现,而是重用对象的实例 ,则可以使用像InScopeInRequestScopeInSingletonScope这样的方法来配置它,就像您已经为ISession和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. 在单例中使用Repository不是一个好主意。 I use InRequestScope to make one instance serve just one request. 我使用InRequestScope使一个实例只提供一个请求。 If using entity framework, you could check out this answer for details 如果使用实体框架,您可以查看此答案以获取详细信息

It depends on how the default scope in ninject works (I'm not a ninject user). 这取决于ninject中默认作用域的工作方式(我不是ninject用户)。

It will however work if you specify InRequestScope on the repository mapping. 但是,如果在存储库映射上指定InRequestScope ,它将起作用。

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. 这就是Request范围更好的原因。 If the repos fail, it will only fail for one request (unless it's a problem with the db). 如果repos失败,它只会失败一个请求(除非它是db的问题)。

I've written a set of best practices: http://blog.gauffin.org/2011/09/inversion-of-control-containers-best-practices/ 我写了一套最佳实践: http//blog.gauffin.org/2011/09/inversion-of-control-containers-best-practices/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM