New Posts New Posts RSS Feed: CoroutinFns.AsOperation ..how to use?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

CoroutinFns.AsOperation ..how to use?

 Post Reply Post Reply
Author
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Topic: CoroutinFns.AsOperation ..how to use?
    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


 


Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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();


Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Posted: 23-Feb-2012 at 9:05am
Leaving the return type unchanged?
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post Posted: 23-Feb-2012 at 9:06am
OperationResult implements INotifyCompleted and IResult so you can return it as either. 
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: 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;
        }
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: 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;
        }
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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();

    }

 


Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post 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?

Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Posted: 24-Feb-2012 at 11:14am
This would be really great!
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: 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.
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Posted: 24-Feb-2012 at 11:53am
I think in my tries of today I was doing

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

Thanks mgood!
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post 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
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: 27-Feb-2012 at 8:49am

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

 
Back to Top
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Posted: 27-Feb-2012 at 9:08am
Ok, I'm...my HandleError was returning  a IEnumerator<IResult> ...it's ok now thanks!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down