【问题标题】:How to hook to action of another Orchard Module如何挂钩另一个果园模块的操作
【发布时间】:2015-05-21 18:28:20
【问题描述】:

鉴于:模块 A 和模块 B 在 Orchard CMS 中启动并运行

目标:当模块A的动作被调用时,执行模块B的一些代码

详情:模块 A 更新 UserPartRecord 表,模块 B 应在创建新记录时更新其他表

我看到我必须在模块 A 的操作中添加一些代码(调用模块 B 的代码),但是如果没有对依赖项进行硬编码(在模块 A 中使用模块 B 的类),我该如何做到这一点?

【问题讨论】:

  • 您是否要发布到模块 B 中的控制器?或者您是否只是想从 A 调用 B 中的代码?如果是后者,如果不依赖,这怎么可能?为什么要调用操作而不是服务类?
  • @BertrandLeRoy 是的,我只是想在发生用户更改时执行一些代码。我通过挖掘 Orchard 源代码找到了解决方案,请参阅我的答案。

标签: model-view-controller module dependencies orchardcms


【解决方案1】:

在咨询了我的枕头后,我确实记得 Orchard 用户也是 Orchard 内容项,因此使用一些内容管理界面应该是要走的路。

这是我的解决方案:

模块 A(更新 Orchard 用户并应通知其他模块)

public class UserController
{
  // public
    public UserController( ... ,
      Orchard.Security.IMembershipService aMembershipService,
      Orchard.ContentManagement.IContentManager aContentManager,
      System.Collections.Generic.IEnumerable<Orchard.ContentManagement.Handlers.IContentHandler> aContentHandlers)
    {
      Logger = Orchard.Logging.NullLogger.Instance;

      mMembershipService = aMembershipService;
      mContentManager = aContentManager;
      mContentHandlers = aContentHandlers;
    }

    ...

    // this method updates Orchard users by utilizing the Orchard content manager / handler 
    // functionality to notify other modules about creation, update or removal of a user
    public System.Web.Mvc.ActionResult Update()
    {
      ...

      foreach (... lUser in lUserQuery)
      {
        // ... check whether a matching orchard user exists
        Orchard.Security.IUser lOrchardUser = mMembershipService.GetUser(lUser.IntranetLogin);
        if (lOrchardUser != null)
        {
          // update user
          ...

          // notify handlers about update (adopted from src\Orchard\ContentManagement\DefaultContentManager.cs)
          Orchard.ContentManagement.Handlers.UpdateContentContext lUpdateContext =
            new Orchard.ContentManagement.Handlers.UpdateContentContext(lOrchardUser.ContentItem);

          mContentHandlers.Invoke(aHandler => aHandler.Updated(lUpdateContext), Logger);            
        }
        else
        {
          // create user

          lOrchardUser = mMembershipService.CreateUser(...);

          // note: 
          //  no notify handling needed as mMembershipService.CreateUser() already notifies content handlers
        }
      }

      ...

      // remove deprecated users
      foreach (int lDeprecatedOrchardUserID in lDeprecatedOrchardUserIDs)
      {   
        // removeuser
        Orchard.Users.Models.UserPart lUserPart =
          mContentManager.Get<Orchard.Users.Models.UserPart>(lDeprecatedOrchardUserID);

        if (lUserPart != null)
        {
          // the following call removes the user (content item) and notifies content handlers about removal
          mContentManager.Remove(lUserPart.ContentItem);
        }
      }

      ...
    }

    public Orchard.Logging.ILogger Logger { get; set; }

  // private
    ...

    private Orchard.Security.IMembershipService mMembershipService;
    private Orchard.ContentManagement.IContentManager mContentManager;
    private System.Collections.Generic.IEnumerable<
      Orchard.ContentManagement.Handlers.IContentHandler> mContentHandlers;
}

模块 B(在 Orchard 用户发生“更改”时收到通知)

public class TestHandler : Orchard.ContentManagement.Handlers.ContentHandler
{
  // public
    public TestHandler()
    {
      OnCreated<Orchard.Users.Models.UserPart>(UserCreated);
      OnUpdated<Orchard.Users.Models.UserPart>(UserUpdated);
      OnRemoving<Orchard.Users.Models.UserPart>(UserRemoving);
    }

  // private
    private void UserCreated(Orchard.ContentManagement.Handlers.CreateContentContext aContext,
      Orchard.Users.Models.UserPart aUserPart)
    {
      // do Module B specific tasks when a user is created
    }

    private void UserUpdated(Orchard.ContentManagement.Handlers.UpdateContentContext aContext,
      Orchard.Users.Models.UserPart aUserPart)
    {
      // do Module B specific tasks when a user is updated
    }

    private void UserRemoving(Orchard.ContentManagement.Handlers.RemoveContentContext aContext,
      Orchard.Users.Models.UserPart aUserPart)
    {
      // do Module B specific tasks when a user is removed
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    相关资源
    最近更新 更多