0

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

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Alan Wayne
  • 5,122
  • 10
  • 52
  • 95
  • @PavelAnikhouski Thanks for the reference, I may have to do something similar--but it feels different from my question -- and does not seem to allow for a generic parameter to be assigned to the generic model. (I could be wrong here--more study is needed). Thanks. – Alan Wayne Jan 20 '20 at 19:31
  • 3
    The only generic constraint related to constructors is `new()`, meaning the type has to have a default constructor. That's the only possible constructor constraint. –  Jan 20 '20 at 19:34
  • @PavelAnikhouski After some further study, your reference helps. Bottom line--What I want can't be done. Thanks. – Alan Wayne Jan 20 '20 at 19:50

0 Answers0