【发布时间】:2016-07-01 18:14:38
【问题描述】:
我对 async/await 完全陌生,并且一直在“改进”我两年前编写的一个完全同步的应用程序。我有以下情况,我对我的解决方案不满意。
XAML 绑定到以下两个类:
XAML
private InsuranceEditor _primaryinsurance;
public InsuranceEditor PrimaryInsurance
{
get { return _primaryinsurance; }
set { if (_primaryinsurance == value) return; _primaryinsurance = value; OnPropertyChanged("PrimaryInsurance"); }
}
private InsuranceEditor _secondaryinsurance;
public InsuranceEditor SecondaryInsurance
{
get { return _secondaryinsurance; }
set { if (_secondaryinsurance == value) return; _secondaryinsurance = value; OnPropertyChanged("SecondaryInsurance"); }
}
PrimaryInsurance 和 SecondaryInsurance 类继承自抽象类 Insurance:
abstract class InsuranceEditor : SimpleViewModelBase, ISave, IDataErrorInfo
{
.................
// Constructor
public InsuranceEditor(PatientDemographicsEditor patient, Billing service)
{
...
}
用于异步构造 PrimaryInsurance 的工厂模式 (Async OOP Constructor)
private async Task<PrimaryInsurance> InitializeAsync()
{
// asyncData = await GetDataAsync();
return this;
}
public static Task<PrimaryInsurance> Create(PatientDemographicsEditor patient, Billing service)
{
var ret = new PrimaryInsurance(patient, service);
return ret.InitializeAsync();
}
// Constructor
private PrimaryInsurance(PatientDemographicsEditor patient, Billing service)
: base(patient, service)
{
Editor_Id = 1;
........
}
class SecondaryInsurance : InsuranceEditor
{
// Constructor
private SecondaryInsurance(PatientDemographicsEditor patient, Billing service)
: base(patient, service)
{
Editor_Id = 2;
............................
}
}
从逻辑上讲,主要和次要保险仅在Editor_Id 上有所不同,因此从共同的InsuranceEditor 继承似乎很自然。现在的问题是“正确地”将 async/await 应用于主要和次要保险。我希望用法类似于:
PrimaryInsurance = await PrimaryInsurance.Create(....) 其中Create(...) 被识别为PrimaryInsurance 的静态方法(不一定是抽象类InsuranceEditor 的静态方法。)
这个可以吗?
编辑#1。发布这个问题后,我想它可能已经被问过了,@ 987654322@ 并且我想要的东西不能在 C# 中完成。这是正确的吗?
编辑 #2:我在 VS 中遇到的问题来自使用说明:
PrimaryInsurance = 等待 PrimaryInsurance.Create(Patient, BILLING);
VS 告诉我:
成员 'InsuranceEditor.Create(PatientDemographicsEditor, Billing)' 无法通过实例引用访问;用类型限定它 改名
然后,如果我允许 VS 创建 Create(...)(这样就不会出现错误),它会在抽象的 InsuranceEditor 类中进行,而不是在 PrimaryInsurance 类中。
internal Task<InsuranceEditor> Create(PatientDemographicsEditor patient, Billing bILLING)
{
throw new NotImplementedException();
}
我做错了什么?
【问题讨论】:
-
如果它们的区别只是
Editor_Id,为什么不只使用一个类并将Editor_Id 作为参数传递给私有构造函数呢?然后你就做await InsuranceEditor.CreatePrimary(...)或await InsuranceEditor.CreateSecondary(...) -
@ScottChamberlain 这正是我写完问题后的想法。但我可以看到其他情况会更困难,所以我希望有一些关于如何在这种情况下通过子类上的静态 Create 使用继承的想法。
-
我不太确定你的问题是什么。您问“可以完成”,但看起来您的问题中有一个可行的示例。你的问题到底是什么?
-
必须始终在声明它们的类型上调用静态成员,而不是子类型。所以如果你想打电话给
BaseClass.Create()而不仅仅是Create()。使用实例方法,您可以在子类中调用Create(),因为它也是BaseClass的实例,因此它具有相同的公共和受保护实例成员可用,但静态成员绑定到类型(想想 @ 987654340@ object kinda) 不是那种类型的实例。 -
@JamesFaix: “静态成员必须始终在声明它们的类型上调用,而不是子类型”——这不是真的。例如。给定
class A { public static void M() { } }和class B : A { },您可以致电B.M();。在A中声明的M()方法绝对是B的有效static 成员,可以这样引用。
标签: c# oop asynchronous