Nombre de messages:
9
Karma:
55
Inscrit:
13/07/2012
Localisation:
India
Hi, I wanted to know how does the NopObjectContext know about the tables in the database provided in the connection string. My understanding was that in Code first, for a class that inherits from DbContext, there have to be getter and setter DbSet's for every Entity. However I do not see this in the NopObjectContext. Can someone please explain how it is done here in Nopcommerce? Thanks
Nombre de messages:
14
Karma:
110
Inscrit:
21/09/2012
Localisation:
United States
Here is my understanding: The way you talked about is the standard/default way when you use EF. There are couple of drawbacks with that approach, one is related to unit test. You can look at the following definitions: 1. IRepository under namespace Nop.Core.Data 2. EfRepository under namespace Nop.Data; 3. IDbContext under namespace Nop.Data 4. NopObjectContext under namespace Nop.Data Let's look at a example, Insert a customer (which is implemented in CustomerService under namespace Nop.Services.Customers /// </summary> /// <param name="customer">Customer</param> public virtual void InsertCustomer(Customer customer) { if (customer == null) throw new ArgumentNullException("customer"); _customerRepository.Insert(customer); //event notification _eventPublisher.EntityInserted(customer); } _customerRepository is defined as private readonly IRepository<Customer> _customerRepository; and will/could be a EfRepository<Customer> object, inside EfRepository<Customer>, there are two private property private IDbSet<Customer> _entities; private readonly IDbContext _context; _context will/could be the NopObjectContext and there is a Insert method public void Insert(T entity) { try { if (entity == null) throw new ArgumentNullException("entity"); this.Entities.Add(entity); this._context.SaveChanges(); } ......... } I guess this is what so called Repository Pattern.