通用的多层数据访问模式? [英] Generic multi-layer data access pattern?

查看:21
本文介绍了通用的多层数据访问模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试一些用于 n 层数据访问的新模式,并发现了一种看起来非常灵活且易于实现的模式.基本上,我需要一个解决方案,使各种数据层可即时插入/交换 - 即从数据库访问基本数据、分布式缓存、本地缓存等.

I've been playing around with some new patterns for n-layer data access, and came across one that seems very flexible and easy to implement. Basically, I needed a solution that could make various data layers pluggable/swapabale on the fly - i.e., base data access from DB, distributed caching, local caching, etc.

下面的代码很容易重复使用,而且效率非常高 - 只比我之前的完全硬编码的解决方案长了几个滴答声.

The code below is easily reused and incredibly efficient - only a few ticks longer than my previous entirely-hard-coded solution.

这看起来如何?有什么办法可以更好地实施吗?任何一般的想法或批评?使用过类似模式的人有什么意见吗?

How does this look? Is there any way that this could be implemented better? Any general thoughts or critiques? Any input from those who have used similar patterns?

基类:

public class DataAdapterFactory<T> where T : class
{
    private DataAdapter<T> Adapter;
    public DataAdapterFactory(DataAdapterBase<T> Base)
    {
        Adapter = Base;
    }
    public void Extend<U>() where U : DataAdapterExtender<T>, T, new()
    {
        DataAdapterExtender<T> Extender = new U();
        Extender.BaseAdapter = Adapter as T;
        Adapter = Extender;
    }
    public T GetAdapter()
    {
        return Adapter as T;
    }
}
public class DataAdapter<T> where T : class { }
public class DataAdapterBase<T> : DataAdapter<T> where T : class { }
public class DataAdapterExtender<T> : DataAdapter<T> where T : class
{
    public T BaseAdapter;
}

在 DAL 中实施:

// base interface defines methods
public interface IMyDataAdapter
{
    string GetString();
}
// base sql adapter
public class SqlDataAdapter : DataAdapterBase<IMyDataAdapter>, IMyDataAdapter
{
    public string GetString()
    {
        return "SQL";
    }      
}
// provides cache support
public class DistributedCacheExtender : DataAdapterExtender<IMyDataAdapter>, IMyDataAdapter
{
    public string GetString()
    {
        return BaseAdapter.GetString() + ", Distributed Cache";
    }   
}
// provides local cache support
public class LocalCacheExtender : DataAdapterExtender<IMyDataAdapter>, IMyDataAdapter
{
    public string GetString()
    {
        return BaseAdapter.GetString() + ", Local Cache";
    }
}

访问数据:

public IMyDataAdapter GetAdapter() 
{
    // create adapter based on SqlDataAdapter
    DataAdapterFactory<IMyDataAdapter> factory = new DataAdapterFactory<IMyDataAdapter>(new SqlDataAdapter());
    // implement distributed cache
    factory.Extend<DistributedCacheExtender>();
    // implement local cache
    factory.Extend<LocalCacheExtender>();
    return factory.GetAdapter();
}

使用上面的工厂,基本适配器和扩展器的任意组合(Extend<>() 必须按执行顺序调用)可以通过接口轻松无缝地即时使用,而业务层对此一无所知实施.

Using the factory above, any combination of base adapters and extenders (Extend<>() must be called in the order of execution) can be used easily and seamlessly on the fly via the interface, with the business layer knowing nothing of the implementation.

在上面的这种情况下,调用 GetString() 将导致SQL、分布式缓存、本地缓存".在实际场景中,将首先调用本地缓存.如果某个项目不存在,我们将转到分布式缓存,如果不存在,我们将从数据库中获取它——并且任何模块都可以根据对象根据需要换入或换出、用户等

In this case above, calling GetString() would result in "SQL, Distributed Cache, Local Cache". In a real world scenario, the local cache would be called first. If an item isn't there, we'd head over to the distributed cache, and if it isn't there, we'd get it from the DB - and any module could be swapped in or out as needed depending on the obejct, user, etc.

推荐答案

我会看看 http://en.wikipedia.org/wiki/Decorator_pattern 为此 - 您的示例将变成这样:

I'd look at the http://en.wikipedia.org/wiki/Decorator_pattern for this - your example would then become something like this:

public interface IMyDataAdapter
{
    string GetString();
}

public class SqlDataAdapter :  IMyDataAdapter
{
    public string GetString()
    {
        return "SQL";
    }      
}

public class LocalCacheDecorator : IMyDataAdapter
{
    private IMyDataAdapter adapter;
    public LocalCacheDecorator(IMyDataAdapter adapter)
    {
        this.adapter = adapter;
    }

    public string GetString()
    {
        return "Local cache, " + this.adapter.GetString();
    }
}

这篇关于通用的多层数据访问模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆