创建一个采用默认搜索标准作为构造函数参数的存储库是否有意义?

发布于 2024-08-22 23:29:01 字数 584 浏览 0 评论 0原文

我正在重构我建立的网站。这是一家旅行社的网站。我正在考虑实施存储库模式。

该旅行社有两个部门,拥有自己的网站并针对不同的客户群体。然而,两个网站的后端是相同的。他们都访问相同的数据库表。有些表仅包含用于标识记录所属部门的列。

假设我想要构建一个 TripRepository,它将能够返回 Trip 对象。您认为创建一个将 Division 作为构造函数参数的存储库有意义吗?这样所有的结果都只针对那个部门?

换句话说,在伪代码中:

class TripRepository
{
    method constructor( Devision );

    // this will then only return Trips for the Devision passed to the constructor
    method findAllTrips( /* some other criteria */ );
}

我当然可以将它作为可选的构造函数参数,甚至可以有一个setter来切换Devisions(如果需要)......但总的来说,这样做有什么异议吗?

或者我应该始终将划分标准传递给所有搜索方法?

I'm refactoring a website I build. It's a website for a travel agency. I'm thinking of implementing the repository pattern.

The travel agency has two divisions, that have their own website and target different groups of customers. The backend however is the same for both websites. They both access the same DB tables. Some tables simply have columns that identify what division the records are for.

Let's imagine I want to build a TripRepository, that will be able to return Trip objects. Do you feel it makes sense to create a Repository that takes the Division as a constructor argument? Such that all results will be only for that Division?

So in other words, in pseudo code:

class TripRepository
{
    method constructor( Devision );

    // this will then only return Trips for the Devision passed to the constructor
    method findAllTrips( /* some other criteria */ );
}

I could very well have it as an optional constructor argument of course, and even have a setter to switch Devisions if need be... but in general is there something objectional with doing this?

Or should I just always pass the Devision criterium to all search methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2024-08-29 23:29:01

除非您的 findAllTrips(...) 方法具有基于“Devision”的完全不同的逻辑(顺便说一句,它不应该是 Division 吗?),否则没有理由创建单独的实例。即使您出于某种原因这样做,我想您也必须将实例化的对象放入其他集合中,在这些集合中将使用... Devision 作为键(即参数)来访问它们。

因此,我个人认为这不是一个好的设计决策:看起来不直观,无论谁将来在代码中遇到这种情况,都可能想知道走这条路的真正动机是什么。

Unless your findAllTrips(...) method has completely different logic based on the "Devision" (shouldn't it be Division, btw?) there is no reason to create seperate instances. And even if you do for some reason, I suppose you will have to put the instantiated objects in some other collection, where they will be accessed using... Devision as a key (i.e. parameter).

So personally I think it wouldn't be a good design decision: doesn't look intuitive and whoever meet this in your code in the future will probably wonder what the real motive was to go this route.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文