BeginFetch with criteria requires KnownTypeAttribute?

BeginFetch with criteria requires KnownTypeAttribute?

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


mrkenn posted on Thursday, September 27, 2012

(Csla 4, Silverlight 5)

I'm having some trouble understanding how to accomplish calling fetch on a BLB when the call requires multiple parameters.

In .Net, I usually have a simple class with my parameters as class properties that I wrap in a SingleCriteria during the fetch:

 

MyCriteria c = new MyCriteria(param1, param2, param3);
MyObj bo = DataPortal.Fetch<MyObj>(new SingleCriteria<MyObj, MyCriteria>(c));
return bo;

In Silverlight, I tried the same thing:

MyCriteria c = new MyCriteria(param1, param2, param3);
DataPortal<MyObj> dp = new DataPortal<MyObj>();
dp.FetchCompleted += handler;
dp.BeginFetch(new SingleCriteria<MyObj, MyCriteria>(c));

 

Unfortunately, I get the following at runtime:
Type '[namespace].MyCriteria' with data contract name 'MyCriteria:http://schemas.datacontract.org/2004/07/[namespace]' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

 

Is there a better way to wrap up parameters I need to execute the fetch?

 

RockfordLhotka replied on Thursday, September 27, 2012

The data portal requires that all types you pass through the data portal implement IMobileObject. The easiest way to accomplish this is to subclass one of the CSLA base types. In your case, subclass CriteriaBase.

Also, make sure to use managed backing fields so they automatically serialize. Otherwise you'll need to override the get/set state methods as described in the 'Using CSLA 4' books.

mrkenn replied on Thursday, September 27, 2012

Thanks for the quick response Rocky. I had gave that a shot (I  tried before without managed backers) with the same results. Am I missing something obvious?

 

using System;
using Csla;
using Csla.Serialization;
namespace MyNamespace
{
    [Serializable]
    public class MyCriteria : CriteriaBase<MyCriteria>
    {
        private String _param1;
        public String param1
        {
            get { return _param1; }
            set { _param1 = value; }
        }

        private String _param2;
        public String param2
        {
            get { return _param2; }
            set { _param2 = value; }
        }

        private String _param3;
        public String param3
        {
            get { return _param3; }
            set { _param3 = value; }
        }

        public MyCriteria () { }

    }

 

JonnyBee replied on Thursday, September 27, 2012

Use managed properties.

mrkenn replied on Friday, September 28, 2012

Thanks for the response, tho apparently my understanding of what "managed backing fields" is incorrect.

My criteria class had private backing fields for the properties, tho even a class with no properties at all has the same error. Per your suggestion, I added the properties as such with the same results:

 

public static PropertyInfo<int?> param1Property = RegisterProperty(new PropertyInfo<int?>("param1", "param1"));
public int? param1

{
  get { return ReadProperty(param1Property); }
  set { LoadProperty(param1
Property, value); }

}

 

JonnyBee replied on Friday, September 28, 2012

SingleCriteria is supposed to ONLY wrap one value - not a new criteria object and should now be considered oblsolete..

So your MyCriteria object should inherit from CriteriaBase and implement managed properties and have a default (parameterless) public constructor.

Then use that object itself - do not wrap inside a SingleCriteria object.

MyCriteria c = new MyCriteria(param1, param2, param3);
DataPortal<MyObj> dp = new DataPortal<MyObj>();
dp.FetchCompleted += handler;
dp.BeginFetch(c);

mrkenn replied on Friday, September 28, 2012

That now makes perfect sense... thanks for the assist!

Copyright (c) Marimer LLC