I am completely new to async/await and have been "improving" a totally synchronous app I wrote two years ago. I have the following situation that I am unhappy with my solution.
The XAML is bound to the following two classes:
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"); }
}
The classes, PrimaryInsurance, and SecondaryInsurance inherit from the abstract class Insurance:
abstract class InsuranceEditor : SimpleViewModelBase, ISave, IDataErrorInfo
{
.................
// Constructor
public InsuranceEditor(PatientDemographicsEditor patient, Billing service)
{
...
}
Factory pattern used for asynchronous construction of 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;
............................
}
}
Logically, both primary and secondary insurance only differ in their Editor_Id
, so it seemed natural to inherit from a common InsuranceEditor
. The problem now comes with "correctly" applying async/await to the Primary and Secondary Insurance. I would like usage to be something like:
PrimaryInsurance = await PrimaryInsurance.Create(....)
where Create(...)
is recognized as a static method of PrimaryInsurance
(not necessarily a static method of the abstract class InsuranceEditor
.)
Can this be done?
Edit #1. After posting this question, I'm thinking it may have already been asked, What's the correct alternative to static method inheritance? and that what I want can't be done in C#. Is this correct?
Edit #2: The problem I am having in VS comes with the usage statement:
PrimaryInsurance = await PrimaryInsurance.Create(Patient, BILLING);
VS tells me that:
Member 'InsuranceEditor.Create(PatientDemographicsEditor, Billing)' cannot be accessed with an instance reference; qualify it with a type name instead
And then if I allow VS to make the Create(...) (so that there are no errors), it makes this in the abstact InsuranceEditor class, not the PrimaryInsurance Class.
internal Task<InsuranceEditor> Create(PatientDemographicsEditor patient, Billing bILLING)
{
throw new NotImplementedException();
}
What am I doing wrong?