Let's say I have the following C # class that I want to be immutable. You can only install it using a parameterized constructor.
public class InsulineInjection { private InsulineInjection() {
Now I would like to use ORM to create this POCO. However, as far as I can see, all .NET ORM expects properties to be available, and have a public constructor to be able to create this POCO. So I would have to change my POCO to this:
public class InsulineInjection { public InsulineInjection() { } public InsulineInjection(Millilitre millilitre, DateTime dateTime, string remark) { this.Remark = remark; this.DateTime = dateTime; this.Millilitre = millilitre; } public string Remark { get; set; } public DateTime DateTime { get; set; } public Millilitre Millilitre { get; set; } }
This, however, makes my POCO volatile again. Someone who uses it can simply change any property that is not important.
As far as I can see this, I could solve this in two ways:
- Write my own level of data access (or modify orm) to be able to create the correct POCO instances using the constructor I created.
- Create some kind of cartographer. Let ORM create simple DTO objects and use a converter to convert the DTO objects to my POCO at the appropriate time.
I am inclined towards solution 2. Does anyone have an example of how to do this? Or does anyone have a better solution than the ones I described above?
source share