有人对 n 层 Web 应用程序的类命名有好的文章或好的建议吗?

发布于 2024-07-05 16:51:05 字数 871 浏览 4 评论 0原文

我习惯了 LLBLGen 在基于数据库结构生成对象时提供的布局,这可能会为数据库中给定的“用户”表生成以下类文件:

/EntityClasses/UserEntity.vb
/CollectionClasses/UserCollection.vb

这为数据访问提供了一些基本功能。 但是,当您想在此基础上实现业务逻辑时,您将如何布局? 例如,给定一个可能如下所示的表结构:

 USER
 userId
 firstName
 lastName
 username
 password
 lockedOut

如果您想锁定用户怎么办? 您将从表示层调用什么代码? 您是否会实例化 UserEntity 类,并执行以下操作:

 User = new UserEntity(userId)
 User.lockedOut = true
 User.Save()

或者您是否会创建一个新类,例如 UserHelper (/BusinessLogic/UserHelper.cs),它可能具有 LockOutUser 函数。 这会将代码更改为:

 UH = new UserHelper()
 UH.LockOutUser(userId)

或者您会扩展 UserEntity 基类,并创建添加新功能的 UserEntityExt 吗? 因此,表示层的代码可能如下所示:

 User = new UserEntityExt(userId)
 User.LockOutUser()

或者...您会做其他事情吗?

您的目录/命名空间结构和文件/类命名约定是什么?

I'm used to the layout that LLBLGen gives when it generates objects based on a database structure, which might generate the following class files for a given "User" table in the database:

/EntityClasses/UserEntity.vb
/CollectionClasses/UserCollection.vb

This provides some base functionality for data access. However, when you want to implement business logic on top of that, how are you laying things out? For example, given a table structure that might look like this:

 USER
 userId
 firstName
 lastName
 username
 password
 lockedOut

What if you wanted to lock out a user? What code would you call from the presentation layer? Would you instantiate the UserEntity class, and do:

 User = new UserEntity(userId)
 User.lockedOut = true
 User.Save()

Or would you create a new class, such as UserHelper (/BusinessLogic/UserHelper.cs), which might have a LockOutUser function. That would change the code to be:

 UH = new UserHelper()
 UH.LockOutUser(userId)

Or would you extend the base UserEntity class, and create UserEntityExt that adds the new functionality? Therefore, the code from the presentation layer might look like:

 User = new UserEntityExt(userId)
 User.LockOutUser()

Or... would you do something else altogether?

And what would your directory/namespace structure and file/class naming conventions be?

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

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

发布评论

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

评论(1

晨与橙与城 2024-07-12 16:51:05

我认为您正在寻找的是一个位于域对象之上的服务层。 尽管我可能将其称为 UserService 或 UserTasks,但您基本上可以通过第二个选项来实现这一点。 通过将此 LockUser 进程封装在一个位置,以后当可能涉及更多步骤或其他域对象时,将很容易进行更改。 此外,这也是处理多个数据库调用时实现事务的地方。

I think what you are looking for is a service layer which would sit on top of the domain objects. You essentially have this with your second option although I might call it UserService or UserTasks. By encapsulating this LockUser process in a single place it will be easy to change later when there might be more steps or other domain objects involved. Also, this would be the place to implement transactions when dealing with multiple database calls.

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