在DDD和CQRS中,对于读取查询,什么是允许界面和易于测试的策略? [英] In DDD and CQRS, for Read queries, what is a strategy that allows for interfaces and easy testing?

查看:92
本文介绍了在DDD和CQRS中,对于读取查询,什么是允许界面和易于测试的策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP / MySQL ...

I'm using PHP/MySQL...

我正在使用域的命令侧存储库接口。一切顺利。但是,我在查询(阅读)方面坚持要做的事情。我是否会根据发现的一些共同点将查询作为单独的方法分组在类中?还是将每个查询都设为自己的类?我应该如何使用界面进行测试(以后更容易更换)?

I'm using interfaces for repositories on my Domain "command" side. This is going well. But I'm stuck on what to do on the "queries" (read) side. Do I make queries each as separate methods grouped in classes by some common similarity I find? Or do I make each query its own class? How should I go about using interfaces to make testing (and easier replacement later)?

我研究过的一些地方:

  • A github php example
  • An attempt to find the best decoupled approach to the read-side queries. Examples use C# unfortunately and make it a bit more challenging to grasp.

这些解决方案令人困惑吗?

These solutions are convoluted?

推荐答案

从第二个c#链接访问我们的项目之一。如果您想使用某种查询外观,那将是我遇到的最好的外观。我最喜欢的是创建查询对象和查询处理程序,然后由通用查询处理器执行将对象传递给正确的处理程序的工作。

We currently use the approach from the second c# link for one of our projects. If you want to use a query facade of some kind, then that's the best one I've come across. What I like most about it is that you create query objects and query handlers, and then a generic query processor does the work of passing the object to the correct handler.

总体它运作良好,但是仍然觉得我在努力工作,而不是需要的。除了需要维护更多内容外,我不确定该查询抽象给我带来了什么。可测试性通常是对该问题的回答,但是我不对单元查询进行单元化。我认为单元测试最好留给可能破坏/影响其他代码的代码。查询是否可以破坏常规测试中不会立即显现的任何内容?

Overall it works well, however it still feels like I'm working harder than I need to. I'm not really sure what this query abstraction is giving me, other than having more to maintain. Testability is often the response to that question, but I for one do not unit test queries. Unit testing in my opinion is best left for code that can potentially break/affect other code. Can a query really break anything that wouldn't be immediately apparent during regular testing? It either works or it doesn't.

如果确实有令人信服的理由使用立面,则建议将与查询相关的内容归类到一个界面中。例如 IBillingQueries和 ICustomerQueries。

If you do have a compelling reason to use a facade, then I recommend grouping the queries that are related into an interface. Such as "IBillingQueries" and "ICustomerQueries".

这篇关于在DDD和CQRS中,对于读取查询,什么是允许界面和易于测试的策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆