0

I’m in the process of writing my first MVC application, and whilst I have been trying to apply the guidelines given in other answers the structure of the project is making this difficult (and I'm making a mess of things!). Currently, the application is split into the following layers:

  • Domain objects/logic accessed using Web API services. I’m treating this layer as a black box as some of the code is used in several WPF applications and is not under my control.

  • Repository layer. This layer calls the Web API services, and was added to help unit testing. It calls the services and returns a deserialized domain model.

  • Controller. I have two different styles of controller depending on how the data will be used.

    In the first type, the controller calls the methods in the repository and then uses a mapper class to convert the domain objects to the view models. The mapping is done manually as some of the view models are significantly different to the underlying domain objects. E.g:

    DomainObject domainObject = m_repository.GetObject(id);
    ViewModel model = ModelMapper.PopulateViewModel(domainObject);
    return View(model);
    

    The second type is designed to provide data used by AJAX calls. Here, the controller calls the repository as before, but then the domain model is converted to JSON using custom Json.Net converters. E.g:

    var jsonNetResult = new JsonNetResult();
    DataObject data = m_repository.GetData(id);
    
    jsonNetResult.Converters.Add(new DataObjectConverter());
    jsonNetResult.Data = data;
    
    return jsonNetResult;
    

Questions:

It feels like I’m doing a lot of switching between JSON, the domain objects and view models. This doesn’t seem particularly performant – can/should the repository be creating the view models instead of the controller?

If the data is intended to be supplied as JSON, I'm converting the domain objects to JSON and not using view models. This seems inconsistent, but I don't want to map to a view model just for the sake of it. Is this the right approach?

I can find a lot of examples using MVC on top of the entity framework, but I’m struggling to find examples of using Web API within an MVC application. Can anyone point me in the right direction?

Hopefully this makes sense, thanks!

1 Answer 1

2

I would suggest a simpler model for your application: Think of Web APIs and classic MVC controllers as two ways to expose your repository information where the repository layer reflects your persistent storage. You don’t want to have the MVC controller call into the Web API – just go straight to the repository.

There are a number of examples of how to use Web APIs with MVC applications, you can for example have a look at [1].

Henrik

[1] http://www.asp.net/web-api/samples

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

1 Comment

I have had a look at those examples, and can see how they are using the Web API and controller layer. But, the Web API layer is providing what the view needs (in the same way a controller would) whereas in the project I’m writing the Web API layer is providing access to business models that don’t closely reflect the structure of the views. The reason I have a repository layer between the controllers and Web API is more to help my testing than anything else – possibly this is badly named.

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.