2

I am looking for a way to subscribe to events like Storing a specific object type to ServiceStack.Redis. For example I may

using (var redisClient = new RedisClient())
using (var redisMyObjects = redisClient.As<MyObject>())
{
        redisMyObjects.Store(myObject);//<-- I want this to trigger an event somehow
}

Is there anything like a OnStore event which I can hook too, anything out of the box? if not, is there any recommendation about how this should be done?

4

1 回答 1

2

我不认为有什么可以挂钩的(可能是错的)。

想到了两个选项:
1 - 制作扩展方法
2 - 发布消息以存储您的对象并拥有一个处理程序来侦听响应并执行某些操作。这可能是矫枉过正,因为它正在进入发布/订阅领域。但是,我认为,值得研究。(此处为基本示例,请参阅此处的Pub/Sub)。

扩展方法

public static class RedisClientExtensions
{
    public static void StoreWithTrigger<T>(this IRedisTypedClient<T> redisClient, T value, Action<T> trigger)
    {
        redisClient.Store(value);
        trigger(value);
    }
}

使用扩展方法

public void MyMethod()
{
    using (var redisClient = new RedisClient())
    using (var redisMyObjects = redisClient.As<MyObject>())
    {
        redisMyObjects.StoreWithTrigger<MyObject>(new MyObject(), TriggerEvent);//<-- I want this to trigger an event somehow
    }
}

private void TriggerEvent<T>(T value)
{
        //dosomething
}

希望这能给你一些想法。

于 2013-05-23T16:15:53.600 回答