【问题标题】:Mixed architecural approach between layered or shared entities分层或共享实体之间的混合架构方法
【发布时间】:2014-12-12 23:23:13
【问题描述】:

我们正在开发一个包含以下层的应用程序:

  • 用户界面
  • 业务层 (BL)
  • 数据层 (DL):包含通用 CRUD 查询和自定义查询
  • 物理数据层 (PDL):例如实体框架

我们正在寻找一种将物理数据层的实体共享给 DL 和 BL 的方法。

这些点对于决定最佳架构很重要:

  • 可重用性:应尽可能轻松地将数据库字段迁移到其他层
  • 快速实现:向数据库添加字段不应导致在所有层之间映射实体
  • 可扩展性:可以使用特定于 BL 的属性扩展 BL 实体(对于 DL 实体也是如此)

我遇到过为所有层共享实体的架构(+ 快速实现,- 可扩展性)或每层具有一个实体 (DTO) 的架构(+ 可扩展性,- 快速实现/可重用性)。 This blogpost 描述了这两种架构。

是否有一种方法可以结合这些架构并考虑我们的要求?

目前我们已经提出了以下类和接口:

接口:

// Contains properties shared for all entities
public interface I_DL
{
    bool Active { get; set; }
}

// Contains properties specific for a customer
public interface I_DL_Customer : I_DL
{
    string Name { get; set; }
}

PDL

// Generated by EF or mocking object
public partial class Customer
{
    public bool Active { get; set; }
    public string Name { get; set; }
}

DL

// Extend the generated entity with custom behaviour
public partial class Customer : I_DL_Customer
{

}

BL

// Store a reference to the DL entity and define the properties shared for all entities
public abstract class BL_Entity<T> where T : I_DL
{
    private T _entity;

    public BL_Entity(T entity)
    {
        _entity = entity;
    }

    protected T entity
    {
        get { return _entity; }
        set { _entity = value; }
    }

    public bool Active
    {
        get
        {
            return entity.Active;
        }
        set
        {
            entity.Active = value;
        }
    }

}

// The BL customer maps directly to the DL customer
public class BL_Customer : BL_Entity<I_DL_Customer>
{
    public BL_Customer (I_DL_Customer o) : base(o) { }

    public string Name
    {
        get
        {
            return entity.Name;
        }
        set
        {
            entity.Name = value;
        }
    }
}

【问题讨论】:

    标签: c# architecture


    【解决方案1】:

    DTO-per-layer 设计是最灵活和模块化的。因此,它也是最可重用的:不要将重用相同实体的便利性与架构级别主要关注的不同模块的可重用性混淆。但是,正如您所指出的,如果您的实体经常变化,这种方法既不是最快的开发也不是最敏捷的。

    如果您想在各层之间共享实体,我不会费心通过不同层指定层次结构;我要么让所有层直接使用 EF 实体,要么在所有层共享的不同程序集中定义这些实体——包括物理数据层,它可以通过 EF 代码优先直接保留这些实体,或者转换为/从那些共享实体到 EF 实体。

    【讨论】:

    • 我们确实决定走我所描述的道路。很高兴听到我们走在正确的轨道上。
    猜你喜欢
    • 2013-10-07
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 2012-11-10
    • 2020-01-30
    • 2017-07-08
    相关资源
    最近更新 更多