I'm looking for a way to create a read only (or immutable) version of an entity that I will be using as a reference object. e.g.
class Order
{
public int OrderId {get; private set;}
public virtual Product ProductOrdered{get;set;}
public int ProductId {get;set;}
public int Quantity{get;set;}
}
class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public string Category { get; set; }
}
in the example above Product is supposed to be used as a reference data. i.e. Product should not be modified by code using product as a reference in the Order.
Now, I realize one way to enforce that is to make all the setters as private and no one would be able to change Product...
class ProductReadOnly
{
public int ProductId { get; private set; }
public string Name { get; private set; }
public string Category { get; private set; }
}
but somewhere in my app i'll like the admins to be able to create/modify products. So, I could use the class Product (with public setters)
Now the questions are (assuming I'm able to map Product & ProductReadOnly to the product table using Table attribute)
- will i be able to use ProductReadOnly as a navigation property in the Order class?
- Can I load Product & ProductReadOnly (same product) in the same data context?
- is there an easier way to do it (Detached object?)
- any issues with this approach?