Generic Method of a Generic Class C# for similar classes

Asked

Viewed 359 times

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

  • 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

  • Thanks guys! It worked! Still a little lost with OO, but in time I’ll catch better this.

2 answers

0


You need to pass the generic entity in the parameter, it would look like this

public void Gravar<T>(T obj)
{
    context.Set<T>().Add(obj);
} 

To call this you need only pass the object as parameter, it will understand its type

Empresa empresa = new Empresa
{
    Nome = "Empresa"
};

Gravar(empresa);
  • I tried this way, but error occurs: Dbcontext.Set<Tentity>() is a method, which is not Valid in the Given context

  • Are you making a generic class with these methods? If yes, you can add it to the question. this error is usually "failure" to instantiate the context

  • Yes, I created a class called CRUD to call the methods in other classes (Company, Contact, User).

  • Would it be possible to add this whole class to the question?

  • Cara worked! Now I just need to understand how not to have to be instantiating the CRUD class in order to use the method for each class. Like not having to instantiate Crud for Company, for Contact, User.. So I think it involves more knowledge in OO, which in this case I think would be using abstraction or polymorphism.

  • @Joãoborba Search a little about Dependency injection, I think it will help you in this case, I have a project model on my github, if you want you can take it - link

Show 1 more comment

0

As I did: In my class CRUD (Generic) I left so:

public class Crud
        {
        public void Gravar(T obj)
        {
            context.Set<T>().Add(obj);
            context.SaveChanges();
        }

        private readonly ModelConexao context;

        public Crud(ModelConexao _context)
        {
            context = _context;
        }

And in my form class I called the method so:

protected void btnGravar_Click(object sender, EventArgs e)
        {
            using (ModelConexao db = new ModelConexao())
            {
                T emp = new T()
                {                   
                    dominio = txtEmpresa.Text.Split(' ').FirstOrDefault(),
                    banco = "empresa_" + txtEmpresa.Text.Split(' ').FirstOrDefault(),
                    nome = txtEmpresa.Text.ToString()
                };

                Crud empresa = new Crud(db);
                empresa.Gravar(emp);

Only I had to instantiate the Crud class there, and I think the right thing would be some way not to have to be instantiating for each entity, because in this same code I have to give a Record to the entity Contact.

But dear thanks for the help! Little by little I’ll learn...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.