Should I create an interface for each Model?
Asked Answered
T

1

7

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 :-)

Tillie answered 28/5, 2010 at 11:32 Comment(0)
N
5

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.

Nalley answered 28/5, 2010 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.