Software Architect / Microsoft MVP (AI) and Pluralsight Author

Architecture, C#, MVC, Tooling

ASP.NET Patterns and Practices (MVC)

icon-aspnetmvcIntroduction

Whilst building .NET web applications you search for and identify best practices and implement these where you can to make your code more readable, secure and maintainable.

I’ve taken some of my notes and tips that have guided me in the past and put them together in this blog post.

No “magic strings”

Never use magic strings in your code. This means hard-coding view names, link text etc. into your views.

Controller and Domain logic

Try to keep away domain logic from controller.  Controller should only be responsible for:

  1. Input validation and sanitization.
  2. Get view related data from the model.
  3. Return the appropriate view or redirect to another appropriate action method.

I personally prefer to implement an Orchestrator design pattern in my Controllers.

These encapsulate everything that’s needed to return a strongly typed View Model, whether it be calling an external web service or data store or leveraging a legacy DLL in your domain.

You can read more about that here.

Domain Model != View Model

The Domain Model represents the domain, while the View Model is designed around the needs of the View.  These two worlds might be (and usually are) different.

Additionally, the Domain Model is “data plus behaviours”, is hierarchical and is made of complex types, while the ViewModel is just a DTO, flat, and made of strings.

To remove the tedious and error-prone object-mapping code, you can use AutoMapper.

AutoMapper has saved me a shed load of time in the past.  I initially started out writing Extension Methods such as this:

PersonViewModel person = null = db.People.Find(id).ToViewModel();

Under the hood, ToViewModel would do something like this:

public static PersonViewModel ToViewModel(this Person person)
        {
            PersonViewModel PersonViewModel = new PersonViewModel
            {
                PersonId = person.PersonId,
                Firstname = person.Firstname,
                Lastname = person.Lastname,
                DateOfBirth = person.DateOfBirth,
                DOB = person.DateOfBirth,
                Middlename = person.Middlename,
                Title = person.Title,
                Active = person.Active,
                GenderId = person.GenderId,
                Lookup = person.Lookup
            };
            return PersonViewModel;
        }

With AutoMaper, you don’t need any of that.  You install the NuGet package and you can write code like this:

Mapper.CreateMap<Person, PersonViewModel>();
person = Mapper.Map(db.People.Find(id));

This will save you loads of time. You can configure more complex mappings if your object graph demands it. The out of the box functionality is works well.

Stored Procedures and Entity Framework

Entity Framework makes it easy to fetch data from your datasource.  When things move beyond the Customer and Orders samples you find lying about the web, my preference is to have stored procedures for the more complex scenarios.  If your application doesn’t have many users, only deals with a handful of domain objects and requires basic CRUD functionality, I’d use Entity Framework for all data access.

Make the judgement call as and when you develop each feature.  You can use Entity Framework in conjunction with stored procedures if you want to anyway.

JavaScript Libraries

This is almost IMPOSSIBLE to keep up with.  There are hundreds of these out there and it seems a new one is released every week.  Apparently here are 12 to watch for 2017. Everyone is aware of JQuery, start off with that and then experiment with the others!

Finally

This list isn’t exhaustive and you’ll develop your own best practices whilst working on your own web applications.  If you’re application is well architected and adopts some best practices it’ll be easier for developers to maintain and code new features.  It’ll be easier for new developers to navigate their way around too.  All of this saves time and money!

Keen to hear what patterns and practices you employ?

JOIN MY EXCLUSIVE EMAIL LIST
Get the latest content and code from the blog posts!
I respect your privacy. No spam. Ever.

Leave a Reply