本文介绍了Breeze JS-具有EFContextProvider的存储库,启用/禁用OData过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求使用微风EFContextProvider实现存储库模式.在此存储库中,我将公开一种使用OData过滤查询数据库的方法……就像微风在默认情况下一样.我还想公开一种方法,该方法将忽略OData筛选,并返回一个元素列表,就好像它是默认的EF上下文一样.

I'm looking to implement a repository pattern with breeze EFContextProvider. In this repository, I would expose a method to query the DB using OData filtering... just as breeze behaves by default. I would also want to expose a method that would ignore OData filtering, and return a list of elements as if it was the default EF Context.

因此,总而言之,我的想法是尝试执行以下操作:

So, to sum up, my idea would be to try to do something like this:

public class RepositoryBaseEntity<T> : IRepository<T> where T : class
{
        protected Breeze.WebApi.DataModelContainer _context;

        public RepositoryBaseEntity(Breeze.WebApi.EFContextProvider<DataModelContainer> context)
        {
            _context = context;
        }

        /// <summary>
        /// Gets all elements, ignoring OData filtering
        /// </summary>
        /// <returns>All elements, or null if none exists</returns>
        public IEnumerable<T> GetAll()
        {            
            // disable OData filtering in Breeze.WebApi.EFContextProvider
            return _context.Context.Set<T>();
        }


        /// <summary>
        /// Apply ODataFilters and get elements. Useful for Web API controllers
        /// </summary>
        /// <returns></returns>
        public IEnumerable<T> ApplyODataFiltersAndGet()
        {
            // enable OData filtering in Breeze.WebApi.EFContextProvider
            return _context.Context.Set<T>();
        }

}

我一直在查看Breeze EFContextProvider,并且似乎没有禁用OData过滤的方法.

I've been taking a look at Breeze EFContextProvider, and there doesn't seem to be a way of disabling OData filtering.

虽然我不想使用OData过滤时还是使用普通的旧Entity Framework DataModelContainer,而当我不想使用OData过滤时使用Breeze EFContextProvider包装器...但是使用这种方法,我将拥有两个EF上下文. ..那是我要避免的事情...过去在其他一些项目中,我们使用多个EF上下文时遇到了一些问题.

I've though about maybe using the plain old Entity Framework DataModelContainer when I don't want OData filtering, and using Breeze EFContextProvider wrapper when I do want OData filtering... but using this approach I would have two EF contexts... and that's something i want to avoid... in the past in some other projects we've had some problems using more than one EF contexts.

那么,你们看到这样做的任何方式吗?谢谢!

So, you guys see any way of doing this? Thanks!

推荐答案

OData过滤实际上是在执行和对结果进行JSON序列化之前由WebApi应用的. EFContextProvider提供了初始查询,但是它不应用OData过滤器,因为它对OData一无所知.

The OData filtering is actually applied by WebApi prior to executing and JSON-serializing the result. The EFContextProvider provides the initial query, but it does not apply the OData filter because it doesn't know anything about OData.

要控制WebApi中的筛选,请在WebApi控制器方法中添加一个ODataQueryOptions参数.这样可以防止WebApi应用过滤,并允许您执行以下操作:

To control the filtering in WebApi, add an ODataQueryOptions parameter to your WebApi controller methods. This prevents WebApi from applying the filtering, and allows you to do it instead:

public IEnumerable<Customer> Customers(ODataQueryOptions options)
{      
    if (youWantToApplyFilters)
    {
        return repository.ApplyODataFiltersAndGet(options);
    }
    else
    {
        return repository.GetAll();
    }
}

然后,在您的存储库中,

Then, in your repository,

/// <summary>
/// Apply ODataFilters and get elements. Useful for Web API controllers
/// </summary>
/// <returns></returns>
public IEnumerable<T> ApplyODataFiltersAndGet(ODataQueryOptions options)
{
    var set = _context.Context.Set<T>();
    return options.ApplyTo(set).Cast<T>();
}

请注意,ODataQueryOptionsSystem.Web.Http.OData.Query中,这意味着您的存储库将特定于WebApi.如果您不喜欢这样做,可以将过滤移出存储库并更靠近控制器,或者可以拆开ODataQueryOptions并将过滤参数放在自己的对象中(但随后必须将它们应用于您自己查询).

Note that ODataQueryOptions is in System.Web.Http.OData.Query, which means that your repository would be WebApi-specific. If you don't like that, you can move the filtering out of the repository and closer to the controller, or you can pull apart the ODataQueryOptions and put the filtering parameters in your own object (but then you'd have to apply them to the query yourself).

这篇关于Breeze JS-具有EFContextProvider的存储库,启用/禁用OData过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 20:06