void create and T createT

0

What is the difference between using void and T create

Void does not return anything to me and T returns an entity to me, is the use of both is according to the scenario I have, should I use one or the other?

It would bring me complications to work my methods without implementing the using

public void Create(T entity)
    {
        Context.Set<T>().Add(entity);
    }

    public void Update(T entity)
    {
        Context.Set<T>().Attach(entity);
        Context.Entry(entity).State = EntityState.Modified;
        TrySaveChanges();
    }

    public void Delete(T entity)
    {
        throw new NotImplementedException();
    }
    
asked by Pedro Ávila 01.04.2016 в 22:03
source

1 answer

0

> > ¿the use of both is according to the scenario that I have, it suits me to use one or the other?

Depends on the purpose of each method, if it is

  • Find () would be quite weird to define it as void, since it is supposed to find and return the list of entities
  • Delete () may not return anything, since you only have to delete

> > > It would bring me complications to work my methods without implementing the using

It should not bring problems although the context instance of entity framework should be by request, that is, each transaction should create a new instance

If you develop a web project, the instance is defined by the request to the server. If it is a desktop development when you start a transaction, it generates the instance of the repository and must be destroyed when it ends.

    
answered by 01.04.2016 / 22:48
source