ASP.NET MVC (MVC2) 使用 Linq to SQL 和存储库层插入/更新数据时的最佳实践
我在这里遇到了一些难题,希望你们中的一些大师能够帮助填补空白。
我目前面临的情况是关于我的“Users”表和“OpenID”表。我的应用程序允许用户拥有多个 OpenID,因此我在单独的表中跟踪它们。
用户
身份证号
用户名OpenID
身份证号
用户ID
声明标识符
我为每个表都有一个 CRUD 存储库,并且还有一个与存储库交互的服务(每个存储库一个服务)。
我的问题是关于插入新用户(因为更新将遵循相同的原则)。以下是我脑海中的选项。
- 让 UserService 插入一个新用户,检索用户的 ID,然后使用 UserID 插入一个新的 OpenID
- 让 UserService 将新用户连同 ClaimedIdentifier 一起发送到 UserRepository,并让存储库同时插入 User 和 OpenID(这不会不太适合 CRUD 方法)
- 创建 User 表和 OpenID 表的视图,创建 UsersOpenIDRepository 和 UsersOpenIDService,然后插入到视图中。
任何超出我能想到的想法或建议将不胜感激。
请注意,我没有使用 NHibernate,因此我可以按照我认为合适的方式对我的域进行建模。我在这个项目中坚持使用 Linq to SQL,
I'm in a bit of a conundrum here and I'm hoping for some of you Guru's to help fill in the blanks.
The situation I'm currently facing is with regards to my "Users" table and my "OpenID" table. My app allows for a user to have multiple OpenID's, so I keep track of them in a separate table.
Users
ID
UsernameOpenID
ID
UserID
Claimedidentifier
I have a CRUD repository for each table, and I also have a Service that interfaces with the Repository (one service per repo).
The question I have is with regards to inserting a new user (since updating will follow the same principal). Here are the options that I have in my head.
- Have the UserService insert a new user, retrieve the user's ID and then insert a new OpenID using the UserID
- Have the UserService send the new user along with the ClaimedIdentifier to the UserRepository, and have the repository insert both the User and the OpenID (this doesn't fit the CRUD methodology very well)
- Create a View of both the User table an the OpenID table, create a UsersOpenIDRepository and a UsersOpenIDService, and then insert to the View.
Any other thoughts or suggestions beyond what I can think of will be greatly appreciated.
Please note, I am not using NHibernate whereby I can model my domain however I see fit. I am sticking to Linq to SQL on this project,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我的经验,Linq2SQL 无论如何都不太适合 CRUD 方法。让它适合意味着要经历太多的困难,这是不值得的。您所描述的问题甚至不是唯一的问题 - 更新实体时情况会变得更糟。
因此,我在当前项目中选择了解决方案 2(将两个实体都插入到 usersrepostory 中)。
我也放弃了存储库的更新方法。相反,我的存储库只有一个 SubmitChanges 方法,必须在对加载的实体执行所有更新后调用该方法。在同一 Web 请求中创建的所有存储库共享相同的 DataContext,因此我在哪个存储库上调用 Submitchanges 并不重要。它不是 CRUD,但它更适合 LINQ2SQL 执行数据库更新的方式。
如果您确实想要纯粹的 CRUD,您可能需要使用 POCO 实体生成模板来检查 EF。
In my experience Linq2SQL does not fit the CRUD methodology very well anyway. Making it fit means jumping through too many hoops to be really worth it. The problem you are describing isn't even the one only one - it gets even worse when updating entities.
Therefore I opted for solution 2 (inserting both entities in the usersrepostory) in my current project.
I also gave up on update methods for the repositories. Unstead my repositories simply have a SubmitChanges method which must be called after all updates are performed on loaded entities. All repositories created in the same web request share the same DataContext, so it doesn't really matter which repository I call Submitchanges on. It's not CRUD, but it lends itself much better to the LINQ2SQL way of performing database updates.
If you really absolutely want pure CRUD, you might want to check out EF with the POCO entity generation template.