Question

I have an asp.net mvc solution with three projects

  • UI ASP.NET MVC
  • POCO My Domain POCO objects
  • Repository using entity framework and t4 to generate context and POCO classes

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

  • It will detect that the User is already in the context so will skip it (that's good).
  • It Will scaffold wiews using POCO.User as model (that's not what i want, i want it to generate ViewModel Class and use it)
  • It will Generate Repository (that's not good Too since i have already my repository in Repository Project)
  • It will scaffold controller with the repository created and send POCO to the views (not good too)

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

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top