I need to create Dlinq entity property value caches for every entity and
every property so that
all entity caches can cleared if entity of this type is changed. Entity
class method uses this as:
string GetCustomerName ById( string customerId ) {
var cache = CacheManager.Ge t<Customer,stri ng, string>("Custom erName");
string res;
if (cache.TryGetVa lue(customerId , out res))
return res;
res = Nowthwind.Custo mers.Where(x =x.CustomerId == customerId).Sel ect(x
=x.CustomerName .Single();
cache.Add(custo merId , res);
return res;
}
If customer has changed, I call
CacheManager.Cl ear( typeof(Customer ) );
I created class below but got "Type expected" compile error in line shown in
comment.
How to fix ?
Andrus.
using System;
using System.Collecti ons.Generic;
namespace Eetasoft.Eeva.E ntity
{
public static class CacheManager
{
struct Key
{
internal Type Entity;
internal string Property;
}
// this causes compile errror: type expected
static Dictionary<Key, Dictionary<,>Ca cheList = new
Dictionary<Key, Dictionary<,>>( );
/// <summary>
/// Gets existing or creates new property cache.
/// </summary>
public static Dictionary<TKey , TValueGet<TEnti ty,TKey,
TValue>(string propertyName)
{
var key = new Key() { Entity = typeof(TEntity) , Property =
propertyName };
Dictionary<TKey , TValuecache;
if (CacheList.TryG etValue(key, out cache))
return cache;
cache = new Dictionary<TKey , TValue>();
CacheList.Add(k ey, cache);
return cache;
}
/// <summary>
/// Clear the cache if entity has changed.
/// </summary>
/// <param name="entity"></param>
public static void Clear(Type entity)
{
foreach (var key in CacheList.Keys)
{
if (key.Entity == entity)
CacheList.Remov e(key);
}
}
}
}
every property so that
all entity caches can cleared if entity of this type is changed. Entity
class method uses this as:
string GetCustomerName ById( string customerId ) {
var cache = CacheManager.Ge t<Customer,stri ng, string>("Custom erName");
string res;
if (cache.TryGetVa lue(customerId , out res))
return res;
res = Nowthwind.Custo mers.Where(x =x.CustomerId == customerId).Sel ect(x
=x.CustomerName .Single();
cache.Add(custo merId , res);
return res;
}
If customer has changed, I call
CacheManager.Cl ear( typeof(Customer ) );
I created class below but got "Type expected" compile error in line shown in
comment.
How to fix ?
Andrus.
using System;
using System.Collecti ons.Generic;
namespace Eetasoft.Eeva.E ntity
{
public static class CacheManager
{
struct Key
{
internal Type Entity;
internal string Property;
}
// this causes compile errror: type expected
static Dictionary<Key, Dictionary<,>Ca cheList = new
Dictionary<Key, Dictionary<,>>( );
/// <summary>
/// Gets existing or creates new property cache.
/// </summary>
public static Dictionary<TKey , TValueGet<TEnti ty,TKey,
TValue>(string propertyName)
{
var key = new Key() { Entity = typeof(TEntity) , Property =
propertyName };
Dictionary<TKey , TValuecache;
if (CacheList.TryG etValue(key, out cache))
return cache;
cache = new Dictionary<TKey , TValue>();
CacheList.Add(k ey, cache);
return cache;
}
/// <summary>
/// Clear the cache if entity has changed.
/// </summary>
/// <param name="entity"></param>
public static void Clear(Type entity)
{
foreach (var key in CacheList.Keys)
{
if (key.Entity == entity)
CacheList.Remov e(key);
}
}
}
}
Comment