Repository模式和LINQ to SQL [英] Repository Pattern and Linq to sql

查看:235
本文介绍了Repository模式和LINQ to SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现使用角色表,用户表和一个有外部参照表的用户ID,角色ID用户认证和授权。

I 'm trying to implement user authentication and authorization using roles table, user table and a xref table having userid, roleid.

有关实施通用repoistory更新的作用,插入作用,添加用户,添加用户角色,更新用户,更新用户角色,验证用户,增加用户会话审计等我一定写单独的功能,用于每个或我可以使用类似功能的一个通用的方法。
有喜欢的用户加入到其他表,并获得基于条件的前5行,在3个表(加入了一键)插入使用单一形式的一些其他业务等。

For implementing generic repoistory to update role, insert role, add user, add user to role, update user, update user role, authenticate user, add user session to audit etc do i have write seperate functions for each or can i use one generic method for similar functions. There are some other operations like joining user to other table and get top 5 rows based on conditions, Inserting in 3 tables (joined on a key) using single form etc

我很困惑阅读了许多文章和样品,样品BCOS我看到走不进深,除了实行CRUD操作。

I'm confused reading many articles and samples, bcos samples i see don't go into deep implemenation except CRUD operations.

有人能指导我还是直接我一个很好的样本/条?

Can someone guide me or direct me to a good sample/article?

推荐答案

首先,遵循什么弗兰克Schwieterman说明。让你的资料库成长为他们使用的增长。此外,了解和利用的IQueryable接口。 L2S,实体框架,LINQ NHibernate的,和一些新的ORM的像亚音速和Telerik的ORM一起,都支持IQueryable的接口。

First off, follow what Frank Schwieterman stated. Let your repositories grow as their usage grows. In addition, learn about and make use of the IQueryable interfaces. L2S, along with Entity Framework, LINQ to nHibernate, and some of the newer ORM's like SubSonic and Telerik's ORM, all support the IQueryable interface.

在你需要可变查询从你的资料库,但仍希望如果需要转乘OR映射器的好处的情况下,IQueryable的是一个强大的工具。假设类似如下:

In the cases where you need mutable queries from your repository, but still want the benefit of interchanging OR mappers if needed, IQueryable is a powerful tool. Assume something like the following:

public class ProductRepository: IProductRepository
{
    public Product GetByID(int id);
    public IList<Product> GetAll();
    public void Insert(Product product);
    public Product Update(Product product);
    public void Delete(Product product);
}

这是一个pretty公共存储库,用裸露的骨头常用方法。随着时间的推移,你可能最终与一帮更多的方法:

This is a pretty common repository, with the bare bones common methods. Over time, you may end up with a bunch more methods:

public IList<Product> GetByOrder(Order order);
public IList<Product> GetByCategory(Category category);
public IList<Product> GetByQuantityInStock(int quantityInStock);

这也是pretty常见的,这取决于你怎么样来解决这个问题,完全可以接受。但是,从长远来看,你的资料库可以增长到一个笨拙的大小,并且它的界面总是会发生变化。你也失去使用幕后的OR映射器的真正好处。

This is also pretty common, and depending on how you like to approach the problem, perfectly acceptable. However, in the long run, your repository can grow to an unwieldy size, and its interface will always be changing. You also loose the real benefit of using an OR mapper behind the scenes.

您可以保留原来的,简单的界面库,但仍提供自己一个很大的灵活性,如果你改变了一个方法:

You can keep the original, simple repository interface, but still provide yourself with a lot of flexibility, if you change one method:

public IQueryable<Product> GetAll();

您库现在返回的查询,而不是已经检索到的对象的列表。现在,您可以自由使用该查询像任何其他支持LINQ的对象:

Your repository now returns a query, rather than a list of already-retrieved objects. You are now free to use this query like any other LINQ enabled object:

var productsWithLowStock = productRepository.GetAll().Where(p => p.Quantity < 10);

var orders = orderRepository.GetAll();
var productsWithOrders = productRepository.GetAll().Where(p => orders.OrderLines.Any(ol => ol.ProductID == p.ProductID));

一旦你开始使用的IQueryable接口与你的资料库,您获得两全其美的:在你的低层次的数据访问的mockable抽象,以及动态查询你的code内的功率。你可以把这个概念有点远,并创建一个实现IQueryable的本身就是一个基础库类,让你消除对GETALL()调用的需要,干脆直接查询库(与另一个复杂的程度虽然。)

Once you start using the IQueryable interface with your repositories, you gain the best of both worlds: A mockable abstraction around your lower-level data access, as well as the power of dynamic querying within your code. You can take the concept a little farther, and create a base Repository class that implements IQueryable itself, allowing you to eliminate the need for the GetAll() call, and simply query the repository directly (albeit with another degree of complexity.)

这篇关于Repository模式和LINQ to SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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