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

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

问题描述

我已经与n层的数据访问一些新的模式玩耍,并在一个似乎非常灵活,易于实现来了。基本上,我需要一个解决方案,可以使各种数据层可插拔/ swapabale上飞 - 即从数据库基础数据访问,分布式缓存,本地缓存,等等

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.

下code很容易重复使用和难以置信的高效率 - 比我的previous完全-硬codeD的解决方案只有少数蜱不再

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();
}

可以轻松,无缝地使用上通过接口的苍蝇,与业务层一无所知的;

使用上面的工厂,基地适配器和扩展的任意组合(>()必须在执行顺序被称为扩展和LT)实施

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,分布式缓存,本地缓存。在真实世界场景中,本地高速缓存器将首先​​调用。如果项目是不存在,我们就头部到分布式缓存,并且如果它不存在,我们就从数据库获得它 - 和任何模块可以在取决于obejct根据需要被交换或出,用户等。

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天全站免登陆