bltoolkit

BLToolkit: Public read-only properties


Assume I have a business object that has some properties that should be read-only. For example:

public class Order
{
     ...
     public DateTime OrderDate { get; set; }
     ...
     public decimal OrderTotal { get; set; }
}

Also assume that OrderTotal is a calculated value returned by a stored proc that cannot be set by the application (whereas OrderDate can). Normally I would simply write the OrderTotal without a public setter:

public decimal OrderTotal { get; private set; }

However if I do so BLToolkit will no longer set the value of this property. I also already tried to write this as an internal property and defining BLToolkit as a friend assembly (InternalsVisibleTo("BLToolkit.4, PublicKey=xyz")) with no success.

How can I write a property without a public setter that can still be populated by BLToolkit?


Solution

  • There is a Storage Property on the MapField Attribute, maybe that will help

        public class Class1
        {
            int _int32 = 0;
            [MapField(Storage = "_int32")]
            public int Int32
            {
                get { return _int32; }
            }
        }