DataPortal CreateCompleted Not Raised

DataPortal CreateCompleted Not Raised

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


mcalhoun posted on Sunday, June 13, 2010

Hi Everyone,

I have am trying to work with CSLA Silverlight v4 Beta 2 and when I create the async factory methods, the CreateCompleted event never seems to be raised. Is this something that should be done automatically, or do I have to do something in my code to trigger the Event being raised? Below is the code I'm having issues with.

Thanks,
Matt

[Serializable]
[ObjectFactory("MyApp.CustomerFactory, MyApp.Domain")]
public class Customer : BusinessBase<Customer>
{

   [Obsolete("required by Silverlight Serialization. Do not call manually", true)]
   public Customer()
   { 
    }

   ...

   public static void NewCustomer(EventHandler<DataPortalResult<Customer>> callback)
   {
      var dp = new DataPortal<Customer>(DataPortal.ProxyModes.LocalOnly);
      dp.CreateCompleted += callback;
      dp.BeginCreate();
    }
}

public class CustomerFactory : ObjectFactory
{
   public Customer Create()
   {
      var entity = (Customer)Activator.CreateInstance(typeof(Customer), null);

      using (BypassPropertyChecks(entity))
      {
         LoadProperty<Guid>(entity, Customer.IdProperty, Guid.NewGuid());
         entity.Name = "Default Name";
      }

      CheckRules(entity);
      MarkNew(entity);
           
      return entity;
    }   
}


[TestMethod, Asynchronous]
public void Can_Create_Customer()
{
   DateTime startTime = DateTime.Now;
   bool created = false;

   Customer entity = null;
   Customer.NewCustomer(delegate(object sender, DataPortalResult<Customer> result)
                                     {
                                         if(result.Error==null)
                                         {
                                             entity = (Customer) result.Object;
                                         }
                                         created = true;
                                     });

   EnqueueConditional(() =>
            {
                Assert.IsTrue((DateTime.Now - startTime) < TimeSpan.FromSeconds(10), "Timeout waiting for DataPortal to create object");
                return created;
            });

           
   EnqueueCallback(() => Assert.IsNotNull(entity));
   EnqueueCallback(() => Assert.IsInstanceOfType(entity, typeof(Customer)));
}

RockfordLhotka replied on Sunday, June 13, 2010

If you are using the local data portal, then your factory methods must accept a callback parameter, and must invoke that callback - otherwise the data portal has no way of knowing when your factory method has completed (since it assumed to be async).

If you are using a remote data portal, then your code should work, since the factory object would be running on the .NET side, where (at that level) all data portal calls are synchronous.

Copyright (c) Marimer LLC