0
I have some classes that will use a record method.
I created a CRUD (Generic) class where would have a method to write, but I need this method to receive as parameter the classes.
There is some way to create a generic method by what I read, but I could not pass a type of parameter that meets all classes.
For example, this same method will record the company, contact, client in the bank, being the EF add function.
Below method I created, but in this case only meets the Company class.
public void Gravar<T>(Empresa obj)
{
context.Empresa.Add(obj);
}
How do I make it in place of Company, fit for Contact, Customer, User, etc.? The way it is I would have to create a record() for each class.
Seria
public void Gravar<T>(T obj) { context.Set<T>.Add(obj) }
? I don’t know if I’m right, so I didn’t put it in answer– Artur Trapp
If you want to create a generic repository (type
T
), a very cool example that will help is in this git: https://github.com/lukemcgregor/StaticVoid.Repository/blob/master/StaticVoid.Repository.EntityFramework/DbContextRepositoryDataSource.cs– Ricardo Pontual
Thanks guys! It worked! Still a little lost with OO, but in time I’ll catch better this.
– João Borba