Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The Model is the logical place to house payment methods and the business rules associated with each method. One approach is to use an enumeration that describes each payment method and this is queried using 'switch' statements. </p> <p>An alternate design incorporates the strengths of polymorphism, and might look like this...</p> <pre><code>public class Model { private readonly List&lt;PaymentMethod&gt; _allPaymentMethods; public Model() { // get payment types from the db // to populate master list _allPaymentMethods = new List&lt;PaymentMethod&gt; {new Cash(), new CreditCard()}; } public List&lt;PaymentMethod&gt; GetPaymentMethods(int age) { List&lt;PaymentMethod&gt; result = _allPaymentMethods.Where(q =&gt; q.Age == age).ToList(); return result; } } public abstract class PaymentMethod { public string Name { get; protected set; } public int Age { get; protected set; } public abstract void ProcessPayment(); public override string ToString() { return Name; } } public class CreditCard:PaymentMethod { public CreditCard() { Name = "Credit Card"; Age = 25; } public override void ProcessPayment() { Console.WriteLine("Thanks for using your card"); } } public class Cash:PaymentMethod { public Cash() { Name = "Cash"; Age = 22; } public override void ProcessPayment() { Console.WriteLine("Thanks for paying cash"); } } </code></pre> <p>This sample hard codes two methods: Cash and Credit Card, and each class knows how to do its work while relying upon inheritance to handle the common attributes. This approach avoids the 'switch' and encapsulates all of the business rules and methods within each class. So the Model exposes only what the ViewModel needs to know in order to present the various payment methods to the user in an items control.</p> <p>When the user changes their age, your VM can refresh the list.</p> <p>A code snippet for the VM looks like...</p> <pre><code>public class ViewModel :INotifyPropertyChanged { public ObservableCollection&lt;PaymentMethod&gt; Methods { get; set; } public ViewModel() { Model m = new Model(); Methods = new ObservableCollection&lt;PaymentMethod&gt;(m.GetPaymentMethods(22)); } #region INotifyPropertyChanged Implementation public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string name) { var handler = System.Threading.Interlocked.CompareExchange (ref PropertyChanged, null, null); if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } #endregion } </code></pre> <p>Whichever approach you use (either enumeration or polymorphism), the rule of thumb is "Does the VM absolutely need to know about this? Can I leverage the OO strengths of inheritance and polymorphism into my architecture?"</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload