I have a base MappedViewModel, that is, it is built for ‘automatic’ mapping to and from domain and other models:
public abstract class MappedViewModel<TEntity> : ViewModel
{
public virtual void MapFromEntity(TEntity entity, bool forCreate = false)
{
Mapper.Map(entity, this, typeof(TEntity), GetType());
if (ModelPurpose == ViewModelPurpose.Create)
{
NullifyReferenceProperties();
}
}
public virtual TEntity MapToEntity()
{
return Mapper.Map<TEntity>(this);
}
protected virtual void NullifyReferenceProperties()
{
}
}
I somehow feel that NullifyReferenceProperties should be abstract in the base class, to force classes that need it to implement it, but many classes don’t need it, and many that need it when the model is not built for creating a new entity. Is it OK like it is now as virtual, or is there some way I can determine how to enforce its use?
Maybe a base MappedViewModel with a derived MappedViewModelForCreate?
If you want to force ALL classes derived from
MappedViewModelto override theNullifyReferencePropertiesmethod make itabstract. If not, I think it’s better to just leave itvirtual.