Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Murray Roke 467 posts 875 karma points c-trib
    Jun 11, 2010 @ 07:12
    Murray Roke
    0

    Making Document and Node Polymorphic

    Hi

    I have an 'export' method which takes IEnumerable<Document> however I'd like to pass IEnumerable<Node> (in the case I want the published version, and it's faster)

    I investigated DuckTyping using this library however it does not work because Document an Node name thier methods and properties differently.

    I tried creating extension methods to implement the implied interface however the library does not seem to support extension methods.

    Is there any other way to treat Document and Node in a poloymorphic manner? (for read only access)

  • Aaron Powell 1708 posts 3044 karma points c-trib
    Jun 11, 2010 @ 07:41
    Aaron Powell
    1

    The only sensible way to do what you're wanting to do is to create a proxy type which sites between both Document and Node. This is what we do with Snapshot, we've got an interface which is a proxy to either Umbraco content or Snapshot content.

    We've also got a class which is a proxy to those interfaces which can be used in your system (confusing yes I know).

    But essentially you create an interface which represents the data you want, then have it implemented for the different underlying data structures (either Node or Document).

    Then you need to have a factory which is what you go through to get the objects you require. The factory should also be an interface (or an abstract class) so you can swap it out either at run time or on application start.

    You then end up with stuff like this:

    public interface IContentFactory {
        IEnumerable<IContent> GetContent();
    }
    
    public interface IContent {
        int Id { get; set; }
        string NodeName { get; set; }
        //and so on
    }
    internal class NodeContent : IContent {
        public NodeContent(nodeFactory.Node source) {
          // set data
        }
    }
    
    internal class DocumentContent : IContent {
        public DocumentContent(Document source) {
          //set data
        }
    }
    public IEnumerable<IContent> SomeMethod(IContentFactory factory) {
      return factory.GetContent();
    }

    What you're trying to do is not exactly something for the faint hearted, you're going to have to use either a DI framework or have some other way of handling factories within your application scope.

  • Murray Roke 467 posts 875 karma points c-trib
    Jun 14, 2010 @ 01:57
    Murray Roke
    0

    Ahh sounds like I may try avoid the whole thing for the moment. :-) But If the performace becaomes an issue I may be comming back to this.

    Thanks for your input.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies