CSLA 4.1 C# what is the correct way to declare a managed property in my Identity class?

CSLA 4.1 C# what is the correct way to declare a managed property in my Identity class?

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


JCardina posted on Friday, February 04, 2011

This is giving me all kinds of trouble.  There are mentions in the v4 docs that it should be done differently with an example but either I get compiler errors or the property is always null depending on the different ways I've tried it. 

I know I'm missing something fundamental because this is my first class and attempt at using CSLA since version 1.5x

I need a Guid value stored with the identity when the user logs in representing their User ID.

Here is what I have now:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using Csla.Security;
using Csla.Serialization;
using XXXX.Data;
using System.Security.Cryptography;
using System.Text;
using System.Data;


namespace XXXX.Library
{
  
        [Serializable()]
        public class XXXXIdentity : CslaIdentity
        {

            public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty(typeof(Guid), IdProperty);
            public Guid Id
            {
                get { return ReadProperty(IdProperty); }
                private set { LoadProperty(IdProperty, value); }
            }
           

            internal static void GetIdentity(string username, string password, EventHandler<DataPortalResult<XXXXIdentity>> callback)
            {
                DataPortal.BeginFetch<XXXXIdentity>(new UsernameCriteria(username, password), callback);
            }

#if !SILVERLIGHT
            internal static XXXXIdentity GetIdentity(string username, string password)
            {
                return DataPortal.Fetch<XXXXIdentity>(new UsernameCriteria(username, password));
            }

            internal static XXXXIdentity GetIdentity(string username)
            {
                return DataPortal.Fetch<XXXXIdentity>(username);
            }
#endif         
         
         

            private void DataPortal_Fetch(UsernameCriteria crit)
            {
               //...some irrelevant encryption stuff here...

                IsAuthenticated = false;

                using (DBCommandWrapper cm = DBUtil.GetCommandFromSQL("SELECT AID FROM AUSER WHERE AUSER.ALOGIN = @LOGIN AND AUSER.APASSWORD = @PASSWORD AND AUSER.AACTIVE = @ATRUE;"))
                {

                    cm.AddInParameter("@PASSWORD", DbType.String, sPassword);
                    cm.AddInParameter("@LOGIN", DbType.String, sLogin);
                    cm.AddInParameter("@ATRUE", DbType.Boolean, true);
                    using (SafeDataReader dr = DBUtil.GetReaderFromCommand(cm))
                    {
                        if (dr.Read())
                        {
                            Id = dr.GetGuid("AID");//<--- Id is null throwing an exception
                            IsAuthenticated = true;
                        }
                    }
                  
                }

            }

        }//EOC   
}

ajj3085 replied on Friday, February 04, 2011

Do NOT call the RegisterProperty overload which takes a type as the first parameter.  That param tells the FieldManager which type the property belongs to, in your case you're saying the property is part of the Guid BO, which is not what you want.  Remove those, and you should be good to go.

try this:

public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>( x => x.Id);

JCardina replied on Friday, February 04, 2011

Thanks Andy, that makes sense, unfortunately it brings me back to the original compiler error:

Error    1    Cannot convert lambda expression to type 'Csla.PropertyInfo<System.Guid>' because it is not a delegate type   

This is the code:

 public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(x => x.Id);
            public Guid Id
            {
                get { return ReadProperty(IdProperty); }
                private set { LoadProperty(IdProperty, value); }
            }

That's the error I was getting before.

ajj3085 replied on Friday, February 04, 2011

Ah, you should inhertic CslaIdentity<T>, not plain CslaIdentity.  This class compiles fine for me:

 

 

 

 

 

 

 

 

 

public class Class1 : Csla.Security.CslaIdentityBase<Class1

> {

 

 

 

 

public static readonly PropertyInfo<Guid> MyGuidProperty = RegisterProperty<Guid

>( x => x.MyGuid );

 

 

 

 

public Guid

MyGuid {

 

 

 

 

get { return

GetProperty( MyGuidProperty ); }

 

 

 

 

private set { LoadProperty( MyGuidProperty, value

); }

JCardina replied on Friday, February 04, 2011

Aha!  Thank you very much Andy, that's the trick.  I had copied the code from the 4.10 ProjectTracker sample which inherits from plain CslaIdentity.

 

ajj3085 replied on Friday, February 04, 2011

Glad you got it working.

For reference, you could have inherited from the non-generic CslaIdentityBase, and then pass typeof( XXXCustomIdentity) in as the first parameter.   But so many people have been confused by that overload the usual recommendation is not using it at all. 

Copyright (c) Marimer LLC