[英]How to Override existing Customer Created event in nopCommerce
我正在使用nopCommerce版本3.5。 每當任何客戶注冊他/她自己時,我都試圖創建一個默認的供應商帳戶(與創建的客戶一起)。 我創建了一個從“管理”部分安裝的插件。 我已經在“日志”中簽入了“執行安裝/卸載和注冊”方法。 但是我寫的主要方法根本沒有被解雇。 我嘗試清除緩存后,但沒有運氣。
以下是我正在使用的代碼:
文件夾結構:1)\\ Plugins \\ Nop.Plugin.Customer.Module \\ Controllers(空)
2)\\ Plugins \\ Nop.Plugin.Customer.Module \\ Data(空)
3)\\ Plugins \\ Nop.Plugin.Customer.Module \\ Domain(空)
4)\\ Plugins \\ Nop.Plugin.Customer.Module \\ Infrastructure
5)\\ Plugins \\ Nop.Plugin.Customer.Module \\ Services
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的已發布代碼中。
請讓我知道我在這里做錯了嗎?
謝謝
找到了問題,
實際上在DependencyRegistrar.cs
文件中,我錯誤地鍵入了以下內容:
namespace Nop.Plugin.Tax.CountryStateZip.Infrastructure
代替:
namespace Nop.Plugin.Customer.Module.Infrastructure
更正解決了該問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.