2

I previously had a controller that had code like this:

public ActionResult Method(int Id)
        {
            var foo = doThis(Id)
            return View("Error");
        }

doThis() is a method that exists in the controller, and performs some logic. I'm now trying to relocate all business logic to a Services project that contains a bunch of classes.

To start I added a class library Project.Services and then added a class FooServices which contains the following:

namespace Project.Services
{
    class FooServices
    {
        public List<Bar> doThis(int Id)
        {
            //Do stuff
            return parentSets;
        }
    }
}

I've added a reference to this project from my MVC project, and a reference from this Services project to my data model project, but I'm not sure how to proceed now. How can I access these methods from controllers?

1 Answer 1

5

How can I access these methods from controllers?

In order to access an instance method you need an instance of the object:

public ActionResult Method(int Id)
{
    var foo = new FooServices().doThis(Id)
    return View("Error");
}

Of course by doing this you are now strongly coupling your controller logic with a specific implementation of your service making it very difficult to unit test your controllers in isolation.

So to weaken the coupling start by introducing an abstraction:

public interface IFooServices
{
    List<Bar> DoThis(int id)
}

and then have your service layer implement this interface:

public class FooServices: IFooServices
{
    public List<Bar> DoThis(int id)
    {
        //Do stuff
        return parentSets;
    }
}

Alright, now your controller could work with this abstraction:

public class HomeController: Controller
{
    private readonly IFooServices service;
    public HomeController(IFooServices service)
    {
        this.sevrice = service;
    }

    public ActionResult Method(int id)
    {
        var foo = this.service.DoThis(id)
        return View("Error");
    }
}

Great, at this stage we really have a weak coupling between your controller and the service layer. All that's left now is to configure your favorite dependency Injection framework to inject the specific service into your controller.

Sign up to request clarification or add additional context in comments.

1 Comment

I have a following situation, In my project there are ten controllers and ten service classes with business logic involved in each service class. Also I have reference data ( country, vendor, merchant and product names) which I need in all the controllers. Since I need to get the reference data in all the controllers Shall I write these get methods in all the ten service classes (redundant) or do I need to create a service class(there is no business logic) name it "refservice" with all the get methods? What is the best approach?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.