我正在使用 Couchbase 1.8 将复杂实体的集合存储在缓存中。
非常简单的场景,所有在单个控制台应用程序中似乎都可以找到。但是,当我将相同的“想法”重构为不同的程序集时,似乎没有任何效果。
控制台应用程序:
[Serializable]
public class Entity : EntityBase<Entity>
{
public string Title { get; set; }
public Entity() { }
}
public abstract class EntityBase<T> : IEntity<T> where T : new()
{
public string Name { get; set; }
public List<T> Get() { return null; }
}
public interface IEntity<T> where T : new()
{
Name { get; }
List<T> Get();
}
然后在控制台应用程序中,我使用以下方法进行测试:
// client = new CouchbaseClient();
List<Entity> e = new List<Entity> { new Entity { Title = "Entity1" } };
client.Store(StoreMode.Set, "EntityItem", e);
List<Entity> output = client.Get<List<Entity>>("EntityItem"); // return 1 item
但是,当我重构相同的代码时,似乎没有存储任何内容:
// assembly called Entity.Core
// 1. Entity
[Serializable]
[EntityAttribute(Description = "description")]
public class Entity : EntityBase<Entity>
{
public string Title { get; set; }
public Entity() { }
}
// 2. EntityBase
public abstract class EntityBase : IEntity<T> where T : new()
{
private Couchbase _client = new CouchbaseClient("vBucket", "vBucketPassword");
public string Name { get; set; }
public static T Instance { get { return Singleton<T>.Instance; } }
private IEnumerable<T> ToCache<T>() where T : new() { // gets items from my data source }
public List<T> Get()
{
List<T> entity = this._client.Get<List<T>>(this.Name);
// if not in cache, call ToCache<T>() to get the object, cache it and return
return entity;
}
}
// 3. IEntity is the same as above
// 4. Singleton<T> is a class that constructs a singleton pattern based on the T
当我在控制台应用程序中对此进行测试时,名称在缓存中分配,但该项目始终为空,从缓存中返回?
// client = new CouchbaseClient();
List<Entity> entity = Entity.Instance.Get(); // returns, for example 4 items as expected
client.Store(StoreMode.Set, "EntityItem", entity); // should store List<Entity>[4] in cache
List<Entity> output = client.Get<List<Entity>>("EntityItem"); // returns null
我假设这是因为我试图在定义我的实体的抽象类中定义客户端和实体?这种推断可能吗?
更新 我修改了我的测试以将 CouchbaseClient 实例传递给 .Get() 方法。似乎,在 EntityBase 类中的 CouchbaseClient 引用搞砸了。我不是 100% 通过这种方法出售的。