New Posts New Posts RSS Feed: porting from df2010 to df2012 and cocktail v2.2
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

porting from df2010 to df2012 and cocktail v2.2

 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: porting from df2010 to df2012 and cocktail v2.2
    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

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: 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(....)
 
 
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: 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



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: 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
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: 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.
 


Edited by mgood - 21-Jan-2013 at 11:05am
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down