1

我有一个现有的 Web 应用程序,其中 asp.net 客户端通过使用业务项目的 dll 来使用业务。商业项目的代码如下。

现在,我们需要将其设为 WCF Web 服务。我需要从现有的 BankAccountService 类创建一个服务接口(合同)。但是该类有一个重载的构造函数。此构造函数不能在服务接口中定义(因为 Web 服务不是面向对象的)。我可以在这里使用什么使其作为 Web 服务运行的最佳解决方案是什么?

我们是否需要创建任何外观而不是将 BankAccountService 作为服务实现?

注意:我了解在实现服务后提取接口不是一个好习惯。但是对于这种特殊情况,我们需要这样做。

注意:这是一个非常简单的应用程序,这是应用程序中唯一需要的功能。该网站将是唯一使用该服务的客户。

参考:

1 定义构造函数签名的接口?

代码

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

1 回答 1

1

如果要将类公开为 WCF 服务,则服务本身需要在构造函数中构建必要的对象,RepositoryLayer.IRepository<RepositoryLayer.BankAccount>ApplicationServiceForBank.IBankAccountFactory. 这些是用于检索服务消费者应该不可知的数据的对象。

在这种情况下,您可以通过 WCF 公开BankAccountService并创建各种方法来定义检索数据所需的存储库/工厂类型:

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

    [OperationContract]
    void FreezeAllAccountsForUserByAccountTypeBar(int userId);

    [OperationContract]
    void FreezeAllAccountsForUserByAccountTypeEtc(int userId);
}

由于听起来您正在控制服务的双方,因此另一种选择是将枚举传递给定义所需存储库/工厂类型的方法:

public enum RepoFactory
{
    Foo, Bar, Etc,
}

[ServiceContract]
public interface IBankAccountService
{
    [OperationContract]
    void FreezeAllAccountsForUser(int userId, RepoFactory repoFactory);
}
于 2012-06-27T15:48:11.183 回答