Architecture 在主要是DDD的应用程序中放置特别命令/查询的位置
我正在使用领域驱动的设计方法开发一个web应用程序,但是我的应用程序的某些方面并不适合DDD。例如,我们需要对员工工资进行批量更新。加载整个员工实体并为每个员工保存它是没有效率的,因为通常一次更新数百份工资。此外,还需要同时执行其他任务,例如记录旧工资和记录新工资的生效日期。所以我想说,这种类型的操作超出了我的核心域的有界上下文。此外,我还了解到,最好从程序的角度来处理该操作。这一切都很好,但是我仍然希望我的应用程序是连贯的和有组织的,无论我在应用程序的特定部分使用什么方法 例如,我使用以下结构Architecture 在主要是DDD的应用程序中放置特别命令/查询的位置,architecture,domain-driven-design,ddd-repositories,procedural-programming,bounded-contexts,Architecture,Domain Driven Design,Ddd Repositories,Procedural Programming,Bounded Contexts,我正在使用领域驱动的设计方法开发一个web应用程序,但是我的应用程序的某些方面并不适合DDD。例如,我们需要对员工工资进行批量更新。加载整个员工实体并为每个员工保存它是没有效率的,因为通常一次更新数百份工资。此外,还需要同时执行其他任务,例如记录旧工资和记录新工资的生效日期。所以我想说,这种类型的操作超出了我的核心域的有界上下文。此外,我还了解到,最好从程序的角度来处理该操作。这一切都很好,但是我仍然希望我的应用程序是连贯的和有组织的,无论我在应用程序的特定部分使用什么方法 例如,我使用以下结构
- 用户界面
- 应用
- 模型
- 基础设施
- 存储库
- 查找器(用于单独读取模型)
- 命令
我目前正在考虑使用名称空间“过程”来描述逻辑上结合在一起的这些查询/命令。一方面,它是合适的,因为我所描述的与存储的过程类似,并且应用程序的这一部分使用的是过程而不是DDD方法,这是描述性的。我唯一担心的是,这种命名约定意味着使用存储过程,事实并非如此 命令/查询在应用程序职责中,查看六边形体系结构。您可以将临时命令和查询作为适配器/端口添加到应用程序中我让我的存储库能够开始事务,然后将TransactionCommand传递给所有相关的存储库。一旦最终的存储库完成更新,我就提交它。这样,我仍然可以使用现有的存储库(基本上每个模型/实体一个存储库),而无需编写特殊/自定义查询 我只希望让我的模型/实体自己实现存储库接口,而不是为每个接口定义一个单独的存储库。前者允许多态存储库操作,后者则不允许。活活烧死 下面是事务存储库的用法,此方法取自Entity1Service类(我的服务层,每个实体一个类,包含使用一个或多个存储库来完成某些目标的静态方法) 存储库定义
public interface ITransaction
{
SqliteCommand TransactionCommand { get; set; }
bool InTransaction { get; }
void BeginTransaction();
void RollbackTransaction();
void CloseTransaction();
void EndTransaction();
}
public class RepositorySQLiteTransactional<T> : RepositorySQLite<T>, ITransaction where T : new()
{
public RepositorySQLiteTransactional(IDataMapperSQLite<T, string[]> dataMapper) : base(dataMapper) { }
public RepositorySQLiteTransactional(IDataMapperSQLite<T, string[]> dataMapper, SqliteCommand transactionCommand) : this(dataMapper)
{
_dbLayer.DblTransactionCommand = transactionCommand;
}
public SqliteCommand TransactionCommand
{
get { return (SqliteCommand)_dbLayer.DblTransactionCommand; }
set
{
_dbLayer.DblTransactionCommand = value;
}
}
public bool InTransaction
{
get { return _dbLayer.DblInTransaction; }
}
public void BeginTransaction()
{
try
{
_dbLayer.DblBeginTransaction();
}
catch( Exception )
{
_dbLayer.DblCloseTransaction();
throw;
}
}
public void RollbackTransaction()
{
_dbLayer.DblRollbackTransaction();
}
public void CloseTransaction()
{
_dbLayer.DblCloseTransaction();
}
public void EndTransaction()
{
_dbLayer.DblEndTransaction();
}
}
public interface IRepository<T>
{
List<T> Read(IConditions conditions);
T FindOne(IQuery query);
List<T> FindAll(IQuery query);
void WriteOne(T obj);
void WriteOne(T obj, out int newId);
void WriteOne(IQuery query);
void WriteAll(List<T> objs);
void UpdateOne(T obj);
void UpdateAll(List<T> objs);
void UpdateOne(IQuery query);
void ReplaceAll(List<T> objs);
void DeleteAll();
void DeleteAll(List<T> objs);
//void Add(T entity);
//void Delete(T entity);
//void Edit(T entity);
//void Save();
}
public class RepositorySQLite<T> : IRepository<T> where T : new()
{
protected AndroidDB _dbLayer;
protected IDataMapperSQLite<T, string[]> _dataMapper;
private RepositorySQLite() // force data mapper init
{
}
public RepositorySQLite(IDataMapperSQLite<T, string[]> dataMapper)
{
}
public List<T> Read(IConditions conditions) { throw new NotImplementedException(); }
public void WriteOne(T obj, out int newId) { throw new NotImplementedException(); }
public void WriteOne(IQuery query) { throw new NotImplementedException(); }
private void ClearMapState()
{
}
public void ReplaceAll(List<T> objs)
{
}
public void WriteAll(List<T> objList)
{
}
public void WriteOne(T obj)
{
}
public void UpdateOne(T obj)
{
}
public void UpdateAll(List<T> objs)
{
}
public void UpdateOne(IQuery query)
{
}
public T FindOne(IQuery query)
{
}
public List<T> FindAll(IQuery query)
{
}
public void DeleteAll(List<T> objs)
{
}
public void DeleteAll()
{
}
public void DeleteAll( IQuery query )
{
}
}
公共接口ITransaction
{
SqliteCommand TransactionCommand{get;set;}
布尔内部交换{get;}
void beginsaction();
作废回滚事务();
void CloseTransaction();
作废EndTransaction();
}
公共类RepositorySQLiteTransactional:RepositorySQLite,ITransaction其中T:new()
{
公共RepositorySQLiteTransactional(IDataMapperSQLite数据映射器):基本(数据映射器){}
public RepositorySQLiteTransactional(IDataMapperSQLite数据映射器,SqliteCommand transactionCommand):此(数据映射器)
{
_dbLayer.DblTransactionCommand=transactionCommand;
}
公共SqliteCommand TransactionCommand
{
获取{return(SqliteCommand)u dbLayer.DblTransactionCommand;}
设置
{
_dbLayer.DblTransactionCommand=值;
}
}
公共场所内交易
{
获取{return}dbLayer.DblInTransaction;}
}
公共无效开始生效()
{
尝试
{
_dbLayer.DblBeginTransaction();
}
捕获(例外)
{
_dbLayer.DblCloseTransaction();
投掷;
}
}
公共无效回滚事务()
{
_dbLayer.DblRollbackTransaction();
}
公共事务()
{
_dbLayer.DblCloseTransaction();
}
公开作废EndTransaction()
{
_dbLayer.DblEndTransaction();
}
}
公共接口假定
{
列表读取(图标条件);
T FindOne(IQuery查询);
列表FindAll(IQuery查询);
无效书面通知(T obj);
无效写入(T obj,out int newId);
无效写单(IQuery查询);
无效写入(列出对象);
无效更新(T obj);
void UpdateAll(列出对象);
void UpdateOne(IQuery查询);
void ReplaceAll(列出对象);
void DeleteAll();
void DeleteAll(列出对象);
//无效添加(T实体);
//无效删除(T实体);
//无效编辑(T实体);
//作废保存();
}
公共类RepositorySQLite:IRepository,其中T:new()
{
受保护的AndroidDB_数据层;
受保护的IDataMapperSQLite\u数据映射器;
private RepositorySQLite()//强制数据映射器初始化
{
}
公共存储SQLite(IDataMapperSQLite数据映射器)
{
}
公共列表读取(IConditions条件){throw new notimplemented
public interface ITransaction
{
SqliteCommand TransactionCommand { get; set; }
bool InTransaction { get; }
void BeginTransaction();
void RollbackTransaction();
void CloseTransaction();
void EndTransaction();
}
public class RepositorySQLiteTransactional<T> : RepositorySQLite<T>, ITransaction where T : new()
{
public RepositorySQLiteTransactional(IDataMapperSQLite<T, string[]> dataMapper) : base(dataMapper) { }
public RepositorySQLiteTransactional(IDataMapperSQLite<T, string[]> dataMapper, SqliteCommand transactionCommand) : this(dataMapper)
{
_dbLayer.DblTransactionCommand = transactionCommand;
}
public SqliteCommand TransactionCommand
{
get { return (SqliteCommand)_dbLayer.DblTransactionCommand; }
set
{
_dbLayer.DblTransactionCommand = value;
}
}
public bool InTransaction
{
get { return _dbLayer.DblInTransaction; }
}
public void BeginTransaction()
{
try
{
_dbLayer.DblBeginTransaction();
}
catch( Exception )
{
_dbLayer.DblCloseTransaction();
throw;
}
}
public void RollbackTransaction()
{
_dbLayer.DblRollbackTransaction();
}
public void CloseTransaction()
{
_dbLayer.DblCloseTransaction();
}
public void EndTransaction()
{
_dbLayer.DblEndTransaction();
}
}
public interface IRepository<T>
{
List<T> Read(IConditions conditions);
T FindOne(IQuery query);
List<T> FindAll(IQuery query);
void WriteOne(T obj);
void WriteOne(T obj, out int newId);
void WriteOne(IQuery query);
void WriteAll(List<T> objs);
void UpdateOne(T obj);
void UpdateAll(List<T> objs);
void UpdateOne(IQuery query);
void ReplaceAll(List<T> objs);
void DeleteAll();
void DeleteAll(List<T> objs);
//void Add(T entity);
//void Delete(T entity);
//void Edit(T entity);
//void Save();
}
public class RepositorySQLite<T> : IRepository<T> where T : new()
{
protected AndroidDB _dbLayer;
protected IDataMapperSQLite<T, string[]> _dataMapper;
private RepositorySQLite() // force data mapper init
{
}
public RepositorySQLite(IDataMapperSQLite<T, string[]> dataMapper)
{
}
public List<T> Read(IConditions conditions) { throw new NotImplementedException(); }
public void WriteOne(T obj, out int newId) { throw new NotImplementedException(); }
public void WriteOne(IQuery query) { throw new NotImplementedException(); }
private void ClearMapState()
{
}
public void ReplaceAll(List<T> objs)
{
}
public void WriteAll(List<T> objList)
{
}
public void WriteOne(T obj)
{
}
public void UpdateOne(T obj)
{
}
public void UpdateAll(List<T> objs)
{
}
public void UpdateOne(IQuery query)
{
}
public T FindOne(IQuery query)
{
}
public List<T> FindAll(IQuery query)
{
}
public void DeleteAll(List<T> objs)
{
}
public void DeleteAll()
{
}
public void DeleteAll( IQuery query )
{
}
}
public interface IManageSalaries
{
void ChangeEmployeeSalary(Guid employee, Salary newSalary,DateTime startingFrom);
//possible bulk operation
void ChangeSalary(Guid[] employees, Salary newSalary,DateTime startingFrom);
SalaryInfo[] GetSalaryHistory(Guid employeeId);
Salary GetCurrentSalary(Guid employeeId);
}