暴露库作为一个IQueryable [英] Expose a repository as an IQueryable

查看:98
本文介绍了暴露库作为一个IQueryable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个仓库公开为一个'的IQueryable类型。

这个仓库使用LINQ to NHibernate的与数据库进行通信。

任何人都可以在一个示例实现指向我?

例如,你会相应的GetEnumerator()在我的仓库实现什么样子的?

编辑:

将为这样的事情是适当的?

 公共类MyTypeRepository:IEnumerable的< MyType的>
{
    IEnumerator的< MyType的> IEnumerable的< MyType的> .GetEnumerator()
    {
        返回Session.Linq< MyType的>()的GetEnumerator();
    }


    IEnumerator的IEnumerable.GetEnumerator()
    {
        返回((IEnumerable的< MyType的>)本).GetEnumerator();
    }

}
 

解决方案

我觉得一个存储库的可以给你的1个或多个IQueryables / IEnumerables,但不是:一个库的一个IQueryable。

这可能是这样的:

 公共接口IPersonRepository
 {
    IEnumerable的<人> GetAllPersons();
    无效AddPerson(人的人);

    // 更多...
 }
 

您可以返回 IQeryable<人> 从GetAllPerson(),但可能不会是一种进步。 IEnumerable的更简单,更小的耦合。

I'd like to expose a Repository as an 'IQueryable' type.

The repository uses Linq to NHibernate to communicate with the database.

Can anyone point me at an example implementation?

For example, what would the corresponding 'GetEnumerator()' implementation on my repository look like?

Edit:

Would something like this be appropriate?

public class MyTypeRepository : IEnumerable<MyType>
{        
    IEnumerator<MyType> IEnumerable<MyType>.GetEnumerator()
    {
        return Session.Linq<MyType>().GetEnumerator();
    }


    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((IEnumerable<MyType>)this).GetEnumerator();
    }

}

解决方案

I think a Repository can give you 1 or more IQueryables/IEnumerables, but not : a Repository is an IQueryable.

It could look like:

 public interface IPersonRepository
 {
    IEnumerable<Person> GetAllPersons();
    void AddPerson(Person person);

    // more...
 }

You could return IQeryable<Person> from GetAllPerson() but that may not be an improvement. IEnumerable is simpler, less coupling.

这篇关于暴露库作为一个IQueryable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆