【问题标题】:3 tiers nhibernate + wcf + Silverlight3 层 nhibernate + wcf + Silverlight
【发布时间】:2013-01-07 02:48:58
【问题描述】:

最近我被要求开发一个项目。架构如下:

  • 1 层:基于 Nhibernate 的 DataAccess
  • 2层:基于WCF服务和一些Core类的业务层
  • 3 层:基于 Silverlight 的视图

我将使用 DTO 对象在第 2 层和第 3 层之间传递数据。

我已经意识到该项目将拥有庞大的领域模型,并且许多业务实体应该支持标准和自定义 CRUD 操作。在第一层,它将由通用 NHibernate Repository + Specification 解决。

但是第 2 层(一个 WCF 服务)看起来像一组方法,为第 3 层提供 DTO 的自定义和标准 CRUD 接口。

例如模型看起来像:

class Product {}
class Category {}

DTO:

class ProductDTO {}
class CategoryDTO {}

“问题”WCF 服务:

public class DataService
{
  public List<CategoryDTO> GetAllCategories()
  {
  }

  public List<ProductDTO> GetAllProducts()
  {
  }
}

可能的解决方案:

public class ProductDataService
{
  public List<ProductDTO> GetAllProducts()
  {
  }
}

 public class CategoryDataService
{
  public List<CategoryDTO> GetAllCategories()
  {
  }
}

问题:

  1. 上面列出的解决方案有什么好的替代方案吗?
  2. 在这种情况下,WCF 服务中是否可以使用任何“通用”方式?

【问题讨论】:

    标签: wcf silverlight nhibernate architecture n-tier-architecture


    【解决方案1】:

    上面列出的解决方案有什么好的替代方案吗?

    是的,您可以使用 RESTfull 服务。我还建议不要使用胖接口(mathieu 解决方案)。胖接口很难维护、重构,它们笨重。如果你选择 REST,你可以拥有这样的 API(你可以在 WCF 中拥有类似的接口):

    在这种情况下,WCF 服务中是否可以使用任何“通用”方式?

    是的,您可以在服务器上使用泛型,但对于客户端,这将显示为具体类型。示例见this post

    【讨论】:

      【解决方案2】:

      假设您纯粹谈论“参考”数据(类别、产品等)上的 CRUD,您应该有 1 个服务,其中包含许多 GetAllXXX 方法或多个服务,最终继承自一个基础服务。真的没关系,如果你有很多DTO要传输,你会得到很多GetXXX方法。

      请注意这两点:

      • wcf 服务中的方法数量更多,“预热”时间更长(此问题出现在大约 100-200 个方法 (1) 中
      • wcf 不能在没有定义泛型参数的情况下公开“泛型”方法

      例如:

      public class DataService<TIn>
      {
          protected List<TOut> GetAll<TOut>()
          {
               // handle generic loading and transformation here
          }
      }
      
      public class CategoryService : DataService<Category>
      {
          public List<CategoryDTO> GetAllCategories()
          {
              return GetAll<CategoryDTO>();
          }
      }
      

      public class DataService
      {
          protected List<TOut> GetAll<TIn, TOut>()
          {
               // handle generic loading and transformation here
          }
      
          public List<CategoryDTO> GetAllCategories()
          {
              return GetAll<Category, CategoryDTO>();
          }
      }
      

      然后在泛型方法中,您可以使用 Automapper 从 Category 映射到 CategoryDto, 例如。

      最后,对于更多面向用户/业务的服务,您应该有特定于您将在每个“视图”中呈现的数据的 DTO。

      (1) 是的,这是从 OOP 的角度来看的。但是您往往会通过从大型数据库生成代码来解决这些问题 :)

      【讨论】:

        猜你喜欢
        • 2011-01-10
        • 2011-05-14
        • 2011-07-26
        • 1970-01-01
        • 2011-03-02
        • 2011-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多