samedi 17 décembre 2022

How to make generic object to accept two different class models

I want to have 2 very similar classes that will have some common behavior, but different properties, so I need to make these two classes Invoice and CreditNote to be under "one generic object" in this case called model

I was thinking to implement a inteface which will be having input object type like:

public interface IInvoice<T>
{
    T InvoiceType { get; set; }
}

But Im not quite sure if this is the way for this. Ultimately, I would like instancing these classes from interface: IInvoice = new Invoice();

Here is an example:


//380-invoice
if(Convert.ToInt32(invoiceType) == InvoiceTypeCodeEnumDto.Invoice.Id)
{
    Invoice model = new Invoice();
    model.RequestId = request.CorrelationId;
    model.SendToCir = "Auto";
}

//383-creditNote
if (Convert.ToInt32(invoiceType) == InvoiceTypeCodeEnumDto.CreditNote.Id)
{
    CreditNote model = JsonConvert.DeserializeObject<CreditNote>(request.Content)!;
    model.RequestId = request.CorrelationId;
    model.SendToCir = "Auto";
}

model.Name = "foo"; // not accesible

Aucun commentaire:

Enregistrer un commentaire