我的 CRUD LINQ 代码去哪里? ASP.NET MVC
我目前正在一个项目中使用 ASP.NET MVC 框架(几乎是我第一次),
我使用 Linq2SQL 作为我的数据模型。
我应该在哪里有这种代码:
var entries = from e in db.sometable select e;
我目前在控制器中有这种代码并通过我进入视图的数据..
这样可以吗?
如果不是,我该如何让我的 linq2sql 数据模型包含这种代码?
谢谢
丹尼尔
I am currently using the ASP.NET MVC framework on a project (pretty much my first time)
I am using Linq2SQL as my data model..
Where should i have this kind of code:
var entries = from e in db.sometable select e;
I currently have this kinda code in the controller and pass the data i get into the view..
is this ok?
if not how do i entend my linq2sql datamodel to include this kindof code?
Thanks
Daniel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要添加@Poco所说的内容,这里有一个示例:
在
Foo.Common.Repositories
中(在Foo.Common
项目内):内部
Foo.Data.Repositories
(在Foo.Data
项目中):然后在您实际的
Foo.Web
中:在您的
Global.asax
中,您将使用 Ninject 或其他 IoC 容器来解析 IUserRepository :To add what @Poco said, here's an example:
In
Foo.Common.Repositories
(inside theFoo.Common
Project):The inside
Foo.Data.Repositories
(insideFoo.Data
project):Then inside your actual
Foo.Web
:And inside your
Global.asax
you'd have Ninject or some other IoC container to resolveIUserRepository
:MVC 使用存储库模式是很常见的。
通常,您定义一个接口,例如 IProducts,然后实现该接口,调用 linq2sql 代码。您的控制器将接受此接口作为构造函数的参数,以便它依赖于此接口,而不是具体的类。使用依赖注入器(例如 Ninject)将允许您向构造函数提供具体的接口实现。这可以在您的 Web 应用程序上进行单元测试,并且还增加了灵活性。
有一本非常好的书,Pro ASP.NET MVC 2 Framework,解释了这一切。我现在正在读它,我很喜欢它。
It's common to use the Repository pattern for MVC.
Typically, you define an interface, for instance, IProducts, and then, you implement this interface, calling you linq2sql code. Your controller will accept this interface as a parameter for the constructor, so that it depends on this interface, and not on a concrete class. Using a dependency injector, such as Ninject, will allow you to supply a concrete interface implementation to the constructor. This enables Unit Testing on you web app, and also adds flexibility.
There's a really nice book, Pro ASP.NET MVC 2 Framework, that explains all that. I'm currently reading it, and I just love it.
以下是如何实现 存储库的示例 轻量级
除此之外,我还会实现一个附加层来处理您的应用程序业务逻辑并保持控制器的
Here's an example of how to implement the repository pattern
In addition to this I would implement an additional layer to handle your applications business logic and keep your controllers lightweight
在控制器方法中使用 Linq 查询很好。
如果我们谈论关注点分离,其想法是您的数据层(在本例中,为您提供 db.sometable 的存储库(?)代码)解耦您的逻辑代码(控制器方法)在本例中)来自数据存储。
您查询数据层而不是数据库,因此您可以更改底层数据存储,并且您的控制器代码仍然可以工作。
有些人会认为,最好再次将尽可能多的逻辑代码从控制器移出并移入模型代码(请参阅这里的第一个答案),但这取决于你想走多远。
It's fine to have Linq queries in controller methods.
If we're talking about separation of concerns, the idea is that your data layer (in this case, the repository(?) code that supplies you with
db.sometable
) decouples your logic code (controller methods in this case) from the datastore.You query the data layer rather than the database, so you can change the underlying datastore and your controller code will still work.
Some would argue that it's better again to move as much logic code as you can out of the controllers and into your model code (see the first answer here), but it depends how far you want to go.