Dataportal returning an empty object to silverlight client

Dataportal returning an empty object to silverlight client

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


mariogib posted on Sunday, March 04, 2012

Hi

I am trying to create a silverlight CSLA application and I have set everything up as instructed in the samples. When I step through the code the data layer gets the object but passes and empty object back to the client. Please help

 

Mario

JonnyBee replied on Sunday, March 04, 2012

Do you use CSLA managed properties? This is highly recommmended in order to use the mobile formatter.

Can you post some of your code? 

mariogib replied on Monday, March 05, 2012

Hi Jonny

I am using the code generated by codesmith and it is using managed properties.

If I use the async methods without the dataportal it returns the correct data.

 

sample below

 

 //------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated using CodeSmith: v6.0.1, CSLA Templates: v3.0.2.1983, CSLA Framework: v4.0.0.
//     Changes to this file will be lost after each regeneration.
//     To extend the functionality of this class, please modify the partial class 'DocumentType.cs'.
//
//     Template path: EditableRoot.Generated.cst
//     Template website: http://code.google.com/p/codesmith/
// </autogenerated>
//------------------------------------------------------------------------------
#region Using declarations

using System;

using Csla;
using Csla.Rules;
#if SILVERLIGHT
using Csla.Serialization;
#else
using Csla.Data;
#endif

#endregion

namespace DocMan.Business
{
    [Serializable]
    [Csla.Server.ObjectFactory(FactoryNames.DocumentTypeFactoryName)]
    public partial class DocumentType : BusinessBase< DocumentType >
    {
        #region Contructor(s)

#if !SILVERLIGHT
        private DocumentType()
        { /* Require use of factory methods */ }
#else
    public DocumentType()
        { /* Require use of factory methods */ }
#endif

        internal DocumentType(System.Guid guid)
        {
            using(BypassPropertyChecks)
            {
                _guid = guid;
            }
        }
        #endregion

        #region Business Rules

        protected override void AddBusinessRules()
        {
            if(AddBusinessValidationRules())
                return;

            BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(_titleProperty));
            BusinessRules.AddRule(new Csla.Rules.CommonRules.MaxLength(_titleProperty, 50));
        }

        #endregion

        #region Properties

        private static readonly PropertyInfo< System.Guid > _guidProperty = RegisterProperty< System.Guid >(p => p.Guid, string.Empty, RelationshipTypes.PrivateField);
        private System.Guid _guid = _guidProperty.DefaultValue;
#if !SILVERLIGHT
  [System.ComponentModel.DataObjectField(true, false)]
#endif
        public System.Guid Guid
        {
            get { return GetProperty(_guidProperty, _guid); }
            set { SetProperty(_guidProperty, ref _guid, value); }
        }

        private static readonly PropertyInfo< System.Guid > _originalGuidProperty = RegisterProperty< System.Guid >(p => p.OriginalGuid, string.Empty, RelationshipTypes.PrivateField);
        private System.Guid _originalGuid = _originalGuidProperty.DefaultValue;
        /// <summary>
        /// Holds the original value for Guid. This is used for non identity primary keys.
        /// </summary>
        internal System.Guid OriginalGuid
        {
            get { return GetProperty(_originalGuidProperty, _originalGuid); }
            set { SetProperty(_originalGuidProperty, ref _originalGuid, value); }
        }

        private static readonly PropertyInfo< System.String > _titleProperty = RegisterProperty< System.String >(p => p.Title, string.Empty, RelationshipTypes.PrivateField);
        private System.String _title = _titleProperty.DefaultValue;
        public System.String Title
        {
            get { return GetProperty(_titleProperty, _title); }
            set { SetProperty(_titleProperty, ref _title, value); }
        }

 


        //AssociatedOneToMany
        private static readonly PropertyInfo< DocumentList > _documentsProperty = RegisterProperty<DocumentList>(p => p.Documents, Csla.RelationshipTypes.Child);
        public DocumentList Documents
        {
            get
            {
                bool cancel = false;
                OnChildLoading(_documentsProperty, ref cancel);
   
                if (!cancel)
                {
                    if(!FieldManager.FieldExists(_documentsProperty))
                    {
#if SILVERLIGHT
                        //MarkBusy();
                        var criteria = new DocMan.Business.DocumentCriteria {DocumentTypeGuid = Guid};
                       

                        if((true))
                        {
                            DocMan.Business.DocumentList.Exists(criteria,(o,e) =>
                            {
                                if (e.Error != null)
                                    throw e.Error;

                                if(!e.Object.Result)
                                DocMan.Business.DocumentList.NewListAsync((o1, e1) =>
                                {
                                    if (e1.Error != null)
                                        throw e1.Error;

                                    this.LoadProperty(_documentsProperty, e1.Object);

                                    //MarkIdle();
                                    OnPropertyChanged(_documentsProperty);
                                });
                                else
                                DocMan.Business.DocumentList.GetByDocumentTypeGuidAsync(Guid, (o2, e2) =>
                                {
                                    if (e2.Error != null)
                                        throw e2.Error;

                                    this.LoadProperty(_documentsProperty, e2.Object.Child);

                                    //MarkIdle();
                                    OnPropertyChanged(_documentsProperty);
                                });
                            });
                        }
#else
                        var criteria = new DocMan.Business.DocumentCriteria {DocumentTypeGuid = Guid};
                       
   
                        if(!DocMan.Business.DocumentList.Exists(criteria))
                            LoadProperty(_documentsProperty, DocMan.Business.DocumentList.NewList());
                        else
                            LoadProperty(_documentsProperty, DocMan.Business.DocumentList.GetByDocumentTypeGuid(Guid));
#endif
                    }
                }

                return GetProperty(_documentsProperty);
            }
        }


        #endregion

#if !SILVERLIGHT
        #region Synchronous Factory Methods

        public static DocumentType NewDocumentType()
        {
            return DataPortal.Create< DocumentType >();
        }

        public static DocumentType GetByGuid(System.Guid guid)
        {
            var criteria = new DocumentTypeCriteria {Guid = guid};
           
           
            return DataPortal.Fetch< DocumentType >(criteria);
        }

        public static void DeleteDocumentType(System.Guid guid)
        {
            var criteria = new DocumentTypeCriteria {Guid = guid};
           
           
            DataPortal.Delete< DocumentType >(criteria);
        }

        #endregion
#endif

        #region Asynchronous Factory Methods

#if SILVERLIGHT
        public static void NewDocumentTypeAsync(EventHandler<DataPortalResult<DocumentType>> handler)
        {
            var dp = new DataPortal<DocumentType>();
            dp.CreateCompleted += handler;
            dp.BeginCreate();
        }

        public static void GetByGuidAsync(System.Guid guid, EventHandler<DataPortalResult< DocumentType >> handler)
        {
            var criteria = new DocumentTypeCriteria{ Guid = guid};
           
           
            var dp = new DataPortal< DocumentType >();
            dp.FetchCompleted += handler;
            dp.BeginFetch(criteria);
        }

        public static void DeleteDocumentTypeAsync(System.Guid guid, EventHandler<DataPortalResult<DocumentType>> handler)
        {
            var criteria = new DocumentTypeCriteria{Guid = guid};
           
           
            var dp = new DataPortal< DocumentType >();
            dp.DeleteCompleted += handler;
            dp.BeginDelete(criteria);
        }
#endif
       
        #endregion

        #region Overridden properties

        /// <summary>
        /// Returns true if the business object or any of its children properties are dirty.
        /// </summary>
        public override bool IsDirty
        {
            get
            {
                if (base.IsDirty) return true;

 

                if (FieldManager.FieldExists(_documentsProperty) && Documents.IsDirty) return true;

                return false;
            }
        }

        #endregion

        #region DataPortal partial methods

#if !SILVERLIGHT
        partial void OnCreating(ref bool cancel);
        partial void OnCreated();
        partial void OnFetching(DocumentTypeCriteria criteria, ref bool cancel);
        partial void OnFetched();
        partial void OnMapping(SafeDataReader reader, ref bool cancel);
        partial void OnMapped();
        partial void OnInserting(ref bool cancel);
        partial void OnInserted();
        partial void OnUpdating(ref bool cancel);
        partial void OnUpdated();
        partial void OnSelfDeleting(ref bool cancel);
        partial void OnSelfDeleted();
        partial void OnDeleting(DocumentTypeCriteria criteria, ref bool cancel);
        partial void OnDeleted();
#endif
        partial void OnChildLoading(Csla.Core.IPropertyInfo childProperty, ref bool cancel);

        #endregion
        #region Exists Command
#if !SILVERLIGHT

        public static bool Exists(DocumentTypeCriteria criteria)
        {
            return DocMan.Business.ExistsCommand.Execute(criteria);
        }

#else

        public static void Exists(DocumentTypeCriteria criteria, EventHandler<DataPortalResult<ExistsCommand>> handler)
        {
            DocMan.Business.ExistsCommand.Execute(criteria, handler);
        }

#endif

        #endregion

    }
}

JonnyBee replied on Monday, March 05, 2012

The problem is likely that you are using private backing fields.

If you use managed backing fields, MobileFormatter can do all the work for you – serialization just works.

If you use private backing fields, you need to write two methods in every class to help the MobileFormatter. You need to write a method that takes the values from your private fields and puts them into the serialization stream. And you need to write a method that takes the values from the deserialization stream and puts them into your private fields.

mariogib replied on Monday, March 12, 2012

Thank you very much for your help. You were a life saver Big Smile

Copyright (c) Marimer LLC