0

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

コンソールアプリケーション内でこれをテストすると、名前はキャッシュに割り当てられますが、アイテムは常にnullであり、キャッシュから戻ってきますか?

// 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%売られているわけではありません。

4

1 に答える 1

0

初期化時に、バケットのユーザー名とパスワードを構成値としてクライアントに渡します。私のシナリオでは、空白のコンストラクターは言葉になりません。

于 2012-03-27T13:51:05.540 に答える