tags:

views:

52

answers:

3

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 controllers ?

A: 

You could make it a singleton.

SLaks
Why was this downvoted?
SLaks
It shouldn't be downvoted.
Mike C.
A: 

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

David Kemp
Creating a base class won't share the instance, which I assume he's trying to do.
SLaks
+2  A: 
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.

queen3
+1 for IoC Containers
David Kemp