提取通过NHibernate封装的对象状态

huangapple go评论69阅读模式
英文:

Extract object encapsulated state through NHibernate

问题

我正在维护一个应用了领域驱动设计(DDD)原则的 WPF 应用程序。对封装状态(尽可能将字段设为私有)进行了重点关注。
聚合根的状态会通过 NHibernate 与 SQLite 数据库进行持久化。使用 FluentNHibernate 声明映射关系。
没有其他应用程序会访问这个数据库。

面临的问题是,我面临一个新需求,要求特定的聚合根可以通过在同一局域网中运行的不同计算机上的多个相同应用程序实例访问。

设想的解决方案是,我知道有许多可能的解决方案(对象数据库、通过 DTO 进行序列化、文件共享等)。但所有这些都需要大量的新工作来设置。

在我实现一种新的方法来提取这个状态之前,我想更多地了解一下是否可以利用内置在 NHibernate 中的一个功能:

为了持久化聚合根,NHibernate 能够(借助映射、自定义用户类型和大量的反射)提取原始值(字符串、整数、小数等),以便将它们注入到 SQL 查询中以进行持久化。

是否存在一种方法可以拦截这些原始值,以便我可以对它们进行其他处理?基本上,我的想法总结为不仅使用 NHibernate 将状态持久化在本地,还可以获取此状态的“序列化”版本(无需任何额外的配置或映射),以便我可以将其发送到另一端并使用 NHibernate 的帮助来恢复相同的对象(在另一端进行反向过程)。

到目前为止,在查看 NH 的部分位,我找到了 EntityEntry.LoadedState 的踪迹,这可能包含我正在寻找的信息。这对我的需求是否合适呢?

我应该如何:

  1. 获取我所需聚合根的该属性?
  2. 如何从该属性中恢复我的聚合根?
英文:

Context

I'm maintaining a WPF application which applies DDD principles. A strong focus has been made on encapsulating state (=making fields private as much as possible).
The state of the aggregates are persisted into an SQLite database with the help of NHibernate. Mappings are declared with the help of FluentNHibernate.
No other application is accessing the database.

Problem

I'm facing a new requirement which is that a specific aggregate has to be accessible through multiple instances of the same application running on different computers in the same LAN.

Envisioned solution

I know there are many possible solutions (object DB, serialization through DTO, file sharing, etc.). But all of these necessitate a lot of new work in order to set something up.

Before I implement a new way to extract this state, I'd like to have more insights of the possibility to use a feature that is baked into NHibernate:

In order to persist the aggregate, NHibernate is able (with the help of the mapping, custom user types and a lot of reflection) to extract primitive values (string, int, decimal, etc.) in order to inject them into SQL queries so that they are persisted.

Does it exist a way to intercept these raw values somewhere so that I can do something else with them ? Basically my idea summarizes as using NHibernate not only to persist the state locally but also to get a "serialized" version of this state (without any additional configuration or mapping) so that I can send it over the wire and hydrate the same object on the other end (doing the inverse process with the help of NHibernate too).

So far, after looking at the bits of NH I found the track of EntityEntry.LoadedState which could contain the information I'm looking for. Could this be suitable for my needs ?
How could I:

  1. Retrieve that property for my desired aggregate ?
  2. Hydrate my aggregate from that property ?

Following David Osborne's answer, I already have the 'entry point' which defines the when in the form of a domain event handler:

public class SaveCommittedOrderClosed : IDomainEventHandler<CommittedOrderClosed>
{
    public Result<Unit, Error> Handle(IUnitOfWork uow, CommittedOrderClosed @event)
    {
        //Could I use NH here to extract some state from @event.Order ?
    }
}

What I lack the knowledge of is the how.

答案1

得分: 1

你可以考虑使用事件监听器吗?这将为您提供一个'入口点',您可以在此处执行其他操作。

英文:

How about an event listener? This will give you the 'entry point' at which you could do the other actions.

huangapple
  • 本文由 发表于 2023年7月20日 18:10:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定