Criteria Class with Silverlight CSLA 3.6

Criteria Class with Silverlight CSLA 3.6

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


danielw posted on Tuesday, January 13, 2009

Hi all

I am writing a silverlight app using CLSA 3.6.

I have recoded the templates for CLSAGen to create the correct files as required.

However I am having a bit of an issue with the criteria class. I have created a class as detailed in the book.

[Serializable()]
protected class CriteriaSystemUser : CriteriaBase
{

   private string _USR_Login;
   public string USR_Login {
      get { return _USR_Login; }
   }

   public CriteriaSystemUser(String uSR_Login)
   :
base(typeof(CriteriaSystemUser))
   {
      
_USR_Login = uSR_Login;
   }

}

protected void DataPortal_Fetch(CriteriaSystemUser crit)
{
using (SqlConnection cn = new SqlConnection(DataBase.MakingSpaceOperationsConnection))
{
using (SqlCommand cmd = cn.CreateCommand())
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "JM_ServiceCentre_ListBySystemUser";
cmd.Parameters.AddWithValue("@USR_Login", crit.USR_Login);

.........

But If i run this I get a no parameterless constructor defined for this object error

So I then add a constructor

private CriteriaSystemUser()

:base(typeof(CriteriaSystemUser))

{

}

which now runs succesfully but although the USR_Login value gets set in the class. It is not passed through to the dataportal fetch

Any ideas would be most helpful, I have gone back to using the singlecriteria which works fine for now.

 

Cheers

Daniel

sergeyb replied on Tuesday, January 13, 2009

Any CSLA class in SL must have a public parameterless constructor.  There are no exceptions to this rule, including criteria classes.  This requirement stems out of reflection restrictions in Silverlight.

 

 

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: danielw [mailto:cslanet@lhotka.net]
Sent: Tuesday, January 13, 2009 8:33 AM
To: Sergey Barskiy
Subject: [CSLA .NET] Criteria Class with Silverlight CSLA 3.6

 

Hi all

I am writing a silverlight app using CLSA 3.6.

I have recoded the templates for CLSAGen to create the correct files as required.

However I am having a bit of an issue with the criteria class. I have created a class as detailed in the book.

[Serializable()]
protected class CriteriaSystemUser : CriteriaBase
{

   private string _USR_Login;
   public string USR_Login {
      get { return _USR_Login; }
   }

   public CriteriaSystemUser(String uSR_Login)
   :base(typeof(CriteriaSystemUser))
   {
      _USR_Login = uSR_Login;
   }

}

protected void DataPortal_Fetch(CriteriaSystemUser crit)
{
using (SqlConnection cn = new SqlConnection(DataBase.MakingSpaceOperationsConnection))
{
using (SqlCommand cmd = cn.CreateCommand())
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "JM_ServiceCentre_ListBySystemUser";
cmd.Parameters.AddWithValue("@USR_Login", crit.USR_Login);

.........

But If i run this I get a no parameterless constructor defined for this object error

So I then add a constructor

private CriteriaSystemUser()

:base(typeof(CriteriaSystemUser))

{

}

which now runs succesfully but although the USR_Login value gets set in the class. It is not passed through to the dataportal fetch

Any ideas would be most helpful, I have gone back to using the singlecriteria which works fine for now.

 

Cheers

Daniel



danielw replied on Tuesday, January 13, 2009

Thanks for that
I have changed the constructor to public . But I still can not get access to the passed in value in my Fetch procedure

protected void DataPortal_Fetch(CriteriaSystemUser crit)
{
using (SqlConnection cn = new SqlConnection(DataBase.MakingSpaceOperationsConnection))
{
using (SqlCommand cmd = cn.CreateCommand())
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "JM_ServiceCentre_ListBySystemUser";
cmd.Parameters.AddWithValue("@USR_Login", crit.USR_Login);

The Crit.USR_Login is blank

Cheers

Daniel

PeteH replied on Tuesday, January 13, 2009

You will need to add these lines to the CriteriaSystemUser class:

    #region MobileFormatter

    /// <summary>
    /// Override this method to insert your field values
    /// into the MobileFormatter serialzation stream.
    /// </summary>
    /// <param name="info">
    /// Object containing the data to serialize.
    /// </param>
    /// <param name="mode">
    /// The StateMode indicating why this method was invoked.
    /// </param>
    protected override void OnGetState(SerializationInfo info, StateMode mode)
    {
      base.OnGetState(info, mode);
      info.AddValue("Csla.Silverlight.
CriteriaSystemUser._USR_Login", _USR_Login);
    }

    /// <summary>
    /// Override this method to retrieve your field values
    /// from the MobileFormatter serialzation stream.
    /// </summary>
    /// <param name="info">
    /// Object containing the data to serialize.
    /// </param>
    /// <param name="mode">
    /// The StateMode indicating why this method was invoked.
    /// </param>
    protected override void OnSetState(SerializationInfo info, StateMode mode)
    {
      base.OnSetState(info, mode);
     
_USR_Login = info.GetValue<string>("Csla.Silverlight.CriteriaSystemUser._USR_Login");
    }

    #endregion

sergeyb replied on Tuesday, January 13, 2009

Alternatively, you can inherit from CriteriaBase and use RegisterProperty syntax to register custom properties.  This method is probably more consistent with other CSLA classes.

 

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: PeteH [mailto:cslanet@lhotka.net]
Sent: Tuesday, January 13, 2009 10:46 AM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: Criteria Class with Silverlight CSLA 3.6

 

You will need to add these lines to the client version of CriteriaSystemUser.


&nbsp;&nbsp;&nbsp; #region MobileFormatter

&nbsp;&nbsp;&nbsp; /// &lt;summary&gt;
&nbsp;&nbsp;&nbsp; /// Override this method to insert your field values
&nbsp;&nbsp;&nbsp; /// into the MobileFormatter serialzation stream.
&nbsp;&nbsp;&nbsp; /// &lt;/summary&gt;
&nbsp;&nbsp;&nbsp; /// &lt;param name="info"&gt;
&nbsp;&nbsp;&nbsp; /// Object containing the data to serialize.
&nbsp;&nbsp;&nbsp; /// &lt;/param&gt;
&nbsp;&nbsp;&nbsp; /// &lt;param name="mode"&gt;
&nbsp;&nbsp;&nbsp; /// The StateMode indicating why this method was invoked.
&nbsp;&nbsp;&nbsp; /// &lt;/param&a mp;gt;
&nbsp;&nbsp;&nbsp; protected override void OnGetState(SerializationInfo info, StateMode mode)
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; base.OnGetState(info, mode);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; info.AddValue("Csla.Silverlight.</font></font><font face="Tahoma"><font size="2">CriteriaSystemUser</font></font><font face="Tahoma"><font size="2">._USR_Login", _USR_Login);
</font></font><font face="Tahoma"><font size="2">&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; /// &lt;summary&gt;
&nbsp;&nbsp;&nbsp; /// Override this method to retrieve your field values
&nbsp;&nbsp;&nbsp; /// from the MobileFormatter serialzation stream.
&nbsp;&nbsp;&nbsp; /// & amp;lt;/summary&gt;
&nbsp;&nbsp;&nbsp; /// &lt;param name="info"&gt;
&nbsp;&nbsp;&nbsp; /// Object containing the data to serialize.
&nbsp;&nbsp;&nbsp; /// &lt;/param&gt;
&nbsp;&nbsp;&nbsp; /// &lt;param name="mode"&gt;
&nbsp;&nbsp;&nbsp; /// The StateMode indicating why this method was invoked.
&nbsp;&nbsp;&nbsp; /// &lt;/param&gt;
&nbsp;&nbsp;&nbsp; protected override void OnSetState(SerializationInfo info, StateMode mode)
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; base.OnSetState(info, mode);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _</font></font><font face="Tahoma"><font size="2">_USR_Login </font></font><font face="Tahoma"><font size="2">= info.G etValue&lt;int&gt;("Csla.Silverlight.</font></font><font face="Tahoma"><font size="2">CriteriaSystemUser</font></font><font face="Tahoma"><font size="2">._USR_Login");
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; #endregion




danielw replied on Tuesday, January 13, 2009

Sorry not sure if I am being stupid,
But i have added the code as suggested but I still do not get the value back.

Do I need to call some other method than crit.USR_Login to get the value

protected void DataPortal_Fetch(CriteriaSystemUser crit)
using (SqlConnection cn = new SqlConnection(DataBase.MakingSpaceOperationsConnection))
{
using (SqlCommand cmd = cn.CreateCommand())
{
cmd.CommandType = System.Data.
CommandType.StoredProcedure;
cmd.CommandText =
"JM_ServiceCentre_ListBySystemUser";
 cmd.Parameters.AddWithValue("@USR_Login", crit.USR_Login);

 

cheers

Daniel

 

danielw replied on Wednesday, January 14, 2009

Thanks for your help
could not get it to work using the override methods of mobile.
And also could not registerpropertys in the CriteriaBase.

But I took this idea and created a BusinessBase as a criteria as in the book, I could then register the properties and it now works beautifully with the CSLA gen

Cheers

daniel

thunt replied on Monday, January 19, 2009

Daniel,

Could you post code to help me out with same issue?

Cheers

Terry

sergeyb replied on Monday, January 19, 2009

Here is an example of how to use Criteria class for Silverlight inheriting from CriteriaBase.  This specific class is embedded inside a ROLB class, but you can have it as external / stand alone class as well.

 

 

[Serializable]

        public class Criteria : CriteriaBase

        {

            private static PropertyInfo<string> SearchProperty = RegisterProperty<string>(typeof(Criteria), new PropertyInfo<string>("Search", "Search"));

            public string Search

            {

                get { return ReadProperty(SearchProperty); }

            }

            private static PropertyInfo<bool> IncludeAddressProperty = RegisterProperty<bool>(typeof(Criteria), new PropertyInfo<bool>("IncludeAddress", "IncludeAddress"));

            public bool IncludeAddress

            {

                get { return ReadProperty(IncludeAddressProperty); }

            }

            public Criteria(Type type, string search, bool includeAddress)

                : base(type)

            {

                LoadProperty(SearchProperty, search);

                LoadProperty(IncludeAddressProperty, includeAddress);

            }

 

            public Criteria() { }

        }

 

 

Here is how to use it:

public static void GetReadonlySearchedPersonList(string search, bool includeAddress, EventHandler<DataPortalResult<ReadonlySearchedPersonList>> handler)

        {

            DataPortal<ReadonlySearchedPersonList> dp = new DataPortal<ReadonlySearchedPersonList>();

            dp.FetchCompleted += handler;

            dp.BeginFetch((new Criteria(typeof(ReadonlySearchedPersonList), search, includeAddress)));

        }

 

 

private void DataPortal_Fetch(Criteria criteria)

        {

….

 

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: thunt [mailto:cslanet@lhotka.net]
Sent: Monday, January 19, 2009 8:50 AM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: Criteria Class with Silverlight CSLA 3.6

 

Daniel,

Could you post code to help me out with same issue?

Cheers

Terry



danielw replied on Monday, January 19, 2009

Hi Terry,
Using a business base I did it like this.

[Serializable()]
public class Criteria : BusinessBase<Criteria>, ICriteria
{
   private static PropertyInfo<Int32> MED_IDProperty = RegisterProperty<Int32>(new PropertyInfo<Int32>("MED_ID", "MED_ID"));
   public Int32 MED_ID
   {
      get 
      {
         return GetProperty(MED_IDProperty);
      }
      set
      {
         SetProperty(MED_IDProperty,
value);
      }
   }

   public Type ObjectType{
      get {return typeof(Criteria);}
   }

   public Criteria(Int32 mED_ID)
   {
      SetProperty(MED_IDProperty, mED_ID);
   }

   public Criteria()
   {
   }

}

 

with the call being

public static void DeleteMedicalContact(Int32 mED_ID,EventHandler<DataPortalResult<MedicalContact>> handler)
{
DataPortal<MedicalContact> dp = new DataPortal<MedicalContact>();
dp.DeleteCompleted += handler;
dp.BeginDelete(new Criteria(mED_ID));
}

cheers

Daniel

 

Jack replied on Thursday, February 19, 2009

Sergey,

I ran into this same issue but I had put my public constructor inside a #if Silverlight .. #endif block.  I had to take that block out in order for it to work.

Yet my base class has the parameterless public constructor inside those same attributes and it is fine.

Why is that?

thanks

jack

Copyright (c) Marimer LLC