Copied to clipboard

Flag this post as spam?

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


  • David F. Hill 122 posts 242 karma points
    Mar 11, 2011 @ 23:12
    David F. Hill
    0

    NodeFactory and INode interface (4.6.1)

    Hello Umbraco colleagues,

    This is a follow up question to an earlier post:

    our.umbraco.org/forum/developers/api-questions/17742-Get-Parent-Node-using-C-(461)

    Since the umbraco.presentation.nodeFactory is obsolete, I want to get behind the new umbraco.NodeFactory classes.

    When using umbraco.presentation.nodeFactory, this works:

    Node n = new Node(1234);
    Node p = n.Parent;


    but using umbraco.NodeFactory, the "n.Parent" reference produces an error.
    This is the workaround (thanks Tom Fulton):

    Node n = new Node(1234);
    Node p = new Node(n.Parent.Id);


    The error for n.Parent is going to break a fair amount of code and I'm sure others may have the same issue.

    Two questions:

    1) Should I report this as a bug?

    2) Is there any more info somewhere on how to use the new INode interface to do cool new things?

    Thanks,
    David Hill

  • Jesper Hauge 298 posts 487 karma points c-trib
    Mar 12, 2011 @ 00:04
    Jesper Hauge
    2

    Hi David,

     

    First; I don't think this is a bug. The INode/Node combination works perfectly well, and as you noticed it is placed in another namespace than the old nodeFactory, which means old code won't break since the old nodeFactory is still in the assemblies.

    Working with the new NodeFactory I would write your second example like this:

    Node n = new Node(1234);
    INode p = n.Parent;
    // or
    Node p = (Node) n.Parent;

    Depending on what you need exactly. More often than not, an INode is perfectly adequate, and I would stick to the first parent declaration.

    Checking with Reflector reveals the following methods and properties on INode

    public interface INode
    {
        // Methods
        DataTable ChildrenAsTable();
        DataTable ChildrenAsTable(string nodeTypeAliasFilter);
        IProperty GetProperty(string Alias);
    
        // Properties
        List<INode> ChildrenAsList { get; }
        DateTime CreateDate { get; }
        int CreatorID { get; }
        string CreatorName { get; }
        int Id { get; }
        int Level { get; }
        string Name { get; }
        string NiceUrl { get; }
        string NodeTypeAlias { get; }
        INode Parent { get; }
        string Path { get; }
        List<IProperty> PropertiesAsList { get; }
        int SortOrder { get; }
        int template { get; }
        DateTime UpdateDate { get; }
        string Url { get; }
        string UrlName { get; }
        Guid Version { get; }
        int WriterID { get; }
        string WriterName { get; }
    }

    So as you see there's a lot of stuff available for you. I'm especially fond of the new ChildrenAsList property, which is very useful with Linq-2-objects, meaning you can easily do stuff like:

    var node = new Node(1234);
    var textPages = node.ChildreAsList.Where(c => c.NodeTypeAlias == "textPage");

    Regards
    Jesper Hauge

  • David F. Hill 122 posts 242 karma points
    Mar 14, 2011 @ 00:31
    David F. Hill
    0

    Hi Jesper,

    Thank you very much for the good information. Makes more sense now.

    I'm sure I will be taking advanage of the additional functionality.

    David

Please Sign in or register to post replies

Write your reply to:

Draft