简体   繁体   中英

Repository or Service to insert data in database - Entity Framework

What do I use to create a new Address using Entity Framework:

Op1:

IRepository<Address> _addressRepository;

And talk directly to the Database using the the Address Entity.

Op2:

public bool CreateAddress(AddressDto addressDto);

And talk to the service method to insert a new Address.

The question is, in a long turn maintenance of the project, which one gives more guarantees of not existing the risk of someone change something and break another piece of code that depends on it?

Based on your experience, which one is the best approach?

In my experience the second option works best. I love to have a service façade, behind which business logic can be implemented and adapted in a way that will not affect the consumers of my services. Services, by the way, can be things like domain services, web services, Web API. Basically it is a shell around business logic and data access that just exposes methods that some consumer can call.

Exposing a repository method in my view gives away too much. Why would a consuming layer know about a repository implementation? And you will be tied to the repository pattern forever. There's been a lot of discussion about EF and (generic) repositories. Personally, I hate generic repositories. I like to think in terms of aggregate roots. Having a repository for each entity type is often one layer too much, it only gets in the way. DbSet s in a DbContext are basic repositories. The context suits itself perfectly as a unit of work. I tend to turn to contexts directly in service methods, in stead of orchestrating repositories and units of work. You can use repositories, of course, but hide them behind a service façade.

One final remark: I would not return just a boolean from a service method, but an object that contains information about failure/success of the method. Eg a HttpResponse in Web API.

Try to use a Repository like this:

 public class Repository<T> : IRepository<T> where T : class
{
    protected DbSet<T> DbSet;

    public Repository(DbContext dataContext)
    {
        DbSet = dataContext.Set<T>();
    }

    #region IRepository<T> Members

    public void Insert(T entity)
    {
        DbSet.Add(entity);
    }

    public void Delete(T entity)
    {
        DbSet.Remove(entity);
    }

    public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
    {
        return DbSet.Where(predicate);
    }

    public IQueryable<T> GetAll()
    {
        return DbSet;
    }

    public T GetById(int id)
    {
        return DbSet.Find(id);
    }

    #endregion
}

And when you need a new instance of Adress, just call your Repository to do this for you:

var adressRepository = new Repository<Adress>(yourDataContext);

With the AdressRepository just do:

adressRepository.Insert(yourAdressObject)

Finally call SaveChanges of your context :

yourDataContext.SaveChanges();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM