开发者

shared model across controllers

开发者 https://www.devze.com 2022-12-13 00:09 出处:网络
right now in each of my controller classes i am instantiating a new Repository class to access my DB. Is there a way i can simply create this once (a single repository instance shared across all contr

right now in each of my controller classes i am instantiating a new Repository class to access my DB. Is there a way i can simply create this once (a single repository instance shared across all controllers) and pass into all contr开发者_JAVA百科ollers ?


ControllerBuilder.Current.SetControllerFactory(new MyOwnControllerFactory());

In your factory you can set up your repository and pass to ALL controllers.

But of course you better use IoC containers. They have option to create repository once per request / once per app lifetime.


You could make it a singleton.


Dependancy Injection (or create a base class for all your controllers)

0

精彩评论

暂无评论...
验证码 换一张
取 消