Я использую 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 (). Кажется, что, имея в виду ссылку CouchbaseClient в классе EntityBase. Я не продан на 100% при таком подходе.