Did we have an evolution from detached entities to CQRS?

Let’s say you have a software solution that has a data layer that accesses a SQL server. It can do this either directly and also over a HTTP connection. In both cases Datasets are used.

Now when migrating away from Datasets to a more modern technical solutions of course using an ORM framework might be the natural choice. However as there was support for accessing the database over HTTP before if you want to do a similar thing with the ORM framework it means you are working with detached entities.

So I am looking for strategies to tackle the migration. I could either use ORM with detached entities and do change tracking by myself.

A first attempt using TrackableEntities was rather a disaster (in this case we tried EF 6).

A second attempt looks quite promising to me when using an object diffing framework as for example ObjectDiffPatch in combination with NHibernate. This would mean that when wanting to save an object a diff is created with the help of a previous snapshot. This diff is send over HTTP to the database server where the diff is applied to the entity in the NHibernate session. I learned you cannot send the NHibernate entity directly but instead should use a separate DTO. Also these articles (Code First is bad for Domain, Don’t Use ORM Entities To Model The Domain) suggest that I should anyway have a separation between my domain models and persistence models (regarding this articles, is this just the opinion of that author or a general best practice?). The drawback of this means having multiple class definition (maybe some stuff can be auto-generated?). But conceptually diffing seems to work in my prototypes very well.

Another option seems to be to use CQRS and change object always with commands. So you can also easily send command over HTTP.

Is it that CQRS can be seen as the logical follow-up solution as ORM framework don’t provide something like detached entities straight away? Some have suggested to use a task based messaging system (see Tasks, Messages, & Transactions – the holy trinity) which looks very similar to CQRS. Is this how CQRS evolved as a mainstream solution to the detached entity problem?

I read that handling detached entities is a messy adventure and some people call it an anti-pattern (the stripper pattern). Currently I think that CQRS is what people have come up with as a solution to the problem. Is this true?

Can you please also provide some thoughts about the idea of working with diffing to solve the detached entity problem. Maybe you know already a scenario where something like this has been used.


Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *