I have a rich client with quite complex domain logic and I want to keep the domain and presentation layer separated. How would you advice to intergrate the domain logic with the UI in this case.
I implemented in the domain objects and services in following fashion
public class ProductOrder
{
public double SomeParameter {get; private set;}
public IReadOnlyCollection<ManufactureTime> ManufactureTimeNorms { get; }
public IReadOnlyCollection<ProductOrderMaterial> MaterialItems { get; }
public void SetSomeParameter(double value)
{
SomeParameter = value;
RecalculateMaterials(); // modifies domain object
RecalculateTimeToManufacture();
RecalculatePrice();
}
}
Use case
From UX perspective, the workflow is:
User creates product order by selecting a product from calatog.
- Parameters are copied from catalog to order and based on domain logic material and time needed to manufacture is calculated.
- User sees the calculated values
User manually changes some parameters
- Based on domain logic material and time needed to manufacture is calculated.
- User sees the recalculated values
Manually edits the calculated parameters
User clicks save and the parameters and calculates values are persisted in database.
The problem:
Typically, there is a ViewModels are constructed from a domain objects, presented in the UI where user can make changes and when saving, the domain objects are updated.
However, this does not solve the problem of applying business rules and presenting modified domain object back in the UI. How to design two way interaction between viewmodels and the domain?
Since the business logic is fairly complex, I would like to avoid duplicating all business rules in ViewModel.
What I have:
I don't have anything yet, but I would start with this
public class ProductOrderToViewModelMapper
{
public void MapToViewModel(ProductOrder domainObject, ProductViewModel viewModel)
{ ... }
public void MapToDomainObject(ProductViewModel viewModel, ProductOrder domainObject)
{ ... }
}