Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I create an interface for each Model?

I'm just getting started with Dependency Injection (DI) using Ninject and am working through my controllers looking to decouple them from my models a bit more.

At the moment, inside of my controllers I am creating an instance of some given model e.g:

var activitiesModel = new ActivitiesModel();

For each of my models that I've been instantiating in this way, should I extract an interface for these and then use DI to tie these things together?

An example of where I'm currently doing this is inside my ActivitiesController:

IActivitiesModel _activitiesModel;

        public ActivitiesController(IActivitiesModel activitiesModel)
        {
            _activitiesModel = activitiesModel;
        }

and this is tied together in my global.asax:

Bind<IActivitiesModel>().To<ActivitiesModel>();

Is this the correct way to go about doing this? Should I be creating a new interface for each of my models that is instantiated inside of a controller?

Cheers for any help and nudges in the right direction :-)

like image 252
Jamie Dixon Avatar asked May 28 '10 11:05

Jamie Dixon


1 Answers

It depends on what those models are doing. If they posses data access and manipulation methods then they should be abstracted to weaken the coupling between your controller and data access logic and ease the testing in separation. If they are simply POCO and/or data transfer objects then you don't need to abstract them.

like image 173
Darin Dimitrov Avatar answered Sep 28 '22 23:09

Darin Dimitrov