I’m using EF with ViewModel and AutoMapper design strategies for an MVC3 application.
I’m wondering if there is a more efficient way of creating the CRUD pages then what I’m currently doing.
My Current Process Involves:
- Create the Entity
- Create the ViewModel via copy paste then deleted non-required fields
- Add the Entity to the Context list
- Create a controller via the Visual Studio 2010 create controller wizard page.
- I select a Template of Controller with read write actions and views, using Entity Framework.
- I choose my model to be my ViewModel instead of my entity.
- I select the appropriate context.
- Now the part I part I think can be improved, I have to re-write all the CRUD methods to use AutoMapper and the Entity/ViewModel design pattern changing:
return View(db.BlockedUserViewModels.ToList());
into:
IList<BlockedUser> blockedUsers = db.BlockedUsers.ToList();
IList<BlockedUserViewModel> blockedUserVMs = AutoMapper.Mapper.Map<IList<BlockedUser>, IList<BlockedUserViewModel>>(blockedUsers);
return View(blockedUserVMs);
- I have to add the same [Authorize] and roles permissions to each controller CRUD option.
This seems way overkill in workload! I’m hoping there a better solution. (I’m coming from Python/Django where it requires a single line of code to create beautiful strong CRUD pages)
It sounds like you can add a service and inject it into your controller. Then you only have to call
each time instead of:
This will keep your controllers light and act as a place to keep your crud logic so you don’t have to repeat it everywhere.
Also, you can add the
[Authorize]attribute to the controller if it applies to every action in the controller.