Following your advice from a little while back about having the ability to have multiple saved baskets. I have created a savedbasket class (which is based around the wishlist class) everything seems to be working great, but I can't ever get the custom item cache from the itemcacheservice, I've added it in the merchello.config file, and can successfully resolve it from the ItemItemCache.CustomTypeFields, but I can't ever get an instance of it? Do I need to somehow initialise it before I can resolve it?
We have actually done both of those, the bit we are struggling with is this part of the wishlist code (line 159):
var customerItemCache = merchelloContext.Services.ItemCacheService.GetItemCacheWithKey(wishlist.Customer, ItemCacheType.Wishlist);
We have tried ItemCacheType.Custom, (which didn't work) possibly because the ItemCacheTypeField.BuildCache(); doesn't support it?
AddUpdateCache(ItemCacheType.Custom, NotFound);
We've also tried multiple ways of getting the cache using the custom typefield which we can successfully get via
var typeField = EnumTypeFieldConverter.ItemItemCache.CustomTypeFields.FirstOrDefault(x => x.TypeKey == Domain.AppConstants.SavedBasketKey);
And tried these:
var cache = merchelloContext.Services.ItemCacheService.GetItemCaches(typeField.TypeKey);
var cache2 = merchelloContext.Services.ItemCacheService.GetItemCaches(customer.Key);
var cache3 = merchelloContext.Services.ItemCacheService.GetByKey(typeField.TypeKey);
var cache4 = merchelloContext.Services.ItemCacheService.GetByKey(customer.Key);
var cache5 = merchelloContext.Services.ItemCacheService.GetEntityItemCaches(customer.Key, typeField.TypeKey);
var cache6 = merchelloContext.Services.ItemCacheService.GetItemCacheByCustomer(customer, typeField.TypeKey);
But still no luck with the custom cache :( Any ideas?
When you first create your custom item cache, what are you using as the entityKey ... it should be the customer key.
The EnumTypeFieldConverter is not going to do much for you with custom type fields. For custom type fields it's really only useful for the back office controllers so they don't choke on what would otherwise be a known type field.
What is being returned for your:
cache
cache2
cache3 ....
If there is a record there it should be returning ... those are pretty much wrapped repository calls ...
cache is empty,
cache2 has basket and checkout caches,
cache3 is null,
cache4 is null,
cache5 is empty,
cache6 is null.
Do we have to manually create the custom item caches? we were under the impression that custom item caches would be created by merchello after adding them to the config file, or is that not the case?
The itemcache service doesn't return anything, no matter what we pass it?
Edit:
We may have tracked this down, but could use your expert eye on it! It looks like it was failing in the itemcacheservice when trying to create an instance of the itemcache class here:
and then saving it using the itemcacheservice, at which point we can then carry on and sucessfully create the object which is added to the cache and then can be used.
Did your bypass approach and circumvent the service for the original creation of the item cache ... something like this?
// TODO refactor to use service when patch available
//
// start by using an ItemCacheType that the converter already has cached.
var itemCache = new ItemCache(customer.Key, ItemCacheType.Wishlist);
itemCache.ItemCacheTfKey = [YOUR GUID FROM THE CONFIG];
// save the item cache
MerchelloContext.Current.Services.ItemCacheService.Save(itemCache);
I'll add a few tasks to 2.4.0 to clean this up!
Thanks for the effort and helping sort out figure out what the issues are.
I didn't actually think of doing it like that, that's far more elegant than what I did.. haha!
I ended up doing this..
var type = typeof(ItemCache);
var args = new object[] { customer.Key, typeField.TypeKey };
var instance = type.Assembly.CreateInstance(type.FullName, false, BindingFlags.Instance | BindingFlags.NonPublic, null, args, null, null);
var itemCache = (ItemCache)instance;
merchelloContext.Services.ItemCacheService.Save(itemCache);
But I'll definitely update to use your way until 2.4.0
You're welcome, thanks for your help in pointing us in the right direction to start with and creating a great package :)
Multiple Saved Baskets
Morning Rusty,
Following your advice from a little while back about having the ability to have multiple saved baskets. I have created a savedbasket class (which is based around the wishlist class) everything seems to be working great, but I can't ever get the custom item cache from the itemcacheservice, I've added it in the merchello.config file, and can successfully resolve it from the ItemItemCache.CustomTypeFields, but I can't ever get an instance of it? Do I need to somehow initialise it before I can resolve it?
Cheers,
Tom
Hey Tom - to get an instance of it, you will need to use the
ItemCacheService
(and maybe create an extension off of ICustomerCase).Hi Rusty,
We have actually done both of those, the bit we are struggling with is this part of the wishlist code (line 159):
We have tried ItemCacheType.Custom, (which didn't work) possibly because the ItemCacheTypeField.BuildCache(); doesn't support it?
We've also tried multiple ways of getting the cache using the custom typefield which we can successfully get via
And tried these:
But still no luck with the custom cache :( Any ideas?
When you first create your custom item cache, what are you using as the entityKey ... it should be the customer key.
The
EnumTypeFieldConverter
is not going to do much for you with custom type fields. For custom type fields it's really only useful for the back office controllers so they don't choke on what would otherwise be a known type field.What is being returned for your:
If there is a record there it should be returning ... those are pretty much wrapped repository calls ...
Hi Rusty,
We are using the customer key as the entity key.
cache is empty, cache2 has basket and checkout caches, cache3 is null, cache4 is null, cache5 is empty, cache6 is null.
Do we have to manually create the custom item caches? we were under the impression that custom item caches would be created by merchello after adding them to the config file, or is that not the case?
Hey Tom
You do have to create the item caches.
Checkout the basket object - it's done internally.
https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.Web/Workflow/Basket.cs#L101
Hi Rusty,
That's the bit we are failing on, just here:
https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.Web/Workflow/Basket.cs#L112
The itemcache service doesn't return anything, no matter what we pass it?
Edit:
We may have tracked this down, but could use your expert eye on it! It looks like it was failing in the itemcacheservice when trying to create an instance of the itemcache class here:
https://github.com/Merchello/Merchello/blob/master/src/Merchello.Core/Services/ItemCacheService.cs#L196
as when the cache is not found or created yet the itemcacheservice creates an instance of itemcache, it looks like it calls this constructor
https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.Core/Models/ItemCache.cs#L49
which then goes on to call
EnumTypeFieldConverter.ItemItemCache.GetTypeField(itemCacheType).TypeKey
which, looks like it will only ever call
https://github.com/Merchello/Merchello/blob/f3dd636fb3449d58d82ecd178b2ce9b1093c9615/src/Merchello.Core/Models/TypeFields/TypeFieldMapper.cs#L48
as it is passing a enum, but if it called this instead,
https://github.com/Merchello/Merchello/blob/f3dd636fb3449d58d82ecd178b2ce9b1093c9615/src/Merchello.Core/Models/TypeFields/TypeFieldMapper.cs#L38
then it would work as expected?
We have temporarily bypassed this and called this constructor using reflection (as it is internal)
https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.Core/Models/ItemCache.cs#L98
and then saving it using the itemcacheservice, at which point we can then carry on and sucessfully create the object which is added to the cache and then can be used.
Hi Tom,
Hmm - I see what you're talking about now. That does indeed need to be reviewed due to the internals.
It is weird (must have been a reason early on) that I've left the ItemCacheTfKey property public even though it is set via the constructor.
https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.Core/Models/ItemCache.cs#L126
Did your bypass approach and circumvent the service for the original creation of the item cache ... something like this?
I'll add a few tasks to 2.4.0 to clean this up!
Thanks for the effort and helping sort out figure out what the issues are.
Hi Rusty,
I didn't actually think of doing it like that, that's far more elegant than what I did.. haha!
I ended up doing this..
But I'll definitely update to use your way until 2.4.0
You're welcome, thanks for your help in pointing us in the right direction to start with and creating a great package :)
is working on a reply...