Skip to content

Migrate from Casbin.NET v1.x to v2.0

Sagilio edited this page Jul 25, 2023 · 17 revisions

1. Change the Namespaces

  • Replace NetCasbin.* to Casbin.*

  • Change other namespaces according to list:

    Before Now
    NetCasbin.Abstractions Removed
    NetCasbin.Persist Casbin.Persist.Adapter.File
    NetCasbin.Util Casbin.Functions
    NetCasbin.Extension Casbin

2. Change specific types to interface

  • Replace Model , Config , Enforcer to IModel, IConfig, IEnforcer

  • Replace create APIs according list:

    Before (- means new API) Now
    Model.CreateDefault DefaultModel.Create/new DefaultModel
    Model.CreateDefaultFromText DefaultModel.CreateFromText/ DefaultModel.NewModelFromText
    Model.CreateDefaultFromFile DefaultModel.CreateFromFile/ DefaultModel.NewModelFromFile
    new Config new DefaultConfig
    new Policy -
  • Now, all RBAC API , Management API, RBAC API, RoleManager API will only return IEnumerable<string> instead of List<string>.

3. Change type names

  • Replace type name according the list:

    Before Now
    DefaultFileAdapter FileAdapter
    Effect PolicyEffect
    PolicyEffectType EffectExpressionType
  • Removed type list:

    Removed Reason
    CoreEnforcer / ICoreEnforcer Merge to Enforcer / IEnforcer
    InternalEnforcer APIs as extension methods
    ManagementEnforcer / IManagementEnforcer APIs as extension methods
    Model Migrate to DefaultModel and add IModel
    Policy Unused
    AbstractFunction / IPMatchFunc / KeyMatch2Func ... Unused
    FunctionUtility Unused

4. Adapt Other Breaking Changes

  • Replace enforcer.Get*() methods to enfocer.* properties, for example:

    **Before ** (- means new API) Now
    enforcer.GetModel() enfocer.Mdoel
    enforcer.GetEffector() enfocer.Effector
    enforcer.GetAdapter() enfocer.Adapter
    enforcer.GetWatcher() enfocer.Watcher
  • Replace Obsolete APIs:

    • GetImplicitRolesForUser

      // Obsolete
      public List<string> GetImplicitRolesForUser(string name, params string[] domain);
      // Replacement
      public IEnumerable<string> GetImplicitRolesForUser(string name, string domain = null)
    • CoreEnforcer.NewModel

      // Obsolete
      CoreEnforcer.NewModel();
      CoreEnforcer.NewModelFromText(string text);
      CoreEnforcer.NewModelFromFile(string fileName);
      // Replacement
      DefaultModel.NewModel();
      DefaultModel.NewModelFromText(string text);
      DefaultModel.NewModelFromFile(string fileName);
    • RoleManager

      // Obsolete
      public List<string> GetRoles(string name, params string[] domain);
      public List<string> GetUsers(string name, params string[] domain);
      public bool HasLink(string name1, string name2, params string[] domain);
      public void AddLink(string name1, string name2, params string[] domain);
      public void DeleteLink(string name1, string name2, params string[] domain);
      // Replacement
      public IEnumerable<string> GetRoles(string name, string domain = null);
      public IEnumerable<string> GetUsers(string name, string domain = null);
      public bool HasLink(string name1, string name2, string domain = null);
      public void AddLink(string name1, string name2, string domain = null);
      public void DeleteLink(string name1, string name2, string domain = null);

Now, you can enjoy the v2.0 version and welcome to open an issue if you have any questions!