What persistence engine are you planning on using? Now that you’re a bit more familiar with mvc, seeing a more complete app (with comments) might ease the task.
Also, here are some things I consider to be good practice tips, with reasons.
- Don’t pass entities to your views.
Depending on how you’ve scoped your persistence context / session, it will most likely have been disposed before the view is rendered. If you’ve used lazy load to iterate through child collections on an entity in a view, it will throw an exception. Not passing entities into a view also reduces the chances of accidental changes to the back end.
- Use a view model specific to your view.
If you only need to see name information on a contact list, but not phone and address (wating to save that for a detail veiw), then simply don’t include the extra information in your view model definition. View models are similar to a db View into a given Table, showing just what you want, and how you want it.
- Use a master view model to pass things common to all pages into your _layout template.
Self explanatory.
- Keep your controllers clean.
If you find a given action’s code becomming large and spaghetti like, offload it to a service class.
- Familiarize yourself with inversion of control, and use that to inject interfaced objects into your controllers, services and repositories.
Being able to remove the default ctors from these objects also means you can remove the “using” clauses needed to make them work, decoupling your objects from the interfaced objects.
- Use automapper to map your entities to view models.
Mapping complex view models can generate a lot of looped code. This can get messy real quick. Setup simple this-to-that maps with automapper and get your models later with a single call.
- Separate your application layers!
Remember how I had other namespaces in the samples “Models” folder for domain and persistence? In practice, these would be in their own projects and referenced from the mvc app.
- Use the Authorize attribute.
Regardless of whether or not you’re using built in membership, or rolled your own using IPrincipal and IIdentity, use this attribute to quickly resolve the current users rights to an action.
There’s a lot more, but for now, these should help you out. Don’t fret if one or more of the above are alien right now. You’ll run across things in your own explorations.