HOWTO code this for cslalight

HOWTO code this for cslalight

Old forum URL: forums.lhotka.net/forums/t/7899.aspx


swegele posted on Wednesday, October 28, 2009

I have a method on my Identity class called ImpersonateUser that takes a GUID of a different user.  This in turn calls a command object that checks the security to see if this impersonation is OK.  If all is well then it loads some properties.  I have this working fine syncronous.

        public void ImpersonateUser(Guid UserID)
        {
            ImpersonateUserCommand cmd = ImpersonateUserCommand.ImpersonateUser(UserID);

            LoadProperty(ImpersonatingProperty, true);
            LoadProperty(ImpersonateUserTypeProperty, cmd.ImpersonateUserType);
            LoadProperty(ImpersonateUserIDProperty, UserID);
            LoadProperty(ImpersonateUserNameProperty, cmd.ImpersonateUserName);
        }

But with async I do not know how to code the method on Identity to accept a handler and then call another async method on ImpersonateUserCommand. 

Hope this makes sense?

Sean

sergeyb replied on Wednesday, October 28, 2009

Is this what you are looking for?

 

 

        public void ImpersonateUser(Guid UserID)
        {
           ImpersonateUserCommand.BeginImpersonateUser(UserID, (o,e)=>{

ImpersonateUserCommand cmd = e.Object as ImpersonateUserCommand;
            LoadProperty(ImpersonatingProperty, true);
            LoadProperty(ImpersonateUserTypeProperty, cmd.ImpersonateUserType);
            LoadProperty(ImpersonateUserIDProperty, UserID);
            LoadProperty(ImpersonateUserNameProperty, cmd.ImpersonateUserName);

});
        }

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: swegele [mailto:cslanet@lhotka.net]
Sent: Wednesday, October 28, 2009 5:21 PM
To: Sergey Barskiy
Subject: [CSLA .NET] HOWTO code this for cslalight

 

I have a method on my Identity class called ImpersonateUser that takes a GUID of a different user.  This in turn calls a command object that checks the security to see if this impersonation is OK.  If all is well then it loads some properties.  I have this working fine syncronous.

        public void ImpersonateUser(Guid UserID)
        {
            ImpersonateUserCommand cmd = ImpersonateUserCommand.ImpersonateUser(UserID);

            LoadProperty(ImpersonatingProperty, true);
            LoadProperty(ImpersonateUserTypeProperty, cmd.ImpersonateUserType);
            LoadProperty(ImpersonateUserIDProperty, UserID);
            LoadProperty(ImpersonateUserNameProperty, cmd.ImpersonateUserName);
        }

But with async I do not know how to code the method on Identity to accept a handler and then call another async method on ImpersonateUserCommand. 

Hope this makes sense?

Sean


swegele replied on Wednesday, October 28, 2009

Thanks Sergey...would this block the UI because the call to Identity.ImpersonateUser has no callback passed in?

Forgive me as I am still new to silverlight.

Sean

sergeyb replied on Wednesday, October 28, 2009

Nope, the UI is not blocked.  So, you should disable the UI somehow to keep the user from messing with it.

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: swegele [mailto:cslanet@lhotka.net]
Sent: Wednesday, October 28, 2009 5:33 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: HOWTO code this for cslalight

 

Thanks Sergey...would this block the UI because the call to Identity.ImpersonateUser has no callback passed in?

Forgive me as I am still new to silverlight.

Sean


swegele replied on Wednesday, October 28, 2009

OK so this sets up some sort of dynamic method that is hanging around waiting for BeginImpersonateUser to finish...even though the original call to Identity.ImpersonateUser finishes immediately?

Is there any way to pass in a call back which gets triggered when BeginImpersonateUser is done...so that the UI developer knows this method might take awhile and knows when it is finished?

Also what would the basic signature of the BeginImpersonateUser look like?

Thanks a million Sergey.  I have been putting off making matching async methods on my business objects because I couldn't figure this out :-)

Sean

sergeyb replied on Wednesday, October 28, 2009

Sean,
The code I posted already has a call back,it is just implemented as lamda expression.
Sergey
________________________________
From: swegele [cslanet@lhotka.net]
Sent: Wednesday, October 28, 2009 5:45 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: RE: HOWTO code this for cslalight

OK so this sets up some sort of dynamic method that is hanging around waiting for BeginImpersonateUser to finish...even though the original call to Identity.ImpersonateUser finishes immediately?

Is there any way to pass in a call back which gets triggered when BeginImpersonateUser is done...so that the UI developer knows this method might take awhile and knows when it is finished?

Also what would the basic signature of the BeginImpersonateUser look like?

Thanks a million Sergey. I have been putting off making matching async methods on my business objects because I couldn't figure this out :-)

Sean


swegele replied on Wednesday, October 28, 2009

I am also wondering what happens to any errors that are raised during the execution of the command object?

Sorry for being so noob on this silverlight stuff.

Sean

sergeyb replied on Wednesday, October 28, 2009

You can test e.Error in teh code I posted.

Sergey.
________________________________
From: swegele [cslanet@lhotka.net]
Sent: Wednesday, October 28, 2009 6:16 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: RE: HOWTO code this for cslalight

I am also wondering what happens to any errors that are raised during the execution of the command object?

Sorry for being so noob on this silverlight stuff.

Sean


swegele replied on Wednesday, October 28, 2009

My example differs from yours in 2 ways:
- The method on Identity requires the UI developer to explicitly pass in a delegate
- The method on Identity calls Invoke on the passed in handler when finished to let the UI know it is done

The first example you and I were trying out didn't give any way for the UI to know when things were finished or whether there were errors...unless I am missing something??

Sean


swegele replied on Wednesday, October 28, 2009

I suppose all the trouble comes from having the method like so

Identity.Impersonate --> CommandObject

Since it is the dataportal on the CommandObject that is called, that is where the handler naturally fits in.  I am having to tweek things to get the handler to pass back through the Identity.Impersonate method call.

Perhaps it would be better design to just do the call to the CommandObject and have the CommandObject manipulate the Identity if it succeeds.

Sean

swegele replied on Wednesday, October 28, 2009

How does this look?  I am trying to communicate to the UI developer that calling this method requires a busy state to be set.  It handles errors internally as well as returning the error to the UI if it is needed there.


    public partial class IRBIdentity : MembershipIdentity
    {

        public void BeginImpersonateUser(Guid UserID, EventHandler<DataPortalResult<ImpersonateUserCommand>> handler)
        {

            ImpersonateUserCommand cmd = ImpersonateUserCommand.BeginImpersonateUser(UserID, (o, e) =>
            {
                if (e.Error == null)
                {
                    LoadProperty(ImpersonatingProperty, true);
                    LoadProperty(ImpersonateUserTypeProperty, e.Object.ImpersonateUserType);
                    LoadProperty(ImpersonateUserIDProperty, UserID);
                    LoadProperty(ImpersonateUserNameProperty, e.Object.ImpersonateUserName);
                }

                handler.Invoke(o, e);
            });
        }
}

Sean

Copyright (c) Marimer LLC