I need to convert between these two classes, and want to maintain DRY but not violate the Single Responsibility Pattern…
public class Person
{
public string Name {get;set;}
public int ID {get;set;}
}
public class PersonEntity : TableServiceEntity
{
public string Name {get;set;}
public int ID {get;set;}
// Code to set PartitionKey
// Code to set RowKey
}
More Info
I have some Model objects in my ASP.NET MVC application. Since I’m working with Azure storage I see the need to convert to and from the ViewModel object and the AzureTableEntity quite often.
I’ve normally done this left-hand-right-hand assignment of variables in my controller.
Q1
Aside from implicit/explicit conversion, should this code be in the controller(x) or the datacontext(y)?
Person <--> View <--> Controller.ConverPersonHere(x?) <--> StorageContext.ConvertPersonHere(y?) <--> AzurePersonTableEntity
Q2
Should I do an implicit or explicit conversion?
Q3
What object should contain the conversion code?
Update
I’m also implementing WCF in this project and am not sure how this will affect your recommendation . Please also see this question.
Q1: The controller.
Q2: Convert manually or with the help of a mapping tool such as AutoMapper.
Q3: I would put the code for this in a converter or mapper class like the following. Note that IConverter is shared among all converters, and IPersonConverter just exists so your controllers and service locators can use it.