Using BusinessBase object as Child property

Using BusinessBase object as Child property

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


Frances83 posted on Sunday, August 09, 2009

Hi all,

I'm using CSLA for Silverlight v3.7.0.0.

My situation is the following. I have a class which looks like this:

namespace MyNameSpace
{
    using System;

    using Csla;
    using Csla.Serialization;

    [Serializable]
    public class FormattedNumber : BusinessBase<FormattedNumber>
    {
        private static PropertyInfo<string> numberProperty = RegisterProperty<string>(c => c.Number, "Number", String.Empty);       
        private static PropertyInfo<string> numberFormatProperty = RegisterProperty<string>(c => c.NumberFormat, "NumberFormat", String.Empty);

        public FormattedNumber()
        {
        }

        public string NumberFormat
        {
            get
            {
                return GetProperty(numberFormatProperty);
            }

            set
            {
                SetProperty(numberFormatProperty, value);
            }
        }

        public string Number
        {
            get
            {
                return GetProperty(numberProperty);
            }

            set
            {
                SetProperty(numberProperty, value);
            }
        }
    }
}


And another class which uses the FormattedNumber like this:

namespace OtherNameSpace
{
    using System;

    using Csla;
    using Csla.Serialization;

    [Serializable]
    public partial class MyNumberEdit : BusinessBase<MyNumberEdit>
    {
        private static PropertyInfo<
FormattedNumber > myNumberNumberProperty = RegisterProperty(new PropertyInfo<FormattedNumber >("MyNumber"));

        public FormattedNumber MyNumber
        {
            get
            {
                if (!FieldManager.FieldExists(
myNumberNumberProperty ))
                {
                    LoadProperty(
myNumberNumberProperty , new FormattedNumber());
                }

                return GetProperty(
myNumberNumberProperty );
            }
        }       
    }
}


This code compiles and runs just fine, but when I load it into a dialog using the CslaDataProvider:

<CSLA:CslaDataProvider x:Key="MyNumberData" ManageObjectLifetime="True" IsInitialLoadEnabled="True" Saved="CslaDataProvider_Saved" ObjectType="OtherNameSpace.MyNumberEdit, OtherNameSpace" />

The CanSave-property is set to True, even though nothing has been edited, and when the Save method is called I get this error:

Object not serializable (MyNameSpace.FormattedNumber)

Which is strange since the class does have the SerializableAttribute.

If anyone can help me with this issue I would be much obliged.

Cheers,
Frances

xAvailx replied on Monday, August 10, 2009

Not sure about the error, but, for the FormattedNumber class, you could look at SmartDate as they look to be similar concepts. Also, is there a reason FormattedNumber inherits BusinessBase? It doesn't seem to be doing anything that would require use of BusinessBase.

HTH

Frances83 replied on Monday, August 10, 2009

I cut down the code for demonstration's sake, but the FormattedNumber derives from BusinessBase since it also has Validation.

RockfordLhotka replied on Tuesday, August 11, 2009

I am not sure why you are getting a serialization exception, that seems odd.

But you will certainly run into metastate issues with IsDirty, etc, because you aren't marking the child object as a child.

You either need to use the child data portal to create the child object, or add a constructor to explicitly call MarkAsChild() - either way, something needs to indicate that it is a child object or the metastate management won't function as you expect.

Frances83 replied on Tuesday, August 11, 2009

Fixed. It turned out I was linking against the wrong projects (FormattedNumber resides in a different assembly, which is split up into a Client and a Server project).

My new question is: when the parent object is validated, the validation rules on the child objects aren't called. How can I force that?

Thanks,
Frances

xAvailx replied on Wednesday, August 12, 2009

How are you determining the validation rules aren't being called? You won't see them in the parent's business rules collection.

I've worked on a similar scenario in the past and don't' recall having to do anything special as long as it was a managed property. I think csla will look at the properties defined and based on its type/basetype will try to work its magic.

If you don't get more responses I would recommend creating a new post with your new issue.

HTH.

Frances83 replied on Wednesday, August 12, 2009

I use breakpoints Wink [;)]

I'll put this in a new topic, thanks.

Copyright (c) Marimer LLC