我正在考虑使用 NoSQL 数据库来扩展数据库读取
好主意。听起来您正在走Command Query Responsibility Segregation(CQRS) 的道路。 NoSql 数据库是优秀的读取存储。
您引用的link 描述了一种更新技术
使用 NHibernate 组合 SQL Server 和 MongoDB - 这就是我所说的“组合”(组合)存储库的意思。 “组合存储库”不是标准模式。引用作者的话:
当然,理论上,我们可以将所有数据移动到某个 NoSQL 存储中,但如果我们不想完全摆脱关系数据库怎么办?我们如何将它们组合在一起?
您已经使用Sql-Server 和NoSql 数据库标记了您的原始问题,所以猜测您在Polyglot space 中
Repository Pattern 是一个非常常见的围绕数据持久性的抽象层。
您引用的“组合”链接专门解决了多对多关系(在 Sql 中通常称为连接表)的问题,以及存在许多此类关系时的性能影响。
在更一般的意义上,作为在 NHibernate 中提供拦截点的替代方案,您可能/可能没有通过存储库模式进行抽象数据访问。
这是一个超简单(非通用)的 C# 存储库接口:
public interface IWidgetRepository
{
Task<Widget> FetchWidget(string widgetKey);
Task SaveWidget(Widget toBeSaved);
}
假设我们已经有一个 SqlRepository:
public class SqlWidgetRepository : IWidgetRepository
{
public async Task<Widget> FetchWidget(string widgetKey)
{
... Code to use Obtain an NHibernate session and retrieve and deserialize Widget
}
... Other methods here
}
您也可以选择提供MongoDb 实现
public class MongoWidgetRepository : IWidgetRepository
{
public async Task<Widget> FetchWidget(string widgetKey)
{
... Code to connect to a MongoDb secondary and Find() the
widget and deserialiaze into Widget
}
... Other methods here
}
为了同时维护两个数据库,下面是这个“组合”存储库的示例:
public class ComboWidgetRepository : IWidgetRepository
{
private readonly IWidgetRepository _repo1;
private readonly IWidgetRepository _repo2;
public ComboWidgetRepository(IWidgetRepository repo1, IWidgetRepository repo2)
{
repo1 = repo1;
repo1 = repo2;
}
public async Task<Widget> FetchWidget(string widgetKey)
{
// Just need the one ... first one wins
return await Task.WhenAny(repo1.FetchWidget(widgetKey),
repo2.FetchWidget(widgetKey));
}
public async Task SaveWidget(Widget toBeSaved)
{
// Need both to be saved
await Task.WhenAll(repo1.SaveWidget(toBeSaved),
repo2.SaveWidget(toBeSaved));
}
上述“组合”存储库可以很好地满足单个系统的需求(还有许多其他方法可以使两个数据库保持同步)。
然而,CQRS 经常用于企业规模(即您有许多系统和许多数据库的地方)。
我对@987654325@ 的评论只有在您需要在整个企业中分发数据时才有意义。
概念很简单
- 命令通过总线排队到事务系统(例如“添加小部件”)。
- 您的系统处理小部件执行事务(例如,将小部件插入数据库)
-
Widget 系统随后向总线发布(广播)一条消息,详细说明已添加新小部件(包含所有相关小部件信息)
- 企业中对更新小部件感兴趣的其他系统订阅此消息并将更新他们自己的小部件
Read Store 表示形式(例如,更新到 NoSql 数据库或缓存中,并以最有意义的格式他们)。
- 这样,当用户访问任何其他系统并查看有关“小部件”的屏幕时,系统可以从自己的读取存储中提供数据,而不必从
Widget 系统本身请求数据.