动态可交换数据访问层 [英] Dynamic swappable Data Access Layer

查看:114
本文介绍了动态可交换数据访问层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写驱动的WPF客户端数据。客户通常会从一个WCF服务,查询一个SQL数据库提取数据,但我想直接从SQL或其他任意数据源提取数据的选项。

I'm writing a data driven WPF client. The client will typically pull data from a WCF service, which queries a SQL db, but I'd like the option to pull the data directly from SQL or other arbitrary data sources.

我来到了这个设计,想听听它是否是最好的设计,你的意见。

I've come up with this design and would like to hear your opinion on whether it is the best design.

首先,我们有一些数据对象,我们会要从SQL提取。

First, we have some data object we'd like to extract from SQL.

// The Data Object with a single property
public class Customer
{
    private string m_Name = string.Empty;

    public string Name 
    {
        get { return m_Name; }
        set { m_Name = value;}
    }
}

然后我打算使用的所有数据访问层应该实现一个接口。假设一个人也可以使用的抽象类。思考?

Then I plan on using an interface which all data access layers should implement. Suppose one could also use an abstract class. Thoughts?

// The interface with a single method
interface ICustomerFacade
{
    List<Customer> GetAll();
}



可以创建一个SQL实现。

One can create a SQL implementation.

// Sql Implementation
public class SqlCustomrFacade : ICustomerFacade
{
    public List<Customer> GetAll()
    {
        // Query SQL db and return something useful
        // ...

        return new List<Customer>();
    }
}

我们还可以创建一个WCF实现。与WCF的问题是,它不使用相同的数据对象。它会创建自己的本地版本,所以我们将不得不细节在某种程度上复制。我想,人们可以使用反射来跨越复制类似字段的值。思考

We can also create a WCF implementation. The problem with WCF is is that it doesn't use the same data object. It creates its own local version, so we would have to copy the details over somehow. I suppose one could use reflection to copy the values of similar fields across. Thoughts?

// Wcf Implementation
public class WcfCustomrFacade : ICustomerFacade
{
    public List<Customer> GetAll()
    {
        // Get date from the Wcf Service (not defined here)
        List<WcfService.Customer> wcfCustomers = wcfService.GetAllCustomers();

        // The list we're going to return
        List<Customer> customers = new List<Customer>();

        // This is horrible
        foreach(WcfService.Customer wcfCustomer in wcfCustomers)
        {
            Customer customer = new Customer();
            customer.Name = wcfCustomer.Name;
            customers.Add(customer);
        }

        return customers;
    }
}



我还计划使用一个工厂来决定哪些门面使用。

I also plan on using a factory to decide which facade to use.

// Factory pattern
public class FacadeFactory()
{
    public static ICustomerFacade CreateCustomerFacade()
    {
        // Determine the facade to use
        if (ConfigurationManager.AppSettings["DAL"] == "Sql")
            return new SqlCustomrFacade();
        else
            return new WcfCustomrFacade();
    }
}

这是怎么了DAL通常会被使用。

This is how the DAL would typically be used.

// Test application
public class MyApp
{
    public static void Main()
    {
        ICustomerFacade cf = FacadeFactory.CreateCustomerFacade();
        cf.GetAll();
    }
}



我很欣赏你的思想和时间。

I appreciate your thoughts and time.

推荐答案

您有一个非常灵活的软件一个非常好的开端。你已经用你的方法打的主要问题:你的数据提供商的合同( ICustomerFacade )必须指定由所有实施者使用的数据对象。无论你的SQL和WCF数据提供者必须返回相同的数据对象。

You have a very good start on a really flexible approach to your software. You've already hit the main problem with your approach: your data provider contract (ICustomerFacade) must specify data objects that are used by all implementers. Both your SQL and WCF data providers must return the same data objects.

在标注为这是可怕的那一部分?它实际上不是那么糟糕。你迭代两次,没错,但你这样做,以提供更强大,更灵活的软件架构。性能不会的的坏(除非你遍历列表中的许多许多项目),以及你的系统就能够调用Web服务,并直接调用SQL服务器(无论是否间切换它在一般将是一个好主意)。

That part you label as "this is horrible"? It's not actually all that bad. You're iterating twice, yes, but you're doing so to provide a stronger, more flexible software architecture. Performance will not be that bad (unless you're iterating over many many items in the list), and your system will be able to switch between calling a web service and calling SQL server directly (whether or not it's a good idea in general) at will.

一件事,你可以做些什么来消除双迭代将有你的数据契约取决于你的数据对象的抽象。他们会返回 ICustomer ,而不是客户,例如。然后,您的SQL Server对象和WCF数据对象,因为他们实施的 ICustomer 等接口可能是不同的对象完全,只要。

One thing that you could do to eliminate the double-iteration would be to have your data contract depend on abstractions of your data objects. They'd return ICustomer rather than Customer, for example. Then your SQL Server objects and your WCF data objects could be different objects altogether, as long as they implemented the ICustomer, etc. interfaces.

其他建议:


  • 您应该考虑返回 IList的(甚至的IEnumerable ),而不是列表为您收集返回方法。

  • 您的工厂模式是一个好的开始,但工厂是如此的2000年代。 :)你可能要考虑将充分依赖注入路线;我建议统一在微软企业库

  • You should consider returning IList (or even IEnumerable) rather than List for your collection-returning methods.
  • Your factory pattern is a good start, but factories are so 2000s. :) You might want to consider going the full Dependency Injection route; I'd suggest Unity in the Microsoft Enterprise Library.

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

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