我的 CRUD LINQ 代码去哪里? ASP.NET MVC

发布于 2024-10-04 00:40:33 字数 284 浏览 6 评论 0原文

我目前正在一个项目中使用 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 技术交流群。

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

发布评论

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

评论(4

假装不在乎 2024-10-11 00:40:33

要添加@Poco所说的内容,这里有一个示例:

Foo.Common.Repositories中(在Foo.Common项目内):

public interface IRepository<T>
{
    IEnumerable<T> GetAll();
    void Update(T entity);
    void Add(T entity);
    void Delete(T entity);
    void Save();
}

public interface IUserRepository : IRepository<User>
{
    void GetByCredentials(string username, string password);
}

内部Foo.Data.Repositories (在 Foo.Data 项目中):

public class UserRepository
{
    // ... other methods/properties snipped.

    public IEnumerable<User> GetAll()
    {
        // Where this.Users would be L2Sql or Code-First... or some other ORM.
        return from u in this.Users orderby u.Name select u;
    }
}

然后在您实际的 Foo.Web 中:

public class UserController : Controller
{
    private readonly IUserRepository userRepository;

    public UserController(IUserRepository userRepository)
    {
         this.userRepository = userRepository;
    }

    [AcceptVerbs(HttpVerbs.Get)]
    public ViewResult List()
    {
        var users = this.userRepository.GetAll();
        return this.View(users);
    }
}

在您的 Global.asax 中,您将使用 Ninject 或其他 IoC 容器来解析 IUserRepository :

public static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IUserRepository>().To<UserRepository>();
}

protected void Application_Start()
{
    var kernel = new StandardKernel();

    AreaRegistration.RegisterAllAreas();

    MvcApplication.RegisterGlobalFilters(GlobalFilters.Filters);
    MvcApplication.RegisterRoutes(RouteTable.Routes);
    MvcApplication.RegisterServices(kernel);

    // I'm using MVC3 here:
    DependencyResolver.SetResolver(new NinjectResolver(kernel));
}

To add what @Poco said, here's an example:

In Foo.Common.Repositories (inside the Foo.Common Project):

public interface IRepository<T>
{
    IEnumerable<T> GetAll();
    void Update(T entity);
    void Add(T entity);
    void Delete(T entity);
    void Save();
}

public interface IUserRepository : IRepository<User>
{
    void GetByCredentials(string username, string password);
}

The inside Foo.Data.Repositories (inside Foo.Data project):

public class UserRepository
{
    // ... other methods/properties snipped.

    public IEnumerable<User> GetAll()
    {
        // Where this.Users would be L2Sql or Code-First... or some other ORM.
        return from u in this.Users orderby u.Name select u;
    }
}

Then inside your actual Foo.Web:

public class UserController : Controller
{
    private readonly IUserRepository userRepository;

    public UserController(IUserRepository userRepository)
    {
         this.userRepository = userRepository;
    }

    [AcceptVerbs(HttpVerbs.Get)]
    public ViewResult List()
    {
        var users = this.userRepository.GetAll();
        return this.View(users);
    }
}

And inside your Global.asax you'd have Ninject or some other IoC container to resolve IUserRepository:

public static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IUserRepository>().To<UserRepository>();
}

protected void Application_Start()
{
    var kernel = new StandardKernel();

    AreaRegistration.RegisterAllAreas();

    MvcApplication.RegisterGlobalFilters(GlobalFilters.Filters);
    MvcApplication.RegisterRoutes(RouteTable.Routes);
    MvcApplication.RegisterServices(kernel);

    // I'm using MVC3 here:
    DependencyResolver.SetResolver(new NinjectResolver(kernel));
}
栖迟 2024-10-11 00:40:33

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.

凉墨 2024-10-11 00:40:33

以下是如何实现 存储库的示例 轻量级

除此之外,我还会实现一个附加层来处理您的应用程序业务逻辑并保持控制器的

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

魂牵梦绕锁你心扉 2024-10-11 00:40:33

在控制器方法中使用 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.

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