我有一个基本MappedViewModel
,也就是说,它是为域和其他模型之间的"自动"映射而构建的:
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()
{
}
}
我不知何故觉得NullifyReferenceProperties
在基类中应该是抽象的,以强制需要它的类实现它,但许多类不需要它,并且当模型不是为创建新实体而构建时,许多类需要它。像现在这样可以virtual
,还是有某种方法可以确定如何强制执行它的使用?
也许是具有派生MappedViewModelForCreate
的基础MappedViewModel
?
如果要
强制从MappedViewModel
派生的所有类覆盖NullifyReferenceProperties
方法,请使其abstract
。如果没有,我认为最好把它留virtual
.