0

I have a domain interface ITransformation with several implementing classes such as RemoveRowsTransformation and AddColumnTransformation. The inheritance tree models the command pattern. The ITransformation interface defines an Execute(table). Each implementing class contains different properties it needs to implement the method. For example:

public class RemoveRowsTransformation : ITranformation {
    public int TopRowsCount { get; private set; }
    public int BottomRowsCount { get; private set; }
    //snip
}

public class AddColumnTransformation : ITranformation {
    public string ColumnName { get; private set; }
    //snip
}

Various domain methods either require or create an IEnumerable<ITransformation>. This enumerable has to be presented in the view (sent as JSON over web API, specifically) for viewing and editing. How to most efficiently present the data in the enumerable of transformations (inheritance tree) in the view or elsewhere?

Currently, each concrete transformation is creating a viewmodel. This doesn't sit well with me, because this way the domain is tied to the view layer. Also, if a different view is necessary or a different model is used for saving the data into database, a specific method in the domain will have to be made for each conversion.

An alternative approach I've used is to create an interface which particular models will inherit and domain models filling those interfaces. This seems cleaner, but still leads to a large, unmanageable amount of IDomainClassModel and implementations for every single thing imaginable.

1

What if you injected a dictionary into your view that maps from a concrete type of ITransformation to the appropriate transformation view? You can iterate over your IEnumerable<ITransformation>, call GetType() on each and instantiate the correct view for each using the dictionary.

There are a number of benefits to this approach:

  • Domain isn't coupled to presentation layer
  • Can optionally reuse the same view for multiple concrete implementations of ITransformation without duplicate code or messy design
  • Can specify different dictionaries depending on context (edit vs view, summary view, detailed view, focused view, etc.)
1
  • I like having static typing, but I guess there's no proper way to have my cake and eat it too this time. – bklaric Jul 27 '17 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.