webworm
webworm

Reputation: 11019

Preferred way to pass a property between classes

I have an application where I am trying to keep data access and UI separate. To that end I have two projects, one for the UI and one for data access. The problem I am having is how to efficiently pass data that is obtained from the config file in the UI project to classes in the data access project.

The data access project contains two classes ...

  1. PaymentProcessor.cs
  2. Payment.cs

    The PaymentProcessor class takes care of making a Web API request to obtain a collection of "Payment" objects. I need to pass a string to the Payment object so that it can use the string to see if a specific pattern is present in the payment.

    What is the best way to pass this string into the Payment class? Do I create a property on the PaymentProcessor class and then pass as a property on the Payment class? Seems like I am playing "pass the property". Is there a better way to do this?

Here is some sample code to show how I am using the classes and how I am passing the "ValidationString".

// Code to execute in UI
var payProccessor = new PaymentProcessor();
payProccessor.ValidationStringToPass = "ABCDEFG";
payProccessor.ProcessPayment();

public class PaymentProcessor()
{
  public string ValidationStringToPass {get; set;}

  public void ProcessPayment()
  {
    // Get payment object collection
    List<Payment> paymentCollection = GetPayments();

    foreach(Payment p in paymentCollection)
    {
      p.ValidationString = this.ValidationStringToPass;
      p.DoStuff();
    }
  }
}

public class Payment
{
  string ValidationString {get; set;}

  public void DoStuff()
  {
    // do stuff with the ValidationString
  }
}

Upvotes: 0

Views: 104

Answers (1)

Bilal Bashir
Bilal Bashir

Reputation: 1493

Here is one way to do it using DependencyProperties and bindings.

Make your payment class a DependencyObject

public class Payment : DependencyObject
{
    public static readonly DependencyProperty ValidationStringProperty =
             DependencyProperty.Register("ValidationString", typeof(string), typeof(Payment), new PropertyMetadata(new PropertyChangedCallback(OnValidationStringChanged)));
    public string ValidationString
    {
        get { return (string)this.GetValue(Payment.ValidationStringProperty); }
        set
        {
            this.SetValue(Payment.ValidationStringProperty, value);
        }
    }

    private static void OnValidationStringChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        // do stuff all that stuff you wanted to do ValidationString
    }
}

Then have PaymentProcessor implement INotifyPropertyChanged

public class PaymentProcessor : INotifyPropertyChanged
{
    public PaymentProcessor()
    {
    }
    private string _validationStringToPass; 
    public string ValidationStringToPass 
    {
        get { return _validationStringToPass; }
        set
        {
            _validationStringToPass = value;
            OnPropertyChanged("ValidationStringToPass");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
    //Get your payments here, might want to put this in the constructor
    public void GetPayments()
    {
        Binding bind = new Binding() { Source = this, Path = new PropertyPath("ValidationStringToPass") };
        Payment pay = new Payment();
        BindingOperations.SetBinding(pay, Payment.ValidationStringProperty, bind);
        Payment pay1 = new Payment();
        BindingOperations.SetBinding(pay1, Payment.ValidationStringProperty, bind);
    }
}

Then in your UI code you can do the following

    var payProccessor = new PaymentProcessor();
    payProccessor.GetPayments();
    payProccessor.ValidationStringToPass = "ABCDEFG";

and when ever you change your ValidationStringToPass property in your PaymentProcessor it will update the ValidationString property in your Payments that you bound to in the GetPayments method.

Upvotes: 1

Related Questions