Rad1
Rad1

Reputation: 215

MVC 4 Controller and Web Api controller

I am currently working on an MVC 4 project and i need to give acces to the database to the mobile apllication so i choosed to implement my Web services in Web Api to get a Json resulat . The problem is i have many code redundancy ! the same code is existent in MVC controller and in the Web Api controller .

For exemple the get procedure :

1- web api controller :

 public User GetUser(int id)
    {
        User user = db.Users.Find(id);
        if (user == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }

        return user;
    }

2-MVC controller

 public ActionResult Details(int id = 0)
    {
        User user = db.Users.Find(id);
        if (user == null)
        {
            return HttpNotFound();
        }
        return View(user);
    }

one is returning the User entity in JSon and the other return a view ! So how can use one Controller in the other to get rid of redandency ?

Upvotes: 0

Views: 487

Answers (3)

Yishai Galatzer
Yishai Galatzer

Reputation: 8862

You can just return JSON() from the MVC controller and avoid the usage of two frameworks. There is nothing wrong with that, and will keep your life simple.

public ActionResult GetUser(int id) // GetUser is the action name, or you can just use Index
{
    User user = db.Users.Find(id);
    if (user == null)
    {
        return HttpNotFound();
    }

    return Json(user);
}

Upvotes: 1

kkocabiyik
kkocabiyik

Reputation: 4406

As paul said this is all about separation of concerns. Paul provided you an example with a "logical service layer" which is an independent class library in your solution that your other Web Applications or desktop applications etc. reference it. Another example may be a "physical service layer" which is another Web Api Project in your solution, that contains all the service methods of your application. From your MVC project whenever you want a call to have your users, you create a new WebClient to call your web api's GetUser end points.

Upvotes: 1

Paul Taylor
Paul Taylor

Reputation: 5751

Looking at this question from the point of view of separation of concerns, both of the controllers have a different function (supplying JSON and markup respectively), but both need to make use of a common service: namely, data persistence.

For that reason, as @paul suggested in the comments, the repository pattern offers a good design to solve this problem. In your example, you may not seem to gain much, but as soon as your data retrieval and persistence logic becomes more complex, the repository will enforce consistency, reduce redundancy, and support more sophisticated patterns such as dependency injection.

Here's a simple repository:

interface IRepository
{
    User GetUser(int id);
}

public class MyRepository: IRepository
{
    public User GetUser(int id)
    {
        return db.Users.Find(id);
    }
}

Api controller

public User GetUser(int id)
{
    var repo = new MyRepository();
    User user = repo.GetUser(id);
    if (user == null)
    {
        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
    }
    return user;
}

MVC controller:

public ActionResult Details(int id = 0)
{
    var repo = new MyRepository();
    User user = repo.GetUser(id);
    if (user == null)
    {
        return HttpNotFound();
    }
    return View(user);
}

Upvotes: 1

Related Questions