如何在nopCommerce中覆盖现有的Customer Created事件

时间:2015-04-07 12:42:44

标签: events plugins nopcommerce

我使用的是nopCommerce 3.5版。我试图创建一个默认的供应商帐户(附加创建的客户),只要有任何客户注册他/她自己。我已经创建了一个我从Admin部分安装的插件。我已经登录了安装/卸载&注册方法执行。但是我写的主要方法根本没有被解雇。我在清除缓存后尝试过但没有运气。

以下是我正在使用的代码:

文件夹结构: 1)\ Plugins \ Nop.Plugin.Customer.Module \ Controllers(空)

2)\ Plugins \ Nop.Plugin.Customer.Module \ Data(Empty)

3)\ Plugins \ Nop.Plugin.Customer.Module \ Domain(Empty)

4)\ Plugins \ Nop.Plugin.Customer.Module \ Infrastructure

  • 文件:1)DependencyRegistrar.cs

5)\ Plugins \ Nop.Plugin.Customer.Module \ Services

  • 文件:1)CustomerService.cs

6)\ Plugins \ Nop.Plugin.Customer.Module

  • 文件:1)CustomerModulePlugin.cs

  • 文件:2)Description.txt

文件中的代码:

4)\ Plugins \ Nop.Plugin.Customer.Module \ Infrastructure

  • 文件:1)DependencyRegistrar.cs

    namespace Nop.Plugin.Tax.CountryStateZip.Infrastructure
    {
        public class DependencyRegistrar : IDependencyRegistrar
        {
            public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
            {
    
                 builder.RegisterType<Nop.Plugin.Customer.Module.Services.CustomerService>().As<Nop.Services.Customers.ICustomerService>().InstancePerRequest();
    
                ////we cache presentation models between requests
                //builder.RegisterType<CountryStateZipTaxProvider>()
                //    .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"));
            }
    
            public int Order
            {
                get { return 1; }
            }
        }
    }
    

5)\ Plugins \ Nop.Plugin.Customer.Module \ Services     - 文件:1)CustomerService.cs

namespace Nop.Plugin.Customer.Module.Services
{
    public class CustomerService : Nop.Services.Customers.CustomerService //Nop.Services.Customers.ICustomerService
    {

        #region Fields

        private readonly IRepository<Nop.Core.Domain.Customers.Customer> _customerRepository;
        private readonly IRepository<CustomerRole> _customerRoleRepository;
        private readonly IRepository<GenericAttribute> _gaRepository;
        private readonly IRepository<Order> _orderRepository;
        private readonly IRepository<ForumPost> _forumPostRepository;
        private readonly IRepository<ForumTopic> _forumTopicRepository;
        private readonly IRepository<BlogComment> _blogCommentRepository;
        private readonly IRepository<NewsComment> _newsCommentRepository;
        private readonly IRepository<PollVotingRecord> _pollVotingRecordRepository;
        private readonly IRepository<ProductReview> _productReviewRepository;
        private readonly IRepository<ProductReviewHelpfulness> _productReviewHelpfulnessRepository;
        private readonly IGenericAttributeService _genericAttributeService;
        private readonly IDataProvider _dataProvider;
        private readonly IDbContext _dbContext;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        private readonly CustomerSettings _customerSettings;
        private readonly CommonSettings _commonSettings;

        private readonly Nop.Services.Vendors.IVendorService _vendorService;
        private readonly Nop.Services.Customers.ICustomerService _customerService;

        #endregion



        public CustomerService(ICacheManager cacheManager,
            IRepository<Nop.Core.Domain.Customers.Customer> customerRepository,
            IRepository<CustomerRole> customerRoleRepository,
            IRepository<GenericAttribute> gaRepository,
            IRepository<Order> orderRepository,
            IRepository<ForumPost> forumPostRepository,
            IRepository<ForumTopic> forumTopicRepository,
            IRepository<BlogComment> blogCommentRepository,
            IRepository<NewsComment> newsCommentRepository,
            IRepository<PollVotingRecord> pollVotingRecordRepository,
            IRepository<ProductReview> productReviewRepository,
            IRepository<ProductReviewHelpfulness> productReviewHelpfulnessRepository,
            IGenericAttributeService genericAttributeService,
            IDataProvider dataProvider,
            IDbContext dbContext,
            IEventPublisher eventPublisher,
            CustomerSettings customerSettings,
            CommonSettings commonSettings)
            : base(
                cacheManager,
                customerRepository,
                customerRoleRepository,
                gaRepository,
                orderRepository,
                forumPostRepository,
                forumTopicRepository,
                blogCommentRepository,
                newsCommentRepository,
                pollVotingRecordRepository,
                productReviewRepository,
                productReviewHelpfulnessRepository,
                genericAttributeService,
                dataProvider,
                dbContext,
                eventPublisher,
                customerSettings,
                commonSettings
                )
        {
            this._cacheManager = cacheManager;
            this._customerRepository = customerRepository;
            this._customerRoleRepository = customerRoleRepository;
            this._gaRepository = gaRepository;
            this._orderRepository = orderRepository;
            this._forumPostRepository = forumPostRepository;
            this._forumTopicRepository = forumTopicRepository;
            this._blogCommentRepository = blogCommentRepository;
            this._newsCommentRepository = newsCommentRepository;
            this._pollVotingRecordRepository = pollVotingRecordRepository;
            this._productReviewRepository = productReviewRepository;
            this._productReviewHelpfulnessRepository = productReviewHelpfulnessRepository;
            this._genericAttributeService = genericAttributeService;
            this._dataProvider = dataProvider;
            this._dbContext = dbContext;
            this._eventPublisher = eventPublisher;
            this._customerSettings = customerSettings;
            this._commonSettings = commonSettings;
        }


        /// <summary>
        /// Insert a customer
        /// </summary>
        /// <param name="customer">Customer</param>
        public override void InsertCustomer(Nop.Core.Domain.Customers.Customer customer)
        {

            if (customer == null)
                throw new ArgumentNullException("customer");

            _customerRepository.Insert(customer);

            //event notification
            _eventPublisher.EntityInserted(customer);

            // make registered user as a vendor too.
            {
                //Nop.Core.Domain.Vendors.Vendor _vendor = new Nop.Core.Domain.Vendors.Vendor();
                var vendor = new Nop.Core.Domain.Vendors.Vendor()
                {
                    Name = customer.Username,
                    Email = customer.Email,
                    Active = true
                };

                this._vendorService.InsertVendor(vendor);
                customer.VendorId = vendor.Id;

                var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Vendors);
                if (registeredRole == null) throw new NopException("'Vendors' role could not be loaded");
                customer.CustomerRoles.Add(registeredRole);
            }

        }

    }
}

6)\ Plugins \ Nop.Plugin.Customer.Module     - 文件:1)CustomerModulePlugin.cs

namespace Nop.Plugin.Customer.Module
{
    public class CustomerModulePlugin : BasePlugin, IConsumer<EntityInserted<Nop.Core.Domain.Customers.Customer>>
    {

        private readonly Nop.Services.Vendors.IVendorService _vendorService;
        private readonly Nop.Services.Customers.ICustomerService _customerService;

        public override void Install()
        {

            base.Install();
        }

        public void HandleEvent(EntityInserted<Nop.Core.Domain.Customers.Customer> customer)
        {
            if (customer == null)
                throw new ArgumentNullException("customer");

            // make registered user as a vendor too.
            {
                //Nop.Core.Domain.Vendors.Vendor _vendor = new Nop.Core.Domain.Vendors.Vendor();
                var vendor = new Nop.Core.Domain.Vendors.Vendor()
                {
                    Name = customer.Entity.Username,
                    Email = customer.Entity.Email,
                    Active = true
                };


                this._vendorService.InsertVendor(vendor);
                customer.Entity.VendorId = vendor.Id;

                var registeredRole = _customerService.GetCustomerRoleBySystemName(SystemCustomerRoleNames.Vendors);
                if (registeredRole == null) throw new Exception("'Vendors' role could not be loaded");
                customer.Entity.CustomerRoles.Add(registeredRole);
            }
        }

        public override void Uninstall()
        {
            base.Uninstall();
        }
    }
}

这将创建以下文件夹:

1)\ Presentation \ Nop.Web \ Plugins \ Customer.Module

  • 文件:1)Nop.Plugin.Customer.Module.dll

  • 文件:2)Nop.Plugin.Customer.Module.pdb

  • 文件:3)Description.txt

然后我在nopCommerce的已发布代码中复制了生成的文件。

请告诉我这里我做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

发现问题,

实际上在DependencyRegistrar.cs文件中我错误输入了以下内容:

namespace Nop.Plugin.Tax.CountryStateZip.Infrastructure

而不是:

namespace Nop.Plugin.Customer.Module.Infrastructure

纠正修复问题。