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 = null, Action<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(null, new object[0]) : Activator.CreateInstance<T>();
EntityManager.AddEntity(instance);
yield return Coroutine.Return(instance);
}