I am finding this most challenging. Lets say I have the following entity:
public class AccountName : IAccountName, IEntity
{
public string firstName { get; private set; }
public string lastName { get; private set; }
public string birthDate { get; private set; }
public string mi { get; private set; }
public int? chartNumber { get; private set; }
public AccountName(string lastName, string firstName, DateTime birthDate)
{
this.lastName = lastName;
this.firstName = firstName;
this.birthDate = birthDate.ToShortDateString();
}
}
Defined with the following interface:
public interface IAccountName
{
string lastName { get; }
string firstName { get; }
string birthDate { get; }
string mi { get; }
int? chartNumber { get; }
}
Lets further say that I have the following "AccountNameModel" holding all the operations for the AccountName entity:
public class AccountNameModel<T> : IModel
where T : IAccountName, IEntity
{
private readonly T from;
private readonly T to;
public AccountNameModel(T from, T to)
{
this.from = from;
this.to = to;
}
public async Task ChangeAccountNameAsync()
{
var bR = new BillingRepository();
await bR.ChangeAccountNameAsync(from, to);
}
}
Now, I would like to add something by way of inheritance to the AccountNameModel that either creates or enforces the concept that the AccountNameModel has the following two constructors:
public class AccountNameModel<T>: ISOMETHING??<AccountNameModel<T>>, IModel
{
private readonly T t;
public AccountNameModel() { }
public AccountNameModel(T t)
{
this.t = t;
}
}
That is, I need ISOMETHING to either create or enforce the fact that the AccountNameModel has a new constructor with and without the generic class T. A simple interface fails as an instance constructor is not allowed in an interface. So how can this be done? (I keep thinking of T4 template, but I am pretty bad at T4's). Can an abstract class do this (as I have many similar "models")?
Any help is most appreciated. TIA