ProfileSitecore APIBlogLists Tools Help

Blog


    December 05

    Pluggable DataEngine?

    Recently, we had a question on plugging a custom DataEngine into Sitecore:
     
     
    While the DataEngine itself is not pluggable, the individual commands making up the engine is. That allows you to override any command in the engine. To illustrate what the engine actually does, here is the code for DataEngine.GetItem:
     
        public Item GetItem(ID itemId, Language language, Version version) {
          GetItemCommand command = Commands.GetItemPrototype.Clone();
     
          command.Initialize(itemId, language, version);
     
          return command.Execute();
        }
     
    All methods look like this (with different command prototypes).
     
    To override the behaviour of the GetItemCommand, all you need to do is to assign your own class to the GetItemPrototype property. In V5.3, you have to do this in code (in a hook, for instance). In V5.3.1 you will be able to do it directly from web.config.
     
    For the hook, you can use something like this:
     
      public class MyHook : IHook {
        #region IHook implementation
     
        public void Initialize() {
          Database database = Factory.GetDatabase("master");
     
          database.Engines.DataEngine.Commands.GetItemPrototype = new MyGetItemCommand();
        }
     
        #endregion
      }
     
    Set up the hook in web.config like this:
     
      <hooks>
        <hook type="MyNamespace.MyHook, MyAssembly"/>
        ...
      </hooks>
     
    The command implementation should look something like this:
     
      public class MyGetItemCommand : GetItemCommand {
     
        protected override Item DoExecute() {
          // Do your stuff here and return an item or null.
          // Optionally, you can call base.DoExecute();
        }
     
        public override GetItemCommand Clone() {
          MyGetItemCommand command = new MyGetItemCommand();
          command.Engine = Engine;
          return command;
        }
      }
     
    There is a number of other methods you can override, but DoExecute is the one that does the actual work.
     
    Note that if you override any of the event generating commands (such as SaveItem), the events will still be fired. If you wish to take over the event handling, you should also override the CanExecute and Executed methods.
     
    Also, note that no security checking is performed at this level. The DataEngine is called from the ItemProvider (via ItemManager) which handles securit. If you wish to alter the way security is applied, this might be a better place to hook in (which can be done by using the ItemManager.Provider property).
     
     

    Comments (4)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    小西wrote:
    小跑过来看看~~~~
    Apr. 24
    Runi Thomsenwrote:

    Nice one – you helped me out there.

    By the way. The medialibrary has changed since I did development on it and I get allot of questions regarding the media cache and how it works behind the scenes. Could you please blog something about that – or give me a call and explain it to me (in 5 minutes) and I will create a blog entry on my blog explaining it to the broad public.

    Ole, you got my number.

    /Runi Thomsen

    Jan. 16
    No namewrote:
    A real-life example was audit of all GetItem() requests, including those denied by security.

    Instead of a hook we used Sitecore Factory with its ability to set arbitrary properties on objects. Here is a fragment of web.config:
    <database ... >
      ...
      <Engines.DataEngine.Commands.GetItemPrototype hint="defer">
                <obj type="Sample.MyGetItemCommand">
                   <param desc="database">$(id)</param>
                </obj>
      </Engines.DataEngine.Commands.GetItemPrototype>
      ...
    </database>

    The class had to accept one parameter - database name - to be able to resolve reference to DataEngine it belonged to (DataEngine assigned Engine property only to built-in prototypes of commands).

    public class MyGetItemCommand : GetItemCommand
    {
      public MyGetItemCommand(string name)
      {
        Engine = Factory.GetDatabase(name).Engines.DataEngine;
      }
      // other overrides a just like Ole's MyGetItemCommand's ones
    }

    //Taken from a Sitecore Support request :)

    --
    Dmitry Kostenko
    Sitecore Ukraine
    Dec. 5
    Lars Nielsenwrote:
    This is really nice. I think we may want to do an article with a good example. Any ideas to a plugin that would make sense in a "real-life" example.
    Dec. 5

    Trackbacks

    The trackback URL for this entry is:
    http://sitecore.spaces.live.com/blog/cns!E2F0554546F0A020!162.trak
    Weblogs that reference this entry
    • None