让我的存储库中的 2 个方法在同一个控制器 ActionResult 中运行是否正确?

发布于 2024-09-28 15:04:46 字数 2465 浏览 0 评论 0原文

我正在使用 ASP.NET 4.0 MVC 和 C# 创建一个商店,并且对它相当陌生。

我开始创建显示特定类别中的产品的视图页面。

在特定类别页面上,我想要产品列表,并且还想要从数据库中获取类别名称及其相关描述。

目前,我这样做的方法是在我的存储库中使用两种方法:

  1. 使用字符串检索特定类别的产品列表
  2. 使用字符串检索特定类别

然后我在一个 ActionResult 中使用这两种方法,然后将它们传递到视图。

有没有一种方法可以通过对数据库的 1 个方法调用来检索产品列表和类别名称、描述等,或者我做得是否正确?

感谢您提前提供的任何帮助。

我的代码如下:

StoreRepository

public class StoreRepository : topsports.Models.IStoreRepository
{
    private topsportsEntities db = new topsportsEntities();

    public IQueryable<Product> FindProductsByCategory(string c)
    {
        var products = from p in db.Products
                       where p.Category.Name == c
                       select p;

        return products;
    }

    public Category FindCategory(string c)
    {
        return db.Categories.SingleOrDefault(cg => cg.Name == c);

    }
}

IStoreRepository

public interface IStoreRepository
{
    IQueryable<Product> FindProductsByCategory(string c);
    Category FindCategory(string c);

}

StoreController

 public class StoreController : Controller
{
    IStoreRepository storeRepository;

    public StoreController()
        : this(new StoreRepository())
    {
    }
    public StoreController(IStoreRepository repository)
    {
        storeRepository = repository;
    }

    public ActionResult Index(string c)
    {
        var category = storeRepository.FindCategory(c);

        var products = storeRepository.FindProductsByCategory(c).ToList();

        var viewModel = new StoreViewModel
        {
            Products = products,
            Category = category

        };

        return View(viewModel);
    }
}

StoreViewModel

public class StoreViewModel
{
    public List<Product> Products { get; set; }
    public Category Category { get; set; }

}

Category.aspx

<h2><%: Model.Category.Name %></h2>

<p><%: Model.Category.Description %></p>


<ul>
    <% foreach (var item in Model.Products) { %>
        <li>
            <%: item.Name %>, 
            <strong><%: item.Description %></strong>
        </li>
    <%} %>
</ul>

I am creating a store with ASP.NET 4.0 MVC and C# and am fairly new to it.

I have come to creating the View page that displays the products within a certain category.

On the specific category page I want to have the product list and also want the category name with its relevant description taken from the database.

Currently the way I have done this is to have two methods in my Repository:

  1. Retrieve the list of products for a specific category with a string
  2. Retrieve the specific category with a string

I then use both these in one ActionResult and then pass them to the view.

Is there a way that I can retrieve both the product list and category name, description etc from 1 method call to the database or have i done it correctly?

Thank you for any help in advance.

My code is as follows:

StoreRepository

public class StoreRepository : topsports.Models.IStoreRepository
{
    private topsportsEntities db = new topsportsEntities();

    public IQueryable<Product> FindProductsByCategory(string c)
    {
        var products = from p in db.Products
                       where p.Category.Name == c
                       select p;

        return products;
    }

    public Category FindCategory(string c)
    {
        return db.Categories.SingleOrDefault(cg => cg.Name == c);

    }
}

IStoreRepository

public interface IStoreRepository
{
    IQueryable<Product> FindProductsByCategory(string c);
    Category FindCategory(string c);

}

StoreController

 public class StoreController : Controller
{
    IStoreRepository storeRepository;

    public StoreController()
        : this(new StoreRepository())
    {
    }
    public StoreController(IStoreRepository repository)
    {
        storeRepository = repository;
    }

    public ActionResult Index(string c)
    {
        var category = storeRepository.FindCategory(c);

        var products = storeRepository.FindProductsByCategory(c).ToList();

        var viewModel = new StoreViewModel
        {
            Products = products,
            Category = category

        };

        return View(viewModel);
    }
}

StoreViewModel

public class StoreViewModel
{
    public List<Product> Products { get; set; }
    public Category Category { get; set; }

}

Category.aspx

<h2><%: Model.Category.Name %></h2>

<p><%: Model.Category.Description %></p>


<ul>
    <% foreach (var item in Model.Products) { %>
        <li>
            <%: item.Name %>, 
            <strong><%: item.Description %></strong>
        </li>
    <%} %>
</ul>

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

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

发布评论

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

评论(2

独孤求败 2024-10-05 15:04:46

存储库的目的是将数据访问层与业务逻辑分离。当您选择通过类别实体检索产品时,您依赖于延迟加载,这是实体框架的实现细节。例如,当您稍后决定切换到不同的数据访问层(例如手动创建的查询)时,您可能不再拥有此功能。

第二个问题是,当您将大量功能放入单个存储库方法中时,该方法的职责就变得不清楚。正如 @Andrew Barber 所描述的,是的,你会得到很多小方法。然后可以将它们组合起来以产生有用的功能。当您选择创建返回更多结果的更大方法时,您会遇到另一个问题。当方法返回三个或四个数据集时,您会遇到这样的问题:当您只需要这些数据集中的一个或两个数据集时,您要么要创建一个新方法,其作用比原始方法少,要么您将运行四个查询,其中一两个就足够了。

存储库的小方法在组合成一个更大的整体时旨在产生有意义的结果。方法很多不一定有问题。你的代码看起来不错:)。

The purpose of repositories is to decouple your data access layer from your business logic. When you choose to retrieve products through the category entity, you're depending on lazy loading which is an implementation detail of the entity framework. When you would e.g. later on decide to switch to a different data access layer (hand created queries e.g.), it could be you would not have this facility anymore.

A second issue is that when you put a lot of functionality into a single repository method, it becomes unclear what the responsibility of this method is. As @Andrew Barber describes, yes, you will get a lot of small methods. These can then be combined to produce useful functionality. When you would choose to create bigger methods that return more results, you get another problem. When the method that returns e.g. three or four data sets, you get the issues that when you need only on or two of two of these data sets, you either are going to create a new method which does less as the original one, or you are going to run four queries where one or two would have been enough.

The small methods of the repository are meant to produce meaningful results when composed in a larger whole. Many methods is not necessarily a problem. Your code looks fine :).

放手` 2024-10-05 15:04:46

您似乎不需要单独获取 Category 对象,因为您可以从其中一项引用它。但是,我认为像您一样这样做可能会很好,因为例如,您的方法将涵盖类别目前没有任何项目的情况,无论出于何种原因。

It might seem like you don't need to get the Category object separately, since you could reference it from one of the items. However, I think it could be good to do it like you have because, for instance, your method will cover the case where a Category does not have any items in it at the moment, for whatever reason.

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