user2487466
user2487466

Reputation: 35

Modelling Lookup Values in Domain

In domain driven design, I have the following situation.

I have a Vehicle model. The vehicle can be of type HatchbackCar, SedanCar, Truck etc.

The VehicleType is stored in a database lookup table.

Question is:

How do I model the Domain?

Do I Model it like the following:

public class Vehicle
{
    public int VehicleId{Get;Set;}
    **public int VehicleTypeId { get; set; }**
    public string MakeCode { get; set; }
    public string ModelCode { get; set; }
    public int Power { get; set; }
    public int Weight { get; set; }
    public int PowerToWeight { get { return Power/Weight*100; } }     

}

OR

public class Vehicle
{
    public int VehicleId{Get;Set;}
    **public VehicleType VehicleType { get; set; }**
    public string MakeCode { get; set; }
    public string ModelCode { get; set; }
    public int Power { get; set; }
    public int Weight { get; set; }
    public int PowerToWeight { get { return Power/Weight*100; } }     

}

AND

public class VehicleType
{
    public int VehicleTypeId{Get;Set;}
    public string Description{Get;Set;}   

}

If I use the 2nd way, at what stage do I populate the VehicleType model.

Thanks.

Upvotes: 1

Views: 84

Answers (2)

Sven Schelfaut
Sven Schelfaut

Reputation: 552

Can't you use an enum for VehicleType instead of using the id? Your model can then contain the enum, which can be mapped to the database id afterwards. I assume you need the descriptions for showing them in a dropdown or something like that? This makes the VehicleType class a good candidate for a separate read model I guess.

Upvotes: 0

Ilya Palkin
Ilya Palkin

Reputation: 15727

If I use the 2nd way, at what stage do I populate the VehicleType model.

For both ways VehicleType should be set when you create a new instance of 'Vehicle', either VehicleTypeId or VehicleType.

  1. If Vehicle refers to VehicleTypeId then you set specified identifier.
  2. If Vehicle refers to VehicleType then you set an instance of VehicleType. First it is needed to load the instance of VehicleType with specified identifier

Usually I use the second approach since it describes domain model in terms of entities and their relations.

Upvotes: 1

Related Questions