I would like to have a datacontext available in every controller:
public abstract class ApplicationController : Controller
{
private ProjectDataContext datacontext = new SchoolDataContext();
protected ProjectDataContext DataContext
{
get { return datacontext; }
}
}
Controllers:
OhterControllers : ApplicationController
Is it a good idea to inherrit from ApplicationController (better way or pattern?) and is there a way to combinate it with Repository and Unit of work pattern
In my opinion, creating a new type of controller for keeping a data it isn’t an idea that a controller was intended to do. A new common controller should contain some common actions, soon.
Combination of Repository and Unit of work are more appropriate in this case. You can take into account also some IoC container implementation here.
I will suggest you define some IRepository interface for some CRUD operations. And pass this interface into controller constructor. Where some IoC resolver can provide concreate implementation for this interface. Most of modern IoC realisations (for example, Ninject) can provide you with options like create a single concreate implementation for a whole application or only a request.