Design patterns 富客户端数据处理和缓存模式

Design patterns 富客户端数据处理和缓存模式,design-patterns,caching,mvvm,prism,Design Patterns,Caching,Mvvm,Prism,我正在使用WPF-PRISM-MVVM开发富客户端应用程序。 客户端与提供CRUD功能的存储库服务交互,并通过WCF通道发布域实体的任何更改。 客户端处理域实体缓存以提高性能并提供脱机功能。 为此,我编写了一个存储库代理服务,它位于客户端,包装存储库服务,并处理缓存和对域实体更改感兴趣的相关客户端组件的更改传播 经过长时间的介绍,我的问题是这样做吗? 是否有我应该知道的客户端数据处理和缓存模式 干杯, Doron我们在客户端缓存中使用的形式是,我们只需在方法中添加一个属性,viola就会缓存它:

我正在使用WPF-PRISM-MVVM开发富客户端应用程序。
客户端与提供CRUD功能的存储库服务交互,并通过WCF通道发布域实体的任何更改。
客户端处理域实体缓存以提高性能并提供脱机功能。
为此,我编写了一个存储库代理服务,它位于客户端,包装存储库服务,并处理缓存和对域实体更改感兴趣的相关客户端组件的更改传播

经过长时间的介绍,我的问题是这样做吗?
是否有我应该知道的客户端数据处理和缓存模式

干杯,
Doron

我们在客户端缓存中使用的形式是,我们只需在方法中添加一个属性,viola就会缓存它:

我们的方面看起来是这样的-它将实际的缓存实现推迟到
ICacheService
的实例,但您明白了

[Serializable]
[MulticastAttributeUsage(MulticastTargets.Method, PersistMetaData = true)]
public class MethodCacheAttribute : MethodInterceptionAspect
{

    public const int DefaultCacheSeconds = 600;
    private ICacheService CacheService { get; set; }
    private string methodName;

    [IntroduceMember(Visibility = PostSharp.Reflection.Visibility.Family, OverrideAction = MemberOverrideAction.Ignore, IsVirtual = true)]
    public double CacheForSeconds { get; set; }


    public MethodCacheAttribute() : this(DefaultCacheSeconds ) { }
    public MethodCacheAttribute(int seconds)
    {
        this.CacheForSeconds = seconds;
    }

    public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
    {
        this.methodName = method.Name;
    }

    public sealed override void OnInvoke(MethodInterceptionArgs args)
    {
        var key = this.BuildCacheKey(args.Arguments,args.Method);

        var item = ServiceLocator.Current.GetInstance<ICacheService>().Cache<object>(key, () =>
            {
                return args.Invoke(args.Arguments);
            },CacheForSeconds ==0 ? TimeSpan.MaxValue :  TimeSpan.FromSeconds(CacheForSeconds));
            args.ReturnValue = item;
    }


    private string BuildCacheKey(Arguments arguments, MethodBase method)
    {
        var sb = new StringBuilder();
        sb.Append(this.methodName);

        foreach (var argument in arguments.ToArray())
        {
            sb.Append(argument == null ? "_" : argument.GetHashCode().ToString());
        }

        if (method.IsGenericMethod)
        {
            var genericArgs = String.Join("-", method.GetGenericArguments().Select(t => t.Name));
            sb.Append(genericArgs);
        }

        return sb.ToString();
    }
}
[可序列化]
[MulticastAttributeUsage(MulticastTargets.Method,PersistMetaData=true)]
公共类MethodCacheAttribute:MethodInterceptionSpect
{
public const int DefaultCacheSeconds=600;
专用ICacheService缓存服务{get;set;}
私有字符串methodName;
[简介成员(可见性=PostSharp.Reflection.Visibility.Family,OverrideAction=成员OverrideAction.Ignore,IsVirtual=true)]
公共双缓存秒{get;set;}
public MethodCacheAttribute():此(DefaultCacheSeconds){}
公共MethodCacheAttribute(整数秒)
{
this.CacheForSeconds=秒;
}
公共重写void CompileTimeInitialize(MethodBase方法,AspectInfo AspectInfo)
{
this.methodName=method.Name;
}
公共密封覆盖无效OnInvoke(MethodInterceptionArgs args)
{
var key=this.BuildCacheKey(args.Arguments,args.Method);
var item=ServiceLocator.Current.GetInstance().Cache(键,()=>
{
返回args.Invoke(args.Arguments);
},CacheForSeconds==0?TimeSpan.MaxValue:TimeSpan.FromSeconds(CacheForSeconds));
args.ReturnValue=项目;
}
私有字符串BuildCacheKey(参数,MethodBase方法)
{
var sb=新的StringBuilder();
sb.Append(this.methodName);
foreach(arguments.ToArray()中的var参数)
{
sb.Append(argument==null?\参数:argument.GetHashCode().ToString());
}
if(method.IsGenericMethod)
{
var genericArgs=String.Join(“-”,method.GetGenericArguments().Select(t=>t.Name));
sb.附加(一般标记);
}
使某人返回字符串();
}
}

的一个相当经典的应用是添加缓存功能。基本上,您可以使用另一个缓存特定类型请求的组件实现来包装实际的组件实现,从而提高性能。这是一种简单而优雅的方法

  Component myComponent = new ScientificCalculator();
  Component myDecorator = new ComponentDecorator(myComponent);
  // expensive
  double result = myComponent.doExpensiveComputation();

  // use the Decorator and cache the first time
  result = myDecorator.doExpensiveComputation();
  // and now is cheap
  result = myDecorator.doExpensiveComputation();

谢谢这就是我目前使用的策略。我想看看是否还有其他选择。