【发布时间】: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