Not really sure how to explain it in a one-liner but here's my situation.
I have an abstract base class called "Deal" which has a property in it which is of a class "Inventory"
Now a deal can be either Cash, Finance, Lease, Wholesale or Rent-To-Own which is why it's of type Deal. It's also abstract as each of the deal types have some properties that don't apply to other but they all have a set of shared so the various deal types (i.e. - Finance, Lease, etc.) inherit from the base class Deal and have their own added methods and properties.
The same is true with another abstract class of "Inventory". Inventory can be things like car, truck, boat, RV, etc.) so therefore I have a class of Inventory.
Here's the issue:
A deal must contain a unit in inventory. For example, you finance a car or you lease an RV then you have a Deal with an inventory property of type Inventory or whatever the case may be.
When I create the Deal class and I add a property to it like SoldUnit as type Inventory, the T is inherited from the Deal class. How can I create a generic property of type Inventory inside of the Deal class?
Deal won't really work because of the number of possibilities, so how could I make a generic "SoldUnit" property which references an Inventory class inside of the Deal class? And while maintaining strict typing? I know I could make a property called SoldUnit with a type of object or dynamic, but is there a better way?
Here is an excerpt of the code: It doesn't work because type T is already a DealType, but it's the two properties [ ret.Vehicle = new Car(); and ret.SoldUnit = new Car(); ] that I'm trying to populate with a specific inventory type (car, truck, rv, boat, etc.) in the Get() method. The specific inventory type is determined by a flag in the database field.
public abstract class Deal<T>
{
public string Uid { get; set; } = Guid.NewGuid().ToString();
public DealTypes DealType { get; set; } = DealTypes.Finance;
public Buyer Customer { get; set; }
public Inventory<T> Vehicle { get; set; }
public Location Location { get; set; }
public Inventory<T> SoldUnit { get; set; }
public abstract T Add(string id);
public abstract T Update(string id);
public abstract T Get(string id);
}
public abstract class Inventory<T>
{
public string Uid { get; internal set; } = Guid.NewGuid().ToString();
public Location Location { get; set; }
public string StockNumber { get; set; } = "";
}
public class Cash : Deal<Cash>
{
public decimal TradeAllowance { get; set; }
public decimal TradePayoff { get; set; }
public decimal TotalDown { get; set; }
public decimal DueOnDelivery { get; set; }
public override Cash Get(string id)
{
var ret = new Cash();
ret.Customer = new Buyer();
ret.Vehicle = new Car();
ret.Location = new Location();
ret.SoldUnit = new Car();
}
}
public class Car : Inventory<Car>
{
public decimal GrossWeight { get; set; }
public decimal NetWeight { get; set; }
public string Branded { get; set; } = "";
public string FuelType { get; set; } = "";
public string Trim { get; set; } = "";
public string Body { get; set; } = "";
public int Cylinders { get; set; }
public decimal EngineSize { get; set; }
public string TransmissionType { get; set; } = "";
public string Suspension { get; set; } = "";
public string InteriorColor { get; set; } = "";
public bool OdometerExceedsLimit { get; set; }
public bool OdometerTrueMileageUnknown { get; set; }
public bool OdometerFiveDigit { get; set; }
public string TitleNumber { get; set; } = "";
public DateTime? TitleDate { get; set; }
public string TitleState { get; set; } = "";
public DateTime? TitleDue { get; set; }
public DateTime? TitleReceived { get; set; }
}
Is there a way to do this?
Thanks!
What I have tried:
I've tried making the Deal class with two types
public class Deal<TDeal, TInventory>
but then I would have to make a class for every possible combination of deal / inventory type. So far, the only thing I can think of that would work is to make the Vehicle and SoldUnit properties of type object or dynamic but then I don't get the strong typing or the intellisense.
public dynamic Vehicle { get; set; }
public object SoldUnit{ get; set; }