NopObjectContext doubt

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 anni tempo fa
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
11 anni tempo fa
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.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.