Dynamic pluggable data access level

I am writing a data driven WPF client. Typically, a client will retrieve data from a WCF service that queries SQL-db, but I would like it to retrieve data directly from SQL or other arbitrary data sources.

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

First we have some data object that we would 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 to use an interface that all levels of data access should implement. Suppose you can use an abstract class. Thoughts?

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

You can create an 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 , . , - . , , . ?

// 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;
    }
}

factory, , .

// 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.

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

.

+3
3

. : (ICustomerFacade) , . SQL WCF .

, " "? . , , , . ( ), - SQL- ( ) .

, , , , . , ICustomer, Customer. SQL Server WCF , ICustomer ..

:

+4

WCF , :

  • - WCF, , ( "", VS svcutil).

  • Clone() CopyFrom() DTO ( ),

1 - 2 , . , VS -, DTO - Reference.cs , .

WCF, .

+1

svcutil.exe -, .NET .NET. . , svcutil.exe .

Typically, for each service there should be an additional contract assembly that contains all the service interfaces and data for this service. This service is referenced by both the service and the client. Thus, when making changes, they will be displayed both on the client and on the server.

http://blog.walteralmeida.com/2010/08/wcf-tips-and-tricks-share-types-between-server-and-client.html

0
source

Source: https://habr.com/ru/post/1744498/


All Articles