Print Page | Close Window

CoroutinFns.AsOperation ..how to use?

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=3286
Printed Date: 24-May-2024 at 4:31pm


Topic: CoroutinFns.AsOperation ..how to use?
Posted By: pponzano
Subject: CoroutinFns.AsOperation ..how to use?
Date Posted: 23-Feb-2012 at 2:04am
Hello,
I'm migrating to Cocktail... I got a problem with the AsOperationResult ..

    

public IEnumerable<IResult> LoadData()

   {

       IsBusy = true;

       CanPrintExport = false;

 

 

       CoroutineFns.AsOperationResult(

       yield return CoroutineFns.AsOperationResult(() =>

                               repository.myMethod(Date, results =>

                               {

                                   DataItems = new BindableCollection<myObject>(results.Cast<ElencoCollateralsResult>());

                               },

                               e =>

                               {

                                   Caliburn.Micro.Coroutine.BeginExecute(errorHandler.HandleError(e), null, null);

                               }));

 

       IsBusy = false;

       CanPrintExport = DataItems != null && DataItems.Count() > 0;

 

   }


I got the error

Error    9    Cannot convert lambda expression to type 'IdeaBlade.EntityModel.INotifyCompleted' because it is not a delegate type  

How can I fix it?

Thanks


 





Replies:
Posted By: smi-mark
Date Posted: 23-Feb-2012 at 8:57am
You can change that to:

  yield return 

                               repository.myMethod(Date, results =>

                               {

                                   DataItems = new BindableCollection<myObject>(results.Cast<ElencoCollateralsResult>());

                               },

                               e =>

                               {

                                   Caliburn.Micro.Coroutine.BeginExecute(errorHandler.HandleError(e), null, null);

                               }).AsOperationResult();




Posted By: pponzano
Date Posted: 23-Feb-2012 at 9:05am
Leaving the return type unchanged?


Posted By: smi-mark
Date Posted: 23-Feb-2012 at 9:06am
OperationResult implements INotifyCompleted and IResult so you can return it as either. 


Posted By: mgood
Date Posted: 23-Feb-2012 at 10:22am
You can also write this a little cleaner with Cocktail v0.3.1 (available for download now) if you move the AsOperationResult call into myMethod and have myMethod return OperationResult<IEnumerable<ElencoCollateralsResult>>.
 
        public IEnumerable<IResult> LoadData()
        {
            IsBusy = true;
 
            CanPrintExport = false;
 
 
            OperationResult<IEnumerable<ElencoCollateralsResult>> operation;
            yield return (operation = repository.myMethod(Date)).ContinueOnError();
 
            if (operation.CompletedSuccessfully)
                DataItems = new BindableCollection<myObject>(operation.Result);
 
            if (operation.HasError)
                yield return errorHandler.HandleError(operation.Error);
 
            IsBusy = false;
 
            CanPrintExport = DataItems != null && DataItems.Count() > 0;
        }


Posted By: mgood
Date Posted: 23-Feb-2012 at 10:27am
Sorry, minor change. I see your HandleError return IEnumberable<IResult>.
 
        public IEnumerable<IResult> LoadData()
        {
            IsBusy = true;
 
            CanPrintExport = false;
 
 
            OperationResult<IEnumerable<ElencoCollateralsResult>> operation;
            yield return (operation = repository.myMethod(Date)).ContinueOnError();
 
            if (operation.CompletedSuccessfully)
                DataItems = new BindableCollection<myObject>(operation.Result);
 
            if (operation.HasError)
                yield return errorHandler.HandleError(operation.Error).ToSequentialResult();
 
            IsBusy = false;
 
            CanPrintExport = DataItems != null && DataItems.Count() > 0;
        }


Posted By: pponzano
Date Posted: 24-Feb-2012 at 6:41am
Excuse me ... since I retrieve data from stored procedure how should I implement the methods?
 
Mines are in the form
 

public INotifyCompleted LoadUserMessages(DateTime data, int ancheMessaggiVisti, Action<IEnumerable> onSuccess, Action<Exception> onFail)

    {

        var query = Manager.IF_SP_USER_MESSAGES_SELECTQuery(<parameter>);

 

        var op = query.ExecuteAsync();

 

        return op.OnComplete(onSuccess, onFail);

    }

 

but this won't work as OperationResult, neither OperationResult<IEnumerable<userMessage>>();
 
Thanks


Posted By: smi-mark
Date Posted: 24-Feb-2012 at 9:12am
Can you not do:

public OperationResult LoadUserMessages(DateTime data, int ancheMessaggiVisti, Action<IEnumerable> onSuccess, Action<Exception> onFail)

    {

        var query = Manager.IF_SP_USER_MESSAGES_SELECTQuery(<parameter>);

 

        var op = query.ExecuteAsync();

 

        return op.OnComplete(onSuccess, onFail).AsOperationResult();

    }

 




Posted By: pponzano
Date Posted: 24-Feb-2012 at 10:42am
Hello,
if I do so in the applicationrepository (casting .OperationResult()) can I convert to OperationResult<IEnumerable<T>>() in the viewmodel ? is necessary to pass OnSuccess and onFail in the repository?



Posted By: smi-mark
Date Posted: 24-Feb-2012 at 10:48am
I can't remember how it works with Stored Procedures, so Marcel will have to chime in, but when you use the OperationResult you won't have to use onSuccess/onFail as all that functionality is provided by the OperationResult class.


Posted By: pponzano
Date Posted: 24-Feb-2012 at 11:14am
This would be really great!


Posted By: mgood
Date Posted: 24-Feb-2012 at 11:43am
I actually recommend you leave the onSuccess/onFail callbacks and make them optional. In some cases you might find it useful to use the callbacks instead of the returned OperationResult. So, with that said, your method would look like this:
 

public OperationResult<IEnumerable<userMessage>> LoadUserMessages(DateTime data, int ancheMessaggiVisti,

      Action<IEnumerable<userMessage>> onSuccess = null, Action<Exception> onFail = null)

{

     var query = Manager.IF_SP_USER_MESSAGES_SELECTQuery(<parameter>);

     var op = query.ExecuteAsync();

     return op.OnComplete(onSuccess, onFail).AsOperationResult<userMessage>();

}

 
Both, OnComplete and AsOperationResult<T>() will do the cast for you.


Posted By: pponzano
Date Posted: 24-Feb-2012 at 11:53am
I think in my tries of today I was doing

return op.AsOperationResult<IEnumerable<userMessage>>();

Thanks mgood!


Posted By: pponzano
Date Posted: 26-Feb-2012 at 11:39pm
Hello mgood,
everything is ok but I don't have the ToSequentialResult(); method...where is it defined in the Cocktails.Util assembly?
Thanks again


Posted By: mgood
Date Posted: 27-Feb-2012 at 8:49am

It's an extension method defined in the Cocktail assembly. The method is on the CoroutineFns class.

 
http://cocktail.ideablade.com/CocktailHelpReference/webframe.html?Cocktail~Cocktail.CoroutineFns~ToSequentialResult.html - http://cocktail.ideablade.com/CocktailHelpReference/webframe.html?Cocktail~Cocktail.CoroutineFns~ToSequentialResult.html


Posted By: pponzano
Date Posted: 27-Feb-2012 at 9:08am
Ok, I'm...my HandleError was returning  a IEnumerator<IResult> ...it's ok now thanks!



Print Page | Close Window