Edit: You can always let the behavior of value types take care of this for you. Change PaymentDetails for structure instead of class:
public struct PaymentDetails { public int Id { get; set; } public string Status { get; set; } } public class PaymentHelper { public PaymentDetails Details { get; set; } }
If you then try
ph.Details.Status = "Some status";
You will get a compiler error telling you that you cannot do this. Since the return values ββof value types are, well, by value, you cannot change the .Status property.
Or...
If PaymentDetails and PaymentHelper declared in the same class library (separately from the code that you want to prevent writing to the .MyPaymentDetails property, you can use:
public class PaymentDetails { public int Id { get; internal set; } public string Status { get; internal set; } } public class PaymentHelper { public PaymentDetails Details { get; private set; } }
which will prevent any message declared outside this class library from being .Id to .Id or .Status .
Or, launch access to .Id and .Status to go through the helper class, instead of allowing access to access the .Details property:
public class PaymentHelper { private PaymentDetails _details; public string Id { get { return _details.Id; } private set { _details.Id=value; } } public string Status { get { return _details.Status; } private set { _details.Status = value; } } }
Of course, if you are going to do this, you can simply
public calss PaymentDetails { public int Id { get; protected set; } public string Status { get; protected set; } } public class PaymentHelper : PaymentDetails { }
... assuming that this type of inheritance matches the rest of your architecture.
Or just to illustrate the interface proposal suggested by @MrDisappointment
public interface IDetails { int Id { get; } string Status { get; } } public class PaymentDetails : IDetails { public int Id { get; private set; } public string Status { get; private set; } } public class PaymentHelper { private PaymentDetails _details; public IDetails Details { get { return _details; } private set { _details = value; } } }