Kentico CMS 7.0 Developer's Guide

Registering providers in App_Code

Registering providers in App_Code

Previous topic Next topic Mail us feedback on this topic!  

Registering providers in App_Code

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

The most direct way to register custom providers or helpers is using the CMSModuleLoader partial class, which is included in the App_Code folder of your web project by default (Old_App_Code folder on web application installations).

 

1. Extend the CMSModuleLoader partial class inside the App_Code folder. You can either create a new code file for this purpose, or extend an existing definition of the class.

 

[C#]

 

using CMS.SettingsProvider;

 
public partial class CMSModuleLoader
{

 
}

 

2. If you have defined your custom providers in external assemblies, add using statements referring to these assemblies.

 

3. Create a new class inside the CMSModuleLoader that inherits from CMSLoaderAttribute.

 

[C#]

 

private class CustomProviderLoader : CMSLoaderAttribute
{

 
}

 

4. Add the attribute defined by the internal class before the definition of the CMSModuleLoader partial class.

 

[C#]

 

[CustomProviderLoader]

public partial class CMSModuleLoader
{
...

 

5. Override the Init method inside the attribute class.

 

[C#]

 

public override void Init()
{

 
}

 

6. In the Init method, assign a new instance of your custom provider class to the ProviderObject property of the original provider (UserInfoProvider in this example).

 

[C#]

 

public override void Init()

{

  UserInfoProvider.ProviderObject = new CustomUserInfoProvider();

}

 

You can assign classes from both App_Code and other assemblies.

 

Note: When customizing helpers, assign a new instance of your custom class into the HelperObject property instead.

 

Overall code example:

 

[C#]

 

using CMS.SettingsProvider;
using CMS.SiteProvider;
using CMS.GlobalHelper;
 
[CustomProviderLoader]
public partial class CMSModuleLoader
{
  /// <summary>
  /// Attribute class that ensures the loading of custom providers.
  /// </summary>
  private class CustomProviderLoader : CMSLoaderAttribute
   {
      /// <summary>
      /// Called automatically when the application starts.
      /// </summary>
      public override void Init()
       {
          // Registers the 'CustomUserInfoProvider' class as the UserInfoProvider
          UserInfoProvider.ProviderObject = new CustomUserInfoProvider();
 
          // Registers the 'CustomCacheHelper' class as the CacheHelper
          CacheHelper.HelperObject = new CustomCacheHelper();
       }
   }
}

 

The application now uses your customized provider(s) instead of the default functionality.