Author |
Share Topic Topic Search Topic Options
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
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
|
|
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
|
Post Options
Quote Reply
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();
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
Posted: 23-Feb-2012 at 9:05am |
Leaving the return type unchanged?
|
|
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
|
Post Options
Quote Reply
Posted: 23-Feb-2012 at 9:06am |
OperationResult implements INotifyCompleted and IResult so you can return it as either.
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
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; }
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
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; }
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
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
|
|
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
|
Post Options
Quote Reply
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();
}
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
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?
|
|
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
|
Post Options
Quote Reply
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.
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
Posted: 24-Feb-2012 at 11:14am |
This would be really great!
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
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.
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
Posted: 24-Feb-2012 at 11:53am |
I think in my tries of today I was doing
return op.AsOperationResult<IEnumerable<userMessage>>();
Thanks mgood!
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
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
|
|
mgood
IdeaBlade
Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
|
Post Options
Quote Reply
Posted: 27-Feb-2012 at 8:49am |
It's an extension method defined in the Cocktail assembly. The method is on the CoroutineFns class.
|
|
pponzano
Senior Member
Joined: 28-Apr-2011
Location: Italy
Posts: 165
|
Post Options
Quote Reply
Posted: 27-Feb-2012 at 9:08am |
Ok, I'm...my HandleError was returning a IEnumerator<IResult> ...it's ok now thanks!
|
|