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.

asked Jul 26, 2017 at 10:56

1 Answer 1

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.)
answered Jul 26, 2017 at 20:34
1
  • I like having static typing, but I guess there's no proper way to have my cake and eat it too this time. Commented Jul 27, 2017 at 7:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.