Print Page | Close Window

porting from df2010 to df2012 and cocktail v2.2

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=3900
Printed Date: 16-May-2025 at 4:23pm


Topic: porting from df2010 to df2012 and cocktail v2.2
Posted By: pponzano
Subject: porting from df2010 to df2012 and cocktail v2.2
Date Posted: 18-Jan-2013 at 6:27am
Hello,
I've decided to port my old code from devforce 2010 to 2012 and update cocktail...I don't want to use the compatibily pack, want to update my code in order to run as it should be now... just a question,
I've a repository and I call the method in those form

     public OperationResult<IEnumerable<SomeObject>> MyMethod(INDICATORI i, Action<IEnumerable> onSuccess = null, Action<Exception> onFail = null)
        {
            StoredProcQuery query =
            entityManagerProvider.Manager.SomeStore(UserService.CurrentUser.IDUtente, i.ID);

            EntityQueryOperation op = query.ExecuteAsync();

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

        }

  public IEnumerable<IResult> SomeMethod(GetRolesListResult source)
        {
            if (source != null)
            {
                OperationResult<IEnumerable<SomeObject>> operation;
                yield return (operation = adminRepository.MyMethod(source).ContinueOnError());

                if (operation.CompletedSuccessfully)
                {
                  //do something
                }
            }
        }

Now I was wondering if the new metod to call shoul be

  public OperationResult<IEnumerable<SomeObject>> MyMethod(INDICATORI i, Action<IEnumerable> onSuccess = null, Action<Exception> onFail = null)
        {
            StoredProcQuery query =
            entityManagerProvider.Manager.SomeStore(UserService.CurrentUser.IDUtente, i.ID);

            Task<IEnumerable<SomeObject>> op = query.ExecuteAsync();

            return op.OnComplete(onSuccess, onFail);

        }

Is this ok?
How should I call if from the caller? wasn't clearer to use the OperationResult instead of having Task<IEnumerable<T>>?

Thanks




Replies:
Posted By: mgood
Date Posted: 18-Jan-2013 at 9:03am
If you don't use the compatibilty pack you no longer have OperationResult available and with the task-based async pattern you don't use callbacks anymore at all. So, your method would look like this.
 
public async Task<IEnumerable<SomeObject>> MyMethodAsync(INDICATOR i)
{
    var query = entityManagerProvider.Manager.SomeStore(UserService.CurrentUser.IDUtente, i.ID);
 
    return (await query.ExecuteAsync()).Cast<SomeObject>();
}
 
And the caller would call it like this:
 
IEnumerable<SomeObject> result = await MyMethodAsync(....)
 
 


Posted By: pponzano
Date Posted: 21-Jan-2013 at 2:27am
Thanks Marcel,
what about if I'm using the TryExecuteQueryAsync?

For the queries I've got no problem, for StoredProcedures I got

  public async Task<QueryResult<SP_GET_VARIABILI_FROM_SCRIPT_Result>> LoadVariabiliByScript(SCRIPT script, INDICATORI indicatore)
        {
            StoredProcQuery query =
                entityManagerProvider.Manager.SP_GET_VARIABILI_FROM_SCRIPTQuery(indicatore.ID, script.ID, UserService.CurrentUser.IDUtente);

              return await TryExecuteQueryAsync(query);
        }

With the TryExecuteAsync defined as

 private async Task<QueryResult<T>> TryExecuteQueryAsync<T>(IEntityQuery<T> query)
        {
            CancellationToken cancellationToken = new CancellationToken();

            var res = await this.entityManagerProvider.Manager.TryExecuteQueryAsync<T>(query, cancellationToken);

            return res;
        }

As I've understood I can't specify with StoredProcQuery the query generic type ??

Have I to do

public async Task<QueryResult> LoadVariabiliByScript(SCRIPT script, INDICATORI indicatore)
{
            StoredProcQuery query =
                entityManagerProvider.Manager.SP_GET_VARIABILI_FROM_SCRIPTQuery(indicatore.ID, script.ID, UserService.CurrentUser.IDUtente);

              return await TryExecuteQueryAsync(query);
}
and lose the implicit cast of TryExecuteQueryAsync<T>?

Thanks





Posted By: pponzano
Date Posted: 21-Jan-2013 at 8:05am
Excuse me consider I've (2010)

Coroutine.BeginExecute(GetSyntDataForDashBoard(), null,
                ((a, b) =>
                {
                    Coroutine.BeginExecute(LoadTags());
                }));

Where LoadTags is

 private IEnumerator<IResult> LoadTags()
        {
            NumericToColorConverter converter = new NumericToColorConverter();
            IsBusy = true;
            OperationResult<IEnumerable<TAG>> operation;

            yield return (operation = repository.LoadTags()).ContinueOnError();

            if (operation.CompletedSuccessfully)
            {
                listaTags = operation.Result.ToObservableCollection();

                var lst = new ObservableCollection<UserControls.IFTagItem>();

                foreach (var item in listaTags)
                {
                    var tagitem = new UserControls.TagItem { Name = item.NOME, Color = (System.Windows.Media.Color)converter.Convert(item.COLORE, typeof(System.Windows.Media.Color), null, null) };

                    lst.Add(tagitem);

                    if (item.INDICATORIs != null && item.INDICATORIs.Any())
                    {
                        foreach (var indicatore in item.INDICATORIs)
                        {
                            var ind = DataItems.FirstOrDefault(o1 => o1.IDIndicatore == indicatore.ID);

                            if (ind != null)
                            {
                                ind.ListaTagItems.Add(tagitem);

                                ind.NotifyTagsUpdate();
                            }
                        }
                    }
                }

                MatchingTags = lst;

                if (ifTagUserControl != null)
                    ifTagUserControl.MatchingItems = MatchingTags;
            }

            if (operation.HasError)
            {
                yield return ErrorHandler.HandleError(operation.Error).ToSequentialResult();
            }

            IsBusy = false;
        }

How can I concatenate the tasks? shoul I use continueWith(delegate { LoadTags();})?

Thanks


Posted By: mgood
Date Posted: 21-Jan-2013 at 9:05am
The main difference between ExecuteAsync and TryExecuteAsync is that in case of TryExecuteAsync, await doesn't throw an exception if the query fails or gets cancelled. Instead you can programmatically inspect the returned QueryResult and then decide how to proceed. QueryResult also provides more information about the execution instead of just the data.
 
Here is more information to learn about the task-based async pattern.
 
http://drc.ideablade.com/devforce-2012/bin/view/Documentation/program-asynchronously - http://drc.ideablade.com/devforce-2012/bin/view/Documentation/program-asynchronously



Print Page | Close Window