Partial Classes in NopCommerce

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 anni tempo fa
Hi All,

I want to make sure that I'm doing this the right way.  

I have created a custom plugin.  In the Nop.Core > Domain > Customers there is a class called SystemCustomerRoleNames.cs and I need to add a custom role to this class, however, I don't want to modify this class directly since it's a core NopCommerce class.  I want to modify this class in my plugin *project* so that the base system remains untouched.

I noticed that these classes are 'partial' classes but since I'm working from inside my plugin, which is a separate assembly, this is not much help in that I can't simply extend the class.

Question is, in this scenario should I just copy the entire SystemCustomerRoleNames.cs class and bring it into my plugin and modify it there?  Do I have any other options?  

I thought about putting my partial class in the Nop.Core project so i can extend the SystemCustomerRoleName class but then I won't be operating within my plugin and I'm still modifying the base class.

Any feedback is greatly appreciated.
Bobby
7 anni tempo fa
Related:

Plugin - Extending Data Model

https://www.nopcommerce.com/boards/t/29527/extending-entities-without-editing-the-nopcommerce-330-core.aspx
7 anni tempo fa
Thanks for your reply.  I looked at both links. The first link is closer to the issue that I'm trying to resolve but there is no answer so it's not much help to me.  You cannot use partial classes in two separate assemblies, unless I'm dead wrong.

This is the core class that I want to add properties to.  


namespace Nop.Core.Domain.Customers
{
    public static partial class SystemCustomerRoleNames
    {
        public static string Administrators { get { return "Administrators"; } }
        
        public static string ForumModerators { get { return "ForumModerators"; } }

        public static string Registered { get { return "Registered"; } }

        public static string Guests { get { return "Guests"; } }

        public static string Vendors { get { return "Vendors"; } }
    }
}


In my plugin, please note, the namespace is different as it's a different assembly:


namespace Plugins.MyPlugin.Models
{
    public static partial class SystemCustomerRoleNames
    {
        public static string MyRole { get { return "MyRole"; } }
    }
}


The above code does not work because SystemCustomerRoleNames in my plugin only sees "MyRole" and I cannot access ForumModerators, Registered and other roles.

I'm sorry if this is a rookie question, I just don't want to duplicate the core class in my plugin because it's not efficient.  How can I extend the SystemCustomerRoleNames class so I can access all properties within my plugin?

Thank you for any help!
7 anni tempo fa
Thanks for your reply.  I looked at both links. The first link is closer to the issue that I'm trying to resolve but there is no answer so it's not much help to me.  You cannot use partial classes in two separate assemblies, unless I'm dead wrong.

This is the core class that I want to add properties to.  


namespace Nop.Core.Domain.Customers
{
    public static partial class SystemCustomerRoleNames
    {
        public static string Administrators { get { return "Administrators"; } }
        
        public static string ForumModerators { get { return "ForumModerators"; } }

        public static string Registered { get { return "Registered"; } }

        public static string Guests { get { return "Guests"; } }

        public static string Vendors { get { return "Vendors"; } }
    }
}


In my plugin, please note, the namespace is different as it's a different assembly:


namespace Plugins.MyPlugin.Models
{
    public static partial class SystemCustomerRoleNames
    {
        public static string MyRole { get { return "MyRole"; } }
    }
}


The above code does not work because SystemCustomerRoleNames in my plugin only sees "MyRole" and I cannot access ForumModerators, Registered and other roles.

I'm sorry if this is a rookie question, I just don't want to duplicate the core class in my plugin because it's not efficient.  How can I extend the SystemCustomerRoleNames class so I can access all properties within my plugin?

Thank you for any help!
6 anni tempo fa
This is a good question. How can you access the fields from both parts using EF if the files are declared in different classes?
6 anni tempo fa
Does **anybody** from NopCommerce's development team have anything to say about this post?  The concept of a "Plugin" is mute if we can't put all of our code in one project.  

As it stands, plugin creation in NopCommerce works under one of the following scenarios:

1. Create your plugin project and for any NopCommerce class that you want to use/extend, you must copy the entire class into your plugin project and extend it accordingly.  The problem with this approach is that you will end up with a lot of duplicate classes.  To me, this is not much of a plugin approach but a fork of the existing solution.

2. Create your plugin project and for any customization you want to make, you will have to place your modifications outside of your plugin in respective assemblies.  This defeats the purpose of a plugin and upgrading becomes a nightmare.

I have clients that require development and modifications around NopCommerce but don't want to modify the core classes.

Please, anyone, let us know if there's a solution.  I would like to put *all* modifications and extensions in one project to make this a true plugin.

Thank you.
Bobby Pejman
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.