在 类 之间传递 属性 的首选方式
Preferred way to pass a property between classes
我有一个应用程序,我试图将数据访问与 UI 分开。为此,我有两个项目,一个用于 UI,一个用于数据访问。我遇到的问题是如何有效地将从 UI 项目中的配置文件中获取的数据传递到数据访问项目中的 classes。
数据访问项目包含两个 classes ...
- PaymentProcessor.cs
Payment.cs
PaymentProcessor class 负责发出 Web API 请求以获取 "Payment" 对象的集合。我需要将一个字符串传递给付款对象,以便它可以使用该字符串来查看付款中是否存在特定模式。
将此字符串传递到付款 class 的最佳方法是什么?我是否在 PaymentProcessor class 上创建了一个 属性,然后在 Payment class 上作为 属性 传递?好像在玩"pass the property"。有更好的方法吗?
下面是一些示例代码,展示了我如何使用 classes 以及我如何传递 "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
}
}
这是使用 DependencyProperties 和绑定的一种方法。
支付 class 一个 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
}
}
然后让 PaymentProcessor 实现 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);
}
}
然后在您的 UI 代码中,您可以执行以下操作
var payProccessor = new PaymentProcessor();
payProccessor.GetPayments();
payProccessor.ValidationStringToPass = "ABCDEFG";
并且当您更改 PaymentProcessor 中的 ValidationStringToPass 属性 时,它会更新您在 GetPayments 方法中绑定的付款中的 ValidationString 属性。
我有一个应用程序,我试图将数据访问与 UI 分开。为此,我有两个项目,一个用于 UI,一个用于数据访问。我遇到的问题是如何有效地将从 UI 项目中的配置文件中获取的数据传递到数据访问项目中的 classes。
数据访问项目包含两个 classes ...
- PaymentProcessor.cs
Payment.cs
PaymentProcessor class 负责发出 Web API 请求以获取 "Payment" 对象的集合。我需要将一个字符串传递给付款对象,以便它可以使用该字符串来查看付款中是否存在特定模式。
将此字符串传递到付款 class 的最佳方法是什么?我是否在 PaymentProcessor class 上创建了一个 属性,然后在 Payment class 上作为 属性 传递?好像在玩"pass the property"。有更好的方法吗?
下面是一些示例代码,展示了我如何使用 classes 以及我如何传递 "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
}
}
这是使用 DependencyProperties 和绑定的一种方法。
支付 class 一个 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
}
}
然后让 PaymentProcessor 实现 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);
}
}
然后在您的 UI 代码中,您可以执行以下操作
var payProccessor = new PaymentProcessor();
payProccessor.GetPayments();
payProccessor.ValidationStringToPass = "ABCDEFG";
并且当您更改 PaymentProcessor 中的 ValidationStringToPass 属性 时,它会更新您在 GetPayments 方法中绑定的付款中的 ValidationString 属性。