AutoMapper – Model/ViewModel mapping and validation

The goal of this post is to implement a base class to map and initialize view models using the domain object. This base class should make not only the mapping between the model and the view model but also the validation of the view model using the model annotations.

Each requirement will be implemented in two different base classes, the validation class will inherit from the mapping one as the domain object is needed to validate the view model. It makes sense to split the logic to keep the code cleaner and also to be able to use only the mapping functionality in case that we don’t need the validation.


When this class is implemented we can implement our view models like this:

public class ItemViewModel : BaseEntityMapperViewModel<ItemViewModelItem>
    public int Id { getset; }
    public string Name { getset; }

The Model

public class Item
    public int Id { getset; }
    [Index(IsUnique = true)]
    public string Name { getset; }

Sample of usage:

ViewModel – Model

My WebAPI controllers send and receive to the client a view model instance, when a instance needs to be updated in database it needs to be mapped to the domain first. This is because my domain services know nothing about view models:

// Update item
public void Put([FromBody]ItemViewModel updatedItem)
    if (!_itemsService.Update(updatedItem.MapToEntity()))
        throw new HttpResponseException(HttpStatusCode.NotFound);

Model – ViewModel

When a WebAPI need to load a instance from database and send it to the client, it loads a domain instance that needs to be mapped to a view model:

// Read item by id
public ItemViewModel Get(int id)
    var item = _itemsService.Get(id);
    if (item == null)
        throw new HttpResponseException(HttpStatusCode.NotFound);
    return ItemViewModel.MapFromEntity(item);


Continue reading


Development with Visual Studio Online

Visual Studio Online offers a Team Foundation Server on the cloud. This is free up to 5 users, each additional user cost $10 per month. The free version includes:

  • Complete integration with Visual Studio
  • Private code repositories
  • Backlog to track bug task, also Agile Processes
  • Continuous Integration Builds

This features together with the free or paid versions of Visual Studio provides a great development environment. Visual Studio Online can also be used with Eclipse and XCode.

Let’s try this features with the development of my Chess Clock for Windows Store:

Visual Studio Integration
The integration of Visual Studio Online and Visual Studio is straight forward…

1. Register in Visual Studio Online
2. Create a team project online
3. Login with your account from Visual Studio
4. Create or Open a solution and add it to Source Control

This is the Visual Studio Online Home Page:
From this view it is possible to create the backlog items and bug entries, see the code, create test plans, configure continuous integration builds and download the generated binaries as a zip.

Code Repositories
Having the code online is great to forget about backups, also Visual Studio Online offers the same advantages that TFS: Change set history, Code comparison, associate code check-in with bugs of user histories, etc.

For my development I have configured a Main branch for development, and a Production folder with branches for each release.

Continuous Integration
At the moment I have configured only one build configuration: Main.ChessClock.CI. This build is in Release mode and triggered automatically after a code check-in on the Main branch.

Visual Studio Online

Reverse the bits of a byte – c#

This method reverse the bits of a byte:

        // Reverses bits in a byte
        public static byte Reverse(byte inByte)
            byte result = 0x00;

            for (byte mask = 0x80; Convert.ToInt32(mask) > 0; mask >>= 1)
                // shift right current result
                result = (byte) (result >> 1);

                // tempbyte = 1 if there is a 1 in the current position
                var tempbyte = (byte)(inByte & mask);
                if (tempbyte != 0x00)
                    // Insert a 1 in the left
                    result = (byte) (result | 0x80);

            return (result);

A possible improvement could be to use this method to initialize the application to creating a table with all bytes and its inverted value.
Paying a slower initialization and more memory consumption all the conversions at runtime would be much faster.