1

I have an existing web application in which asp.net client uses the business via using the dll of the business project. Code for the business project is given below.

Now, we need to make it as WCF web service. I need to create a service interface (contract) from the existing class BankAccountService. But the class has an overloaded constructor. This constructor cannot be defined in the service interface (as web service is not object oriented). What is the best solution I can use here to make it working as a web service?

Do we need to create any facade rather than making BankAccountService as the service implementation?

Note: I understand that it is not a good practice to extract interface after implementing the service. But for this special scenario we need to do this.

Note: This is a very simple application and this is the only functionality needed in the application. The website will be the only client using the service.

Reference:

1 Interface defining a constructor signature?

CODE

namespace ApplicationServiceForBank
{

public class BankAccountService
{

RepositoryLayer.IRepository<RepositoryLayer.BankAccount> accountRepository;
ApplicationServiceForBank.IBankAccountFactory bankFactory;

public BankAccountService(RepositoryLayer.IRepository<RepositoryLayer.BankAccount> repo, IBankAccountFactory bankFact)
{
    accountRepository = repo;
    bankFactory = bankFact;
}

public void FreezeAllAccountsForUser(int userId)
{
    IEnumerable<RepositoryLayer.BankAccount> accountsForUser = accountRepository.FindAll(p => p.BankUser.UserID == userId);
    foreach (RepositoryLayer.BankAccount repositroyAccount in accountsForUser)
    {
        DomainObjectsForBank.IBankAccount acc = null;
        acc = bankFactory.CreateAccount(repositroyAccount);
        if (acc != null)
        {
            acc.BankAccountID = repositroyAccount.BankAccountID;
            acc.accountRepository = this.accountRepository;
            acc.FreezeAccount();
        }
    }
  }
}
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • This needs some deep analysis because you have a truckload of additional concerns when you move from offline (dll) to online (wcf service). It's meeting time for your company! – Alex Jun 27 '12 at 15:16
  • @Alex This is a very simple application and this is the only functionality needed in the application. The website will be the only client using the service. Can you please list the concerns that may occur? – LCJ Jun 27 '12 at 15:19
  • What if, you abstract the existing service class into perhaps a Business Layer/Service Layer and call that from the Web/Wcf service? – Siva Gopal Jun 27 '12 at 15:50

1 Answers1

1

If you are going to expose the class as a WCF service, then the service itself needs to build up the necessary objects in the constructor, i.e., RepositoryLayer.IRepository<RepositoryLayer.BankAccount> and ApplicationServiceForBank.IBankAccountFactory. These are objects that are used to retrieve the data which the consumer of the service should be agnostic.

In this case, you could expose BankAccountService through WCF and create various methods that define the type of repository/factory needed to retrieve the data:

[ServiceContract]
public interface IBankAccountService
{
    [OperationContract]
    void FreezeAllAccountsForUserByAccountTypeFoo(int userId);

    [OperationContract]
    void FreezeAllAccountsForUserByAccountTypeBar(int userId);

    [OperationContract]
    void FreezeAllAccountsForUserByAccountTypeEtc(int userId);
}

Since it sounds like you are controlling both sides of the service, another option is to pass in an enum to the method which would define the type of repository/factory needed:

public enum RepoFactory
{
    Foo, Bar, Etc,
}

[ServiceContract]
public interface IBankAccountService
{
    [OperationContract]
    void FreezeAllAccountsForUser(int userId, RepoFactory repoFactory);
}
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140