7

When using the examples for Single Page Application, I've the following TodoItem controller:

public partial class MVC4TestController : DbDataController<MVC4TestContext>
{
    public IQueryable<TodoItem> GetTodoItems()
    {
        return DbContext.TodoItems.OrderBy(t => t.TodoItemId);
    }
}


Question 1:
It seems that only EntityModels are supported ?
When using a real ViewModel (model only used for the Views, not not used as 1:1 mapping to database entity), the DbDataController does not support this.

Also using Linq.Translations or PropertyTranslator does not seem to work, see this code extract:

private static readonly CompiledExpressionMap<TodoItem, string> fullExpression =
    DefaultTranslationOf<TodoItem>.Property(t => t.Full).Is(t => t.Title + "_" + t.IsDone);

public string Full
{
    get
    {
        return fullExpression.Evaluate(this);
    }
}


Question 2:
What is the recommended design when using SPA, DBContext and ViewModels ?

Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121

1 Answers1

3

As far as I know so far is - it instists on the usage of "real" model classes bound to DbContext. I have the same problem as you - I need to use my own DTO objects which are "flat". The Json serialisation is currently not able to serialize data which has parent references in child objects (cyclic references). Usually I do not need the entity tree anyways so I created smaller classes which fits perfectly to the view. I tried to use a normal Controller with JsonResult and parsed the returned model into ko.mapping.fromJS after retrieved the data. Thats working fine. But - you have to take care of all the nice stuff the MVC4 generated viewmodels are already dealing with (like creating navigation, etc.). Maybe someone finds a workaround to "fake" a DbContext with DTO data.

Obiwan007
  • 646
  • 1
  • 8
  • 20
  • 2
    After some more experiments: You can use your own DTO objects as far as you have an Id mapped property which is marked as [Key]. But for scaffolding the stuff you need to have a "real" db based model. After the controller and view stuff is generated you could easily replace the DBController stuff with your own DTO and create the data as you like. But of cause you can't use the DbContext for that in the Update/Insert helper methods of the DbController. – Obiwan007 Mar 29 '12 at 17:41