Home > Software design >  Refactoring a WCF Trusted Facade
Refactoring a WCF Trusted Facade

Time:12-17

I'm attempting to refactor a "trusted facade" which currently wraps over 50 service calls to the backend. All calls have different signatures, but everything else is being repeated. The issue with the existing calls was that there was no attempt made to manage the connections, resulting in ephemeral ports remaining in the "BOUND" state.

ORIGINAL CODE:

public class ReportWeb : IReportWeb
{
    ReportService.ReportClient client = new ReportClient();
    ...
    
    public string[] GetAccounts() => client.GetAccounts();
}

NEW CODE:

private ChannelFactory<IReportService> _factory = null;
private IReportService _proxy = null;

private void OpenProxy()
{
    _factory = new ChannelFactory<IReportService>("NetTcpBinding_IReportService");
    _proxy = _factory.CreateChannel();
}

private void CloseProxy()
{
    ((IClientChannel)_proxy).Close();
    _factory.Close();
}

One of 50 similar methods:

public string[] GetAccounts() // Different - name, params, and return type
{
    string[] accounts = null; // Different

    try
    {
        OpenProxy();
        accounts = _proxy.GetAccounts();  // Different
        CloseProxy();
    }
    catch (Exception exception)
    {
        bool faulted = _factory.State == CommunicationState.Faulted;
        _factory.Abort();

        if (faulted)
        {
            throw new ApplicationException(exception.Message);
        }
        else
        {
            throw;
        }
    }
    return accounts;
}

Another similar method:

//Another method
public ContractsInfo[] GetContracts(int contractId) // Different - 
    // name, params, and return type
{
    ContractsInfo[] contracts = null; // Different

    try
    {
        OpenProxy();
        contracts = _proxy.GetContracts(contractId); // Different
        CloseProxy();
    }
    catch (Exception exception)
    {
        bool faulted = _factory.State == CommunicationState.Faulted;
        _factory.Abort();
        if (faulted)
        {
            throw new ApplicationException(exception.Message);
        }
        else
        {
            throw;
        }
    }
    return contracts;
}

Calling code from Web Forms project:

public string[] GetAccounts()
{
    ReportClient client = NewReportClient();
    string[] results = null;

    try
    {
        results = client.GetAccounts();
        client.Close();
    }
    catch (Exception ex)
    {
        client.Abort();
        throw ex;
    }
    return results;
}

There are over fifty other methods like GetData() with different signatures. They will all be identical except for the service call in each, which will vary in params and return type. I need a more abstract, or generic, way of coding this and thus adhere to the DRY principle. Would Func<T, TResult> Delegate be appropriate here? Either way, can someone suggest a best approach here with some stub code to illustrate?

CodePudding user response:

I suppose that this is the case where Generic method with can be applied. It is possible to read about Generics here

Let me show a code example:

public class Foo
{ 
    public T GetDate<T, UArg>(UArg arg) where T : new()
    {
        return new T();
    }
}
  • Related