New Posts New Posts RSS Feed: How to do a synchronous task with a typed OperationResult
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

How to do a synchronous task with a typed OperationResult

 Post Reply Post Reply
Author
JohnBloom View Drop Down
Groupie
Groupie
Avatar

Joined: 30-Nov-2010
Location: Topeka, KS
Posts: 95
Post Options Post Options   Quote JohnBloom Quote  Post ReplyReply Direct Link To This Post Topic: How to do a synchronous task with a typed OperationResult
    Posted: 14-Jun-2012 at 11:40am
We are using the factory and when we implement it we want to do things synchronously. In the past I would just return AlwaysCompletedOperationResult.Instance at the end of the function. However with the factory we get a typed OperationResult like so:

public OperationResult<Customer> CreateAsync(Action<Customer> onSuccess = nullAction<Exception> onFail = null)
        {
            ...
 
            return AlwaysCompletedOperationResult.Instance;
        }

since it is an OperationResult of Customer I get this exception:
"Error	39	
Cannot implicitly convert type 'Cocktail.AlwaysCompletedOperationResult' to 
'Cocktail.OperationResult<DataModel.Customer>'"

What do I need to return in this case?
-John Bloom
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 14-Jun-2012 at 12:03pm
If your creation logic is all synchronous, I'm assuming you don't need to fetch external data as part of the creation. If that's the case, then the creation logic can go into the Create factory method of your entity and you just use the out of the box Factory<T> class. No need to implement your own factory class. The default implementation in Factory<T> first looks for a parameter-less factory method on your type. The name of the method doesn't matter. The method simply needs to be static and return the same type. If it doesn't find a factory method or it finds more than one match, then it attempts to create the instance via the constructor.

If your creation logic does involve steps that should be handled outside of the entity's factory method, then you use a synchronous coroutine similar to what the default Factory<T> does.

        #region IFactory<T> Members
 
        /// <summary>
        ///   Creates a new entity instance of type T.
        /// </summary>
        /// <param name="onSuccess"> Callback to be called if the entity creation was successful. </param>
        /// <param name="onFail"> Callback to be called if the entity creation failed. </param>
        /// <returns> Asynchronous operation result. </returns>
        public OperationResult<T> CreateAsync(Action<T> onSuccess = nullAction<Exception> onFail = null)
        {
            return Coroutine.Start(CreateAsyncCore, op => op.OnComplete(onSuccess, onFail)).AsOperationResult<T>();
        }
 
        #endregion
 
        /// <summary>
        ///   Implements to steps to create a new entity.
        /// </summary>
        /// <remarks>
        ///   The default implementation looks for static Create method on the entity type with no parameters. If none is found, a new instance is created using the Activator class.
        /// </remarks>
        protected virtual IEnumerable<INotifyCompleted> CreateAsyncCore()
        {
            var methodInfo = FindFactoryMethod(typeof(T));
            var instance = methodInfo != null ? methodInfo.Invoke(nullnew object[0]) : Activator.CreateInstance<T>();
            EntityManager.AddEntity(instance);
 
            yield return Coroutine.Return(instance);
        }
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down