This project has moved. For the latest updates, please go here.

Cache will not work when identifier is an class (not string)

Aug 28, 2012 at 12:04 PM

I tried to use an class as identifier in LoadContext, but found though I can compile and deploy my app, the cache willn't work. (It said "could not found cache" in every data query).

Does  identifier has to be string in AgFX?

Following are my code snippets:

-------------------------------------------------

 //Identifier Class

    public class SubCategoryQueryKey
    {
        public enum QueryType { SubCateQuery = 0, HitQuery, LatestQuery };

        public SubCategoryQueryKey(QueryType inQueryType, string inMainCategory, string inSubCategory)
        {
            this.TVQueryType = inQueryType;
            this.MainCategory = inMainCategory;
            this.SubCategory = inSubCategory;
        }

        public QueryType TVQueryType { get; private set; }
        public string MainCategory { get; private set; }
        public string SubCategory { get; private set; }

   }

// LoadContext Class

  public class TvSubCategoryContext: LoadContext
    {
        public SubCategoryQueryKey TvSubCategoryType
        {
            get { return (SubCategoryQueryKey)Identity; }
        }

        public TvSubCategoryContext(SubCategoryQueryKey tvSubCategoryQueryKey)
            : base(tvSubCategoryQueryKey)
        { }


    }

 

Coordinator
Aug 29, 2012 at 7:46 AM

Hi - 

The keys need to be convertible to a string in order for the entry to be properly serialized.


Consider adding a ToString method for your SubCategoryQueryKey.  You can probably get away with just concatenating the property values.

Shawn

Sep 3, 2012 at 4:01 PM

Hi Shawnburke:

      Thanks for the reply.

      I did add ToString() method to the key class (SubCategoryQueryKey) but found the cache still don't work ("could not found cache" in every data query).

     In the end I have to :

      1. add ToString() method to the key class (SubCategoryQueryKey)

      2. Change my LoadContext Class to force use ToString()  in base class. As following

-------------------------------------------------------------------------------------------

  public class TvSubCategoryContext: LoadContext
    {

        public SubCategoryQueryKey TvSubCategoryType
        {
            get { return SubCategoryQueryKey.GetSubCategoryQueryKey((string)Identity); }
        }

        public TvSubCategoryContext(SubCategoryQueryKey tvSubCategoryQueryKey)
            : base(tvSubCategoryQueryKey.ToString())      // To Make Cache work.....
        { }

      .....

   }

---------------------------------------------------------------

    Just though it will be more convinient if parent LoadContext(base class) can automatically call Identifier's ToString() method  .

Regards

Zobeide

Coordinator
Sep 5, 2012 at 6:34 AM

OK, yes, I see now.  Sorry to steer you wrong there.

It's not ToString that you need to override, it's LoadContext.GenerateKey OR LoadContext.GetHashCode.

So, something like

 

public override string GenerateKey() {

return TvSubCategorType.TVQueryType.ToString() + TvSubCategoryType.MainCategory + TvSubCategoryType.SubCategory;

 

}

 

will do the right thing.

 

Here's the base impl for reference

 

protected virtual string GenerateKey() {

            string uniqueKey;

            if (_id is string || _id.GetType().IsPrimitive) {
                uniqueKey = _id.ToString();
            }
            else {
                uniqueKey = _id.GetHashCode().ToString();
            }
            return uniqueKey;
        }