DataPortal Child_Create

DataPortal Child_Create

Old forum URL: forums.lhotka.net/forums/t/10855.aspx


comp1mp posted on Tuesday, November 08, 2011

The following code results in creating a child object in a BusinessLIst that is the child of a Root BusinessBase object.

 

private void Child_Create(Enums.PatientContactType type)

{  

   LoadProperty<Enums.PatientContactType>(TypeProperty, type);           

}

 

I can inspect the child object at runtime, and all looks good. All is not well however. The childobject has only a single validation rule implemented on a property with the [Required] attribute, Yet, IsValid returns true for the root object, even though the property value is an empty string.

Changing my Child_Create code to the following fixes the issue.

private void Child_Create(Enums.PatientContactType peType)

{

   base.Child_Create();

   LoadProperty<Enums.PatientContactType>(seTypeProperty, peType);           

}

I am confused by this. Why is the call to the base method required? 

I am using Csla 3.8.3, and have the videos and the ProjectTracker reference app. There are several instances in Project tracker of Child_Create methods (in the context of BusinessList) that do not call the base method. ProjectResource specifically.

I have implemented a custom PropertyInfoFactory, and I am concerned that my implementation may be causing this?? I walke dthrough the framework code, and the rule is successfully being created.

StefanCop replied on Wednesday, November 09, 2011

1. I think the base.Child_Create() should be invoked after the properties are set. The base Child_Create() let the rules run.

private void Child_Create(Enums.PatientContactType peType)

{  

   LoadProperty<Enums.PatientContactType>(seTypeProperty, peType);     

   base.Child_Create();

}

2. I don't know v3.8, but maybe this version doesn't support Validation Attributes ([Required]) ?

 

JonnyBee replied on Wednesday, November 09, 2011

1. This is the code in Child_Create and DataPortal_Create:

    protected virtual void Child_Create()
    {
      ValidationRules.CheckRules();
    }

    protected virtual void DataPortal_Create()
    {
      ValidationRules.CheckRules();
    }

and should be called after you have set the property values (using LoadProperty or BypassPropertyChecks + set property).

2.  In your AddBusinessRules you must also call base.AddBusinessRules to call this (from Csla.BusinessBase.cs) :

    protected virtual void AddBusinessRules()
    {
      ValidationRules.AddDataAnnotations();
    }

in order to add the DataAnnotation rules to the registered BusinessRules of your BO type.

3. The DataAnnotation rules must be defined on the property and not the PropertyInfo. This is the correct version:

    public static PropertyInfo<string> CustomerNameProperty = RegisterProperty(new PropertyInfo<string>("CustomerName""Customer name"));
    
    [Required]
    public string CustomerName
    {
      get { return GetProperty(CustomerNameProperty); }
      set { SetProperty(CustomerNameProperty, value); }
    }

 

comp1mp replied on Thursday, November 10, 2011

Teach a person to fish :). Next time I will look at the framework code prior to asking, as it is clear now why one must call the base implementation.

I just noticed the business child template provides this guidance  in its override of Child_Create(). I deleted the override in this particular class however as my BusinessObject required parameterized creation. Thinking about it now, I should still be overriding and throwing an exception to prevent child creation without the parameter :).

As far as the ProjectTracker reference app, I am assuming 3.5 did not require the call to the base implementation. As I said in the OP, ProjectResource does not call the base.Child_Create(). There have been a few other instances in my experience where looking at the sample refrence apps has resulted in confusion.

I wonder if the samples should be versioned?

Thanks for the answers!  

Copyright (c) Marimer LLC