如何挂钩另一个Orchard模块的动作

时间:2015-05-20 16:43:37

标签: model-view-controller module dependencies orchardcms

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

目标:在调用模块A的操作时执行模块B中的一些代码

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

我看到我必须添加一些代码(调用模块B的代码)到模块A的操作,但是如何在没有硬件编码依赖项的情况下这样做(使用模块A中的模块B的类)?

1 个答案:

答案 0 :(得分:0)

在用枕头咨询后,我确实记得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用户&#34;更改&#34;时会收到通知)

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
    }
}