repository-patternt4asp.net-mvc-scaffoldingasp.net-mvc-viewmodelt4scaffolding

ASP.NET mvc scaffolding with existing repository and mapping POCO to viewmodels


I have an asp.net mvc solution with three projects

I want my UI project to use ViewModels not the POCO classes.

I need to do scaffolding for UI and For Repository independently (i start with repository scaffolding, and then customize some code and then i will scaffold the uicontrollers view viewmodel)

I do Repository scaffolding in the repository project like this : scaffold Repository -ModelType POCO.User -DbContextType Context this is working like a charm

But in In the default mvcscaffolding template when i use the controller scaffolding : scaffold Controller -ModelType POCO.User -ControllerName UsersController -DbContextType Repository.Context -Repository

So i want to do something like this scaffold Controller -ModelType POCO.User -ControllerName UsersController -DbContextType Repository.Context -RepositoryType Repository.UserRepository -GenerateViewModel

that will skipp repository creation but use it in the controllers, that generates ViewModels from the poco and make mappion using automapper for example and use viewmodel in the view.

Any help where to start is apreciated. Thanks


Solution

  • What you are asking to do makes sense, but is not available out of the box. You can override the T4 templates, with this command

    > scaffold customTemplate Controller 
    

    But to work across projects, you will need to dive into the powershell and create your own scaffolder

    > scaffold customScaffolder ViewModel
    

    See Steven Sanderson's blog posts for more information. Also see this blog post for help in generating DTO or POCO classes in T4 templates.