Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello ,
C#
var employeeList = from b in Context.Employee select b


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 ?
Posted
Updated 23-Aug-15 22:57pm
v2
Comments
F-ES Sitecore 24-Aug-15 4:22am    
You can make it generic by using ADO.net rather than Entity Framework. It seems like you are trying to solve problems caused by bad architecture though, and if I was you I'd question why you have a requirement to do this in the first place and what you are trying to achieve.
vishalharne 24-Aug-15 5:21am    
Thanks F-Es ,
appreciate your concern here, but this requirement is something like I have to read data one table on request and there are such 80 tables, so I didn't wanted to repeat that code instead I was thinking i anything generic could be a used.
F-ES Sitecore 24-Aug-15 5:30am    
If you use ADO.net you'll find it easier to run dynamic\generic sql queries.
vishalharne 24-Aug-15 5:39am    
Okay Will search if I can get something.. if not I will try ADO .net
Andy Lanng 24-Aug-15 4:41am    
I agree with F-ES. I don't really understand what you are trying to achieve.
Is it the case that you create a POCO, chuck it at the context and have the generic context try to match it to the db via the POCOs name and parameters? That is possible but terrible practice :S - also, you would have to make the POCO yourself. You couldn't rely on EF to generate it for you

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:
C#
//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>


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
 
Share this answer
 
Comments
vishalharne 24-Aug-15 6:09am    
Thanks Andy for the quick response. This looks great, but what i really want is -

List
addresses = Factory< workers.address >.SelectAll< address > ();
Customer customer = Factory<workers.customer>.SelectById<customer>(id);

In the above code, can we remove the tight coupling of Factory<workers.address> ?
can we somehow manage to implement Factory < generictype > ??
factory < something which can be resolved at run time > ??


I tried this..but it doesn't takes an type of 'Type', so becomes difficult.

Let me know if you got me. appreciate your help ^_^

Vishal
Andy Lanng 24-Aug-15 6:17am    
Ah. Yeah I tried this once and it doesn't work. You can't tell Factory what type it is trying to return that way. You could set up the Factory like an extension class so you would type it like:
Customer customer = customer.SelectById(id);
Factory Class would be like this:
public static class Factory{
public static T SelectAll(this T type, int id)
But resolving for T becomes cumbersome so you might as well have one for each specific type, but that brings us back to the Factory<T> class
vishalharne 24-Aug-15 6:29am    
yeah that could be the only option left I guess.. I will try to find some more options.. the only pain is if there are 80 tables I have to write 80 statements
thanks Andy ...
Andy Lanng 24-Aug-15 6:35am    
Ha, only 80? I already have 130. Almost all of them have Query(), Select(), QueryById(int), SelectById(int), Search(filter, out int), each with a model, worker, factory, filter on each level and a projection. There are extension methods that deal with transactions.
Now I'm throwing some Views and Stored Procedures into the mix, too ^_^

As you can tell, data is very important to me :P

Good luck ^_^
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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900