Print Page | Close Window

How to do a synchronous task with a typed OperationResult

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3498
Printed Date: 29-Apr-2025 at 6:06pm


Topic: How to do a synchronous task with a typed OperationResult
Posted By: JohnBloom
Subject: How to do a synchronous task with a typed OperationResult
Date 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



Replies:
Posted By: mgood
Date 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);
        }



Print Page | Close Window