You can achieve it with following code
static class ModelStateMappings
{
public static DomainModelMapping<TDomainModel> MapDomainModel<TDomainModel>()
{
// edit the constructor to pass more information here if needed.
return new DomainModelMapping<TDomainModel>();
}
}
public class DomainModelMapping<TDomainModel>
{
public ViewModelMapping<TDomainModel, TViewModel> MapViewModel<TViewModel>()
{
// edit the constructor to pass more information here if needed.
return new ViewModelMapping<TDomainModel, TViewModel>();
}
}
public class ViewModelMapping<TDomainModel, TViewModel>
{
public ViewModelMapping<TDomainModel, TViewModel>
Properties<TDomainPropertyType, TViewModelPropertyType>(
Expression<Func<TDomainModel, TDomainPropertyType>> domainExpr,
Expression<Func<TViewModel, TViewModelPropertyType>> viewModelExpr)
{
// map here
return this;
}
}
You don't have to specify all previously set generic types because they are already remembered as generic parameters of returned type. Generic parameters for Properties
method call can be skipped because they will be inferred by compiler. And you get better typing than using object
s everywhere.
Of course that's the simplest version. You can pass much more information between these types, because you specify how next necessary type is created.
It also make calling MapViewModel
without calling MapDomainModel
first impossible (as soon as you make the constructors internal
and close everything in separate dll), what should be a good thing.