I have a Domain object called User and UserCreateViewModel.Automapper is used for mapping from ViewModel to User.
Everything is working fine. When mapping is done I see User domain object has values in UserId, CreatedOn and ModifiedOn respectively like
00000000-0000-0000-0000-000000000000
1/1/0001 12:00:00 AM
1/1/0001 12:00:00 AM
These are default values when I instainciate User Object. This cause problem as my User object can not create Guid and Dates from properties Get.
Note: I don’t want to hardcode above default values.
Is there a clean approach to solve this problem so that UserId, CreateOn and ModifedOn generated values from properties?
I you understand my problem
UserCreateViewModel
public class UserCreateViewModel
{
public string UserName { get; set; }
public string Password { get; set; }
}
User
private Guid? _guid;
public Guid? UserId
{
get
{
return (_guid.HasValue) ? _guid.Value : Guid.NewGuid();
}
set
{
_guid = value;
}
}
public string UserName { get; set; }
public string Password { get; set; }
private DateTime? _createdOn;
public DateTime? CreatedOn
{
get
{
return (_createdOn.HasValue) ? _createdOn.Value : DateTime.Now;
}
set
{
_createdOn = value;
}
}
private DateTime? _modifiedOn;
public DateTime? ModifiedOn
{
get
{
return (_modifiedOn.HasValue) ? _modifiedOn.Value : DateTime.Now;
}
set
{
_modifiedOn = value;
}
}
Have a look at the BeforeMap and AfterMap features of Automapper. These might help you in customizing the assignment of default values.