1

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?

matk
  • 1,528
  • 2
  • 14
  • 25

2 Answers2

4

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; }
        }
    }
David DV
  • 674
  • 4
  • 9
  • That seems to do it, thanks! Where did you learn about this property? The BLToolkit documentation seems to be silent on this. – matk Mar 13 '12 at 13:27
  • I learned about it on the Blt discussion group http://groups.google.com/group/bltoolkit/browse_thread/thread/108cc68e68333af2 But yeah it's not mentioned in the documentation, but there is a unit-test for it in the MemberMapperTest class – David DV Mar 14 '12 at 11:33
  • Thanks, appreciate it. I love BLToolkit, but the documentation really needs improvement. – matk Mar 15 '12 at 09:13
0

I don't think you can do that, but if I understand you correctly you don't need it. If OrderTotal is returned from stored procedure than it is just fine if you leave it as it is. Unless Order class is representing the actual Order table in the database, you won't have any problem if you accidentally update OrderTotal.

Mladen Macanović
  • 1,099
  • 7
  • 17
  • That's true, but I think it's just not a sound architecture if I have to change the logical layout of my class just to satisfy the ORM, especially if it's publicly visible. I'm wondering though why I can't get the internal/InternalsVisibleTo approach to work? – matk Mar 12 '12 at 07:44