CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Set default values in Editable Child Collection bound to DataGridView

rated by 0 users
Answered (Not Verified) This post has 0 verified answers | 4 Replies | 0 Followers

Top 100 Contributor
68 Posts
superkuton posted on Sat, Jul 21 2012 1:08 AM

I have read several blog posts and the e-books but I cant really implement it properly.

What are the basics of setting default property values of an object in an Editable Child Collection, which is bound to a DataGridView?

 

All Replies

Top 500 Contributor
15 Posts

Your collection will have an AddNewCore method which will call a factory method on your child object when a new instance is added to the list.

That factory method makes the addition of an object in this case work the normal way; there is code in your child that gets called when a new object is created (called by the child data portal) and that is where you do your defaulting. Depending on your version of CSLA and data access decisions it could be in Child_Create method.

Andrew H; Bucks, UK

Top 10 Contributor
1,772 Posts
Suggested by JonnyBee

I typically define default values on the static PropertyInfo and then set default values in Child_Create method.

When defautvalues is defined in PropertyInfo´s - the default value vill be set in the first Property Get method on a "managed" field. 
For private backing fields you must set the default value - preferrably in Child_Create or use private field initializer. 

Jonny Bekkum, Norway CslaContrib Coordinator

Top 100 Contributor
68 Posts

Thank you RockyRocks and JonnyBee.

Kindly take a look on my code and see what I might be missing:

In my Editable Root Object:

        public static readonly PropertyInfo<PayableDetail> PayableDetailsProperty = RegisterProperty<PayableDetail>(c => c.PayableDetails);
        public PayableDetail PayableDetails
        {
            get
            {
                if (!(FieldManager.FieldExists(PayableDetailsProperty)))
                    LoadProperty(PayableDetailsProperty, DataPortal.CreateChild<PayableDetail>());
                return GetProperty(PayableDetailsProperty);
            }
            private set { LoadProperty(PayableDetailsProperty, value); }
        }

In my Editable Child Collection:

         public static PayableDetail NewPayableEditDetail()
        {
            return DataPortal.CreateChild<PayableDetail>();
        }

And in my Editable Child Object:

        [RunLocal]
        protected void Child_Create()
        {
            LoadProperty<int>(PayableIDProperty, 1);
            LoadProperty<string>(ProjectProperty, Budgeting.ProjectLookUp.DefaultValue());
            LoadProperty<int>(AccountIDProperty, 101);
       
            BusinessRules.CheckRules();
           
        }

 

Please advise. Thank you.

Top 10 Contributor
1,772 Posts
JonnyBee replied on Sun, Jul 22 2012 12:16 AM

I don't understand your comment on "In my Editable Child Collection" - it seems to me like your code is a root object that uses a lazy loaded editable child. I do not see a collection beeing used here at all!!!

I´d first check what the values of PaymentDetail actually is. It could be your databing is not correct or not updating correctly. 

Possible resons and tings to check out:

1. PayableDetail field may alreade be set. (ie FieldManager.Exists returns true). I field is set to null or anyt value then FieldManager.Exists will return true).

2. AuthorizationRule on PayableDetails property may not allow property get (in which case get  retun null instead) 

3. PayableDetail object may have authorization rules thet does not allow user to read values (in which case the default value for datatype is returned).

3. I assume  the properties in PayableDetail are managed properties (no private backing fields).

4. I prefer to have lazy loading of properties like this (without property set method):

public static readonly PropertyInfo<PayableDetail> PayableDetailsProperty = RegisterProperty<PayableDetail>(c => c.PayableDetails);
public PayableDetail PayableDetails
{
   get
   {
        if (!(FieldManager.FieldExists(PayableDetailsProperty)))
             SetProperty(PayableDetailsProperty, PayableDetail.NewPayableEditDetail());
        return GetProperty(PayableDetailsProperty);
    }
}

Jonny Bekkum, Norway CslaContrib Coordinator

Page 1 of 1 (5 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems