尝试返回给定 id 和相同 id 之间的行 + offset,大多数逻辑应该去哪里来确定如何检索数据?:BLL 或 DAL
在我的数据库中,有一个名为 ThingsInACircle
的表。 每次将 Thing
添加到 ThingsInACircle
时,都会添加一个自动递增的 ThingId
。
想象一下此表中的 Things
位于一个圆圈中。SELECT Thing FROM ThingsInACircle WHERE ThingId = 10
位于SELECT Thing FROM ThingsInACircle WHERE ThingId = 11
。
另外...从 ThingsInACircle 中选择事物 WHERE ThingId = min(ThingId)
位于从 ThingsInACircle 中选择事物WHERE ThingId = max(ThingId)
我希望能够说:对于给定的 ThingId 和偏移量,返回 ThingsInACircle 中从 ThingId 到 (ThingId + offset) 的所有记录(偏移量可能为负) 。
因此,以此为例进行设置:
这是我们的 ThingId 列表: 1 2 3 4 5 6 7 8 9
如果我想要 @ThingId = 2 和 @offset = 3 的所有 ThingId,那么没有问题
SELECT ThingId FROM ThingsInACircle WHERE
ThingId BETWEEN @ThingId AND (@ThingId + @offset)
,它会返回: 2 3 4 5
但是,如果我想要 @ThingId = 8 和 @offset = 3 的所有 ThingId,那么就会出现问题
该函数应该返回: 7 8 9 1
所以这是我的困境,我的数据访问应该如何层包含一个查询,该查询使用更复杂的存储过程(选择最小值、最大值,并使用 if 和 else 来确定它们是否已被超出)准确确定要检索哪些记录以便将记录视为链接成一个圆圈?
或者业务逻辑层应该确定用户是否请求了超过最小值或最大值的 id + 偏移量,然后使用简单的 DAL 方法来完成它需要返回的内容?
这可能只是一个见仁见智的问题。 我两天前刚刚开始学习三层结构,按照 microsoft 教程,所以我只是想了解一些人认为 DAL 和 BLL 应该如何形成:如果 DAL 应该很简单,BLL 应该做所有的验证......或者相反......或者我的东西一起错过了。
In my database I have a table called ThingsInACircle
. Every time a Thing
is added to the ThingsInACircle
, it gets added with a ThingId
that is auto incremented.
Imagine the Things
in this table as being in a circle.SELECT Thing FROM ThingsInACircle WHERE ThingId = 10
is next toSELECT Thing FROM ThingsInACircle WHERE ThingId = 11
.
Also...SELECT Thing FROM ThingsInACircle WHERE ThingId = min(ThingId)
is next toSELECT Thing FROM ThingsInACircle WHERE ThingId = max(ThingId)
I want to be able to say: For a given ThingId and an offset, return all of the records in ThingsInACircle ranging from the ThingId to (ThingId + offset) (offset may be negative).
So, take this as an example set up:
Here is a list of our ThingIds: 1 2 3 4 5 6 7 8 9
If I want all the ThingIds where @ThingId = 2 and @offset = 3, there's no problem
SELECT ThingId FROM ThingsInACircle WHERE
ThingId BETWEEN @ThingId AND (@ThingId + @offset)
and it would return: 2 3 4 5
But, if I want all the ThingIds where @ThingId = 8 and @offset = 3, then there's a problem
The function should return: 7 8 9 1
So here's my dilemma, should my Data Access Layer contain a query that uses a more complicated stored procedure (Selecting min, max, and using if & else to determine if they have been exceeded) to determine exactly which records to retrieve in order to treat the records as being linked in a circle?
Or should the Business Logic Layer determine whether or not the user has requested an id + offset that exceeds min or max and then use simple DAL methods to accomplish what it needs to return?
This may just be a matter of opinion. I just started learning about three-tiered structure two days ago by following microsofts tutorial, so I just want to get an idea of how some people think DAL and BLL should be formed: If DAL should be simple and BLL should do all of the verifying... Or the other way around... Or something I've missed all together.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@MaxID是最大的事物id(圆的末端)。
有两种情况:
正常情况是偏移量没有溢出。 这完全被第二个之间所覆盖。
第二种情况是当发生溢出时,您需要在边界上应用偏移量。 在本例中,范围为 @ThingID - @ThingID + @offset,由第一个 BETWEEN 处理,范围为 1 - ((@ThingID + @offset) - @MaxID),由第二个 BETWEEN 处理。
如果offset高于@MaxID,则必须考虑所有@ThingID,并且这种情况也被第二个之间覆盖。
@MaxID is the maximum thing id (end of the circle).
You have two cases:
normal case is when you have no overflow on offset. This is covered entirely by the second between.
second case is when you have an overflow, and you need to apply the offset over the boundary. In this case you have the range @ThingID - @ThingID + @offset, handled by the first BETWEEN, and 1 - ((@ThingID + @offset) - @MaxID), handled by the second BETWEEN.
If offset is higher than @MaxID, then all the @ThingIDs have to be considered, and this case is also covered by the second between.
我通常发现这些在其他地方比 DAL 更容易处理,尤其是在处理 RDBMS 时。 其他类型的集合抽象(数组、集合、字典等)将更容易使用。
I usually find these easier to deal with elsewhere than the DAL, especially if you're dealing with an RDBMS. Other kinds of collection abstractions (arrays, collections, dictionaries, etc.) will be easier to work with.
大多数时候(在这种情况下),我建议从业务层的业务逻辑开始(即圆圈中的最后一件事紧挨着第一件事)。
这意味着至少执行两次查询。 第一个获取您的 thingID 范围:
因此您的第二个查询通常是:
参数预先计算为:
然后是第三个(如有必要):
如果您开始看到性能问题或存在数据完整性问题等,您仍然可以考虑将此逻辑移至 DAL 中。
Most of the time (and in this situation), I would recommend starting with business logic (i.e. the last thing in a circle is next to the first thing) in the business layer.
This means doing at least two queries. The first gets your range of thingIDs:
so your second query will normally be:
where the parameters were precomputed as:
and then a third, if necessary:
If you start seeing performance problems or otherwise have issues with data integrity, etc. you can still consider moving this logic into the DAL.