I am looking for a solution to store immutable data in my code instead of the db. In the specific case I want to deal with units. Here an example for units of weight (they won't change, so its ok to store them in my code):
public class Unit
{
public Unit() { }
public Unit(string name, string symbol, double factor, Unit baseUnit, UnitType unitType)
{
this.Name = name;
this.Symbol = symbol;
this.Factor = factor;
this.BaseUnit = baseUnit;
this.UnitType = unitType;
}
public int Id { get; set; }
public string Name { get; set; }
public UnitType UnitType { get; set; }
public string Symbol { get; set; }
public string NamePlural { get; set; }
public Unit BaseUnit { get; set; }
public double Factor { get; set; }
}
public static class TimeUnits
{
public static Unit second = new Unit("second", "s", 1, null, UnitTypes.Time);
public static Unit microsecond = new Unit("microsecond", "μs", 0.000001, second, UnitTypes.Time);
public static Unit millisecond = new Unit("millisecond", "ms", 0.001, second, UnitTypes.Time);
public static Unit minute = new Unit("minute", "min", 60.0, second, UnitTypes.Time);
public static Unit hour = new Unit("hour", "h", 3600.0, second, UnitTypes.Time);
public static Unit day = new Unit("day", "d", 24.0, hour, UnitTypes.Time);
public static Unit week = new Unit("week", "w", 7, day, UnitTypes.Time);
}
As said I do not want to store these immutable information in the db in order to avoid additional unions for ef-core to do when retrieving data from the db.
In the case of gender I simply use an enum:
public enum Gender
{
male = 1,
female = 2,
not_applicable = 9,
dont_want_to_share = 10
}
I would like to have a similar solution for the units. But an enum has only an Id and a Name. For things like the units shown above or other cases I need additional properties (ex. factore, unitType etc.). Thanks a lot for any hint on how I could achieve this so that ef core loads those values like it does with enums.