【问题标题】:Couchbase storing of complex times not firing复杂时间的 Couchbase 存储不触发
【发布时间】:2012-03-19 16:35:33
【问题描述】:

我正在使用 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% 通过这种方法出售的。

【问题讨论】:

  • 您能否确认两个应用程序中的 app.config 相同?在控制台应用程序中,您暗示您正在调用假定 app|web.config 中的“couchbase”部分的无参数构造函数。您在实体中调用的构造函数也需要定义该部分。
  • 嗨,约翰,我确实确认了正确的密钥。我最终创建了一个单独的类来定义 CouchbaseClient 并将其传递给我的方法。工作正常。

标签: complextype couchbase


【解决方案1】:

在初始化时将存储桶用户名和密码作为配置值传递给客户端。在我的场景中,一个空白的构造函数不会说话。

【讨论】:

    猜你喜欢
    • 2011-08-16
    • 2016-08-31
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 2013-12-03
    相关资源
    最近更新 更多