如何以通用方式使用存储库访问数据 [英] How do I access data using repository in generic way

查看:92
本文介绍了如何以通用方式使用存储库访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

Hello ,

var employeeList = from b in Context.Employee select b





使用上面的代码我可以访问 Employee 表中的所有记录。



但我想让这个表变得通用。所以例如我想有一个代码,它将动态地给我 Context.Manager Context.Company 等记录...



如何使其通用?我也不想使用强制转换。

我可以使用泛型方法并传递类型 T ,但是当我再次使用时我必须指定我必须使用的类型。有没有其他方法可以实现?



using above code I can access all the records in Employee table.

But I want to make this table generic. so for example I want to have a code which will dynamically give me records of Context.Manager, Context.Company etc...

How can I make it generic ? I don't want to use cast also.
I can have a generic method and pass type T , but then while using again I have to specify what type I have to use . Is there any other way through which it can be achieved ?

推荐答案

我希望我有这个正确的:



你建议这样做的方式并不是一个好主意,但有很多方法可以达到同样的最终效果。



看一看在此:

I hope I have this correct:

They way you propose to do this is not a good idea, but there are ways to achieve the same end result that are a good idea.

Take a look at this:
//Generic Factory (Reflection)
//Specify the worker and the return type
List<address> addresses = Factory<workers.address>.SelectAll<address>();
Customer customer = Factory<workers.customer>.SelectById<customer>(id);

//POCO
//Have the class do all the work
List<address> addresses = Address.SelectAll();
Customer customer = Customer.SelectById(id);
</address></customer></workers.customer></address></workers.address></address>





我同时使用这两种方法。我为工厂设置了我的工人和模型,但我在模型中使用了这个工厂。如果你看 Address.SelectAll(),你会看到正在实施的通用工厂。



这很棒为了便于在运行时使用,但它们确实需要一些设置(好的,很多设置)。



我有3层:



1:数据访问级别(DAL):

这是我的实体框架所在的位置。我创建了一个dbml(从服务器工具栏拖放表,然后给表和名称的正确别名用作POCO),但我也为每个表创建了部分类。它们的外观很相似。它们都有内部静态查询和类似的选择方法。



2:业务逻辑级别(BLL):

这是我的地方工厂坐。此dll设置为数据级别dll的Friend,因此可以查看内部方法。它通过反射访问这些。这意味着数据结构的内容对其他任何人来说都是一个很好的秘密。它不能从数据级别返回类类型(不一致的访问等),因此有一些POCO类代表原始的EF对象。每个对象都能够与它所代表的EF对象进行转换。



我也有内部静态的工作者,但这只是做一些更复杂的事情。我没有需要这样做,所以你不必担心它。



3:Web界面。

在此之前,我实际上有一个额外的层,我创建POCO称为预测。这些代表BLL中的模型(并再次转换为它们),但它们也可以访问工厂并执行该工作。在这个级别,我可以更自由地使用数据。我可以在投影中合并几个表格数据,例如。这是最简单的对象,因为它可以完成所有工作。你需要设置的所有工作。





这种方法意味着你有一个安全,可靠,适应性强且易于使用的方法数据接口。





有一条不安全的快捷方式:



在您的EF中,按照我的描述创建DAL,但将方法公之于众。这样你就可以在没有任何安全性的情况下产生效果。它仍然安全且适应性强,但由于所有工作都在这些部分课程中完成,它们可能会变得非常臃肿。





通过传递字符串来尝试访问表的想法完全绕过了EF和POCO的想法。您也可以使用SQLClient。



我希望有所帮助。如果不是直接,那么也许它可以帮助你澄清你真正要求的是什么?让我知道^ _ ^



Andy



I use both of these. I set up my workers and models for the factory, but I use this factory within the models. If you look at Address.SelectAll() you would see the generic factory being implemented.

This is great for ease of use at run-time, but they do require some setup (ok, a lot of setup).

I have 3 tiers:

1: Data Access Level (DAL):
This is where my Entity Framework lives. I create a dbml (drag and drop tables from server toolbar, then give the tables and name decent aliases for use as a POCO), but I also create partial classes for each of the tables. Their appearance is similar. They all have internal static query and select methods that are similar.

2: Business Logic level (BLL):
This is where my factory sits. This dll is set as "Friend" of the data level dll so it can see the internal methods. It accesses these via reflection. That means that the contents of the Data structure is kept a good secret for anyone else. It cannot return the class types from the Data Level (inconsistent access et al) so there are some POCO classes that represent the original EF objects. Each object is able to convert to and from the EF object it represents.

I also have workers that are internal static, but that's only to do some more complex stuff. I didn't need to do it that way, so you don't have to worry about it.

3: The Web Interface.
I actually have an extra layer before this where I create POCOs call "Projections". These represent the models from the BLL (and once again convert to and from them), but they also access the factory and perform that work too. At this level I can play around with the data a bit more freely. I can have amalgamations of several tables data in on projection for eg. This is the simplest object to use as it does all the work itself. All the work that you have to set up.


This methods means that you have a safe, secure, adaptable and easy to use interface to you data.


There is a shortcut which is not secure:

In your EF, create the DAL as I described but make the methods public. That way you can still have the effect with none of the security. It is still safe and adaptable, but as all of the work is being done in these partial classes, they could get pretty bloated.


The idea of trying to access the table by passing strings completely bypasses the idea of EF and POCO's. You may as well use SQLClient.

I hope that helps. If not directly then maybe it help you clarify what you are really asking for? Let me know ^_^

Andy


如果我理解你,你想创建通用方法/用于对特定对象(实体)执行CRUD操作的界面...



好​​吧,您可能对 CreateObjectSet [ ^ ]方法,它创建 TEntity的新实例用于查询,添加,修改和删除指定实体类型的对象。



请参阅:

Entity Framework 5.0用于具有复杂LINQ表达式的CRUD操作的通用存储库类。 Microsoft Visual Studio测试工具单元测试 [ ^ ]

使用MVC中的通用存储库模式和工作单元进行CRUD操作 [ ^ ]
If i understand you well, you want to create generic method(s)/interface to perform CRUD operation on specific object (entity)...

Well, you might be interested of CreateObjectSet[^] method, which creates a new instance of TEntity that is used to query, add, modify, and delete objects of the specified entity type.

See:
Entity Framework 5.0 Generic Repository classes for CRUD operation with complex LINQ expression. Microsoft Visual Studio Test Tools Unit Testing[^]
CRUD Operations Using the Generic Repository Pattern and Unit of Work in MVC[^]


这篇关于如何以通用方式使用存储库访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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