EF 和存储库模式
我正在用 MVC 编写一个项目并使用 EF 4.0。我正在使用存储库模式,但不确定在哪里放置一些属性。
public interface IUserRepository<User>
{
User GetUserById(int userId);
void UpdateUser(User user);
void AddUser(User user);
List<User> GetUsersByName(string userName);
void Create(User user);
int NumberOfFollowers { get; set; }
}
我的两个问题是1)。属性 NumberOfFollowers
应该是属性还是方法? 和2)。应该放在User实体类中而不是接口中吗?
干杯。
I am writing a project in MVC and am using EF 4.0. I am using the repository pattern but am unsure about where to place some properties.
public interface IUserRepository<User>
{
User GetUserById(int userId);
void UpdateUser(User user);
void AddUser(User user);
List<User> GetUsersByName(string userName);
void Create(User user);
int NumberOfFollowers { get; set; }
}
My two problems are 1). should the property NumberOfFollowers
be a property or a method?
and 2). should it be placed inside the User entity class instead of the interface?
cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
NumberOfFollowers
将是用户本身的属性,而不是在存储库界面中。NumberOfFollowers
would be a property of the User itself and not in the repository interface.我同意丹尼尔的回答 - 拥有一个名为
NumberOfFollowers
的属性是最合乎逻辑的。作为指导,如果您可以通过 User 对象本身访问数据,则直接在 User 类上创建属性\方法。通常,如果您有其他表的外键,则可以通过 User 类访问这些数据项,并且应该封装在属性\方法内。另一方面,如果您想要查找与
User
相关的信息,但需要另一个对象的帮助,则创建一个UserService
类。保持存储库简单 - 仅具有数据检索\操作方法,并在单独的服务类中创建更多涉及/业务逻辑的方法,例如上面是粗略的指南,绝不是事实上的标准,但它对我来说效果很好。
I will agree wilth Daniel's answer - having a property named
NumberOfFollowers
would be most logical. As a guide, if there is data you can access via theUser
object itself, then have properties\methods created directly on theUser
class. Oftern if you have foregin keys to other tables, then accessing those data items can be done via theUser
class and should be encapsulated inside properties\methods.On the other hand, if you wanted to find information relating to a
User
, but would require another object's help, then create aUserService
class. Keep the repository simple - have data retrieval\manipulation methods only, and created more involved/business logic hungry methods in a seperate service class e.g.The above is rough guide and by no means the defacto standard, but it works well for me.
我写了一篇博客关于它涵盖了如何抽象 ORM 提供程序的特定功能、每个请求对象上下文管理、管理来自多个数据库的模型、使用工作单元模式的事务管理以及完整的源代码和示例。
请看一下,如果您有任何疑问,请告诉我。
I wrote a blog about it and covered how to abstract ORM provider specific features, per request object context management, managing models from multiple databases, Transaction Management with Unit of Work Pattern with complete source code and examples.
Please take a look and let me know if you have any questions.
如果
NumberOfFollowers
是 User 的属性,那么它绝对应该位于User
类上,而不是存储库中。存储库仅负责获取/放入数据。这是我最喜欢的 EF 存储库实现:
http://www.codeproject.com/KB/database/ImplRepositoryPatternEF.aspx
这个非常棒,非常完整,并且具有很多功能。
If
NumberOfFollowers
is a property of a User, it should definitely be on theUser
class, not the repository. The repository is responsible for getting/putting data only.Here is my favorite repository implementation for EF:
http://www.codeproject.com/KB/database/ImplRepositoryPatternEF.aspx
This one is terrific, very complete and comes with a lot of features.
回复:属性或方法,.NET Design Guideless 规定,如果它可以 a) 抛出和异常或 b) 需要相当长的时间才能返回,则它不应该是属性。
re: property or method, .NET Design Guideless dictate that it shouldn't be a property if it could a) throw and exception or b) take a noticeable amount of time to return.