BO Async call returns before observable collection loads

BO Async call returns before observable collection loads

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


cconte posted on Thursday, February 16, 2012

 

Hi Folks,

- Using silverlight 4. Csla 4.2 Bxf and MVVM design pattern

I am designing an SL application and I am struggling to get my List to bind correctly to my observable collection from my viewmodel.

Actually, I am using an observable collection (UserEditVM.MessageInfoList) to wrap all the children of one of properties class (UserEdit.Messages) into a VM class (MessageInfoVM). - Hope i'm clear.

When I call an instance of UserEditVM which going to load the BO, It would  seem like the ObservableCollection MessageInfoList is called before the “BeginRefresh” method fully load the data (may cause by the async call).

I have read on internet some recommendation to use the UOW approach to avoid these syncronization issues but in my case the MessageList is linked to my UserEditBO.

Is there a way to do this or have I structured my solution incorrectly?

To complete this post, I put below a piece of code focused on the issue.

Thanks

Cedric

 

ViewModel (VM) UserEditVM and MessageEditVM :

public class UserEditVM : ViewModel<Library.UserEdit>

    {

        public UserEditVM(int userID)

        {

            Shell.Instance.ShowStatus(new Status { Text = string.Format("User {0}", userID), IsBusy = true });

            BeginRefresh("GetUserEdit", userID);

        }

        public ObservableCollection<MessageInfoVM> MessageInfoList

        {

            // Here the issue : Used before  the BO  is  completely loaded

            get

            {

                var result = new ObservableCollection<MessageInfoVM>();

                if (Model != null)

                {

                    if (Model.Messages != null)

                        foreach (var item in Model.Messages)

                            result.Add(new MessageInfoVM { Model = item });

                }

                return result;

            }

        }

...

}

public class MessageInfoVM : ViewModel<Library.MessageInfo>

    {

        public static readonly DependencyProperty ModelProperty =

        DependencyProperty.Register("Model", typeof(Library.MessageInfo), typeof(MessageInfoVM), null);

 

        public Library.MessageInfo Model

        {

            get { return (Library.MessageInfo)GetValue(ModelProperty); }

            set { SetValue(ModelProperty, value); }

        }

        public void EditItem()

        {

            Shell.Instance.ShowView(

              typeof(LioApp.Views.EditMessage).AssemblyQualifiedName,

              "messageEditVMViewSource", new ViewModels.MessageEditVM(Model.MessageID),

              "MainContent");

        }

    }

 

Business Object (BO)  (VM) UserEdit and MessageEdit :

public partial class UserEdit : BusinessBase<UserEdit>

    {

        private static PropertyInfo<int> UserIDProperty = RegisterProperty<int>(c => c.UserID);

        public int UserID

        {

            get { return GetProperty(UserIDProperty); }

            set { LoadProperty(UserIDProperty, value); }

        }

 

        public static PropertyInfo<string> UserNameProperty = RegisterProperty<string>(c => c.UserName);

        public string UserName

        {

            get { return GetProperty(UserNameProperty); }

            set { SetProperty(UserNameProperty, value); }

        }

        public static PropertyInfo<MessageInfoList> MessagesProperty = RegisterProperty<MessageInfoList>(c => c.Messages);

        public MessageInfoList Messages

        {

            get { return GetProperty(MessagesProperty); }

            private set { LoadProperty(MessagesProperty, value); }

        }

}

public partial class MessageEdit : BusinessBase<MessageEdit>

    {

        public static readonly PropertyInfo<int> MessageIDProperty = RegisterProperty<int>(c => c.MessageID);

        public int MessageID

        {

            get { return GetProperty(MessageIDProperty); }

            set { LoadProperty(MessageIDProperty, value); }

        }

        public static PropertyInfo<string> DescriptionProperty = RegisterProperty<string>(c => c.Description);

        public string Description

        {

            get { return GetProperty(DescriptionProperty); }

            set { SetProperty(DescriptionProperty, value); }

        }

}

Copyright (c) Marimer LLC