简体   繁体   English

在存储库中使用MVC模型作为过滤器

[英]Using an MVC Model as a filter in the repository

I have a details view that is typed to IEnumerable. 我有一个输入为IEnumerable的详细信息视图。 The view with a bunch of drop downs that let you add filters to the list of records rendered. 该视图带有许多下拉菜单,可让您将过滤器添加到呈现的记录列表中。

All these dropdowns correspond to properties on the MVC model: 所有这些下拉列表与MVC模型上的属性相对应:

public class Record
{
    public string CustomerNumber { get; set; }
    public string CustomerName { get; set; }
    public string LineOfBusiness{ get; set; }
    public DateTime? Date { get; set; }
}

Now, I'm using my model as my dto to shuffle data between my controller and my repo. 现在,我将模型用作dto,以便在控制器和存储库之间重新整理数据。 Since all my drop down filters represent the model properties, I pass my model to a repo retrieval method, check its properties and filter based on its values? 由于我所有的下拉过滤器都代表模型属性,因此我将模型传递给repo检索方法,检查其属性并根据其值进行过滤? In other words: 换一种说法:

 public IEnumerable<TradeSpendRecord> Get(TradeSpendRecord record)
    {
        IQueryable<tblTradeSpend> query = _context.tblRecords;


        if (!String.IsNullOrEmpty(record.CustomerName))
            query = query.Where(x => x.CustomerNumber == record.CustomerNumber);

        if (!String.IsNullOrEmpty(record.LineOfBusiness))
            query = query.Where(r => r.LOB == record.LineOfBusiness);

SNIP SNIP

Hope this isn't too subjective, but I'm wondering if anyone has any input about whether this is a good/bad practice. 希望这不是很主观,但是我想知道是否有人对这是好事还是坏事有任何意见。 I haven't seen a whole lot of examples of dynamic filtering like I need to do, and am looking for some guidance. 我没有看到很多我需要做的动态过滤示例,并且正在寻找一些指导。

Thanks, 谢谢,

Chris 克里斯

If you're doing what I think you're doing, I'm not sure this is the best way of doing it. 如果您正在做我认为正在做的事情,那么我不确定这是否是最好的方法。

Keep your 'Models' in your MVC/presentation layer (whether this is one physical assembly or not) dedicated to your presentation layer. 将“模型”保留在专用于您的表示层的MVC /表示层中(无论这是不是一个物理装配)。 The only things that should be touching them are your Views and your Controllers. 唯一应该影响它们的是视图和控制器。 You don't want what should be independent entities to be so tightly coupled to your View Models. 您不希望将独立的实体与视图模型紧密地联系在一起。

I'd suggest creating a separate TradeSpendFilter class, which, at its simplest, exposes the filterable properties of your domain entity (likely more than any given View Model). 我建议创建一个单独的TradeSpendFilter类,该类最简单地公开您域实体的可过滤属性(可能比给定的任何View模型更多)。 You'd then pass this into your "filtering service" or whatever it may be. 然后,您可以将其传递给您的“过滤服务”或任何其他内容。 This also means you can extend your filtering functionality independent of both your domain models and your MVC app. 这也意味着您可以独立于域模型和MVC应用程序来扩展过滤功能。 For example, if you suddenly want to filter multiple objects, you can simply change... 例如,如果您突然想要过滤多个对象,则只需更改...

public class TradeSpendFilter
{
    public string CustomerName { get; set; }
    ...
}

...to... ...至...

public class TradeSpendFilter
{
    public IEnumerable<string> CustomerNames { get; set; }
    ...
}

... without causing all sorts of problems for your MVC app. ...而不会导致您的MVC应用出现各种问题。

Additionally, it will also mean you can make use of your filtering functionality elsewhere, without tying further components to your MVC app and ending up in a bootstrapped mess. 此外,这还意味着您可以在其他地方使用过滤功能,而无需将其他组件与MVC应用程序绑定在一起并最终陷入混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM