Copied to clipboard

Flag this post as spam?

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


  • julius 107 posts 289 karma points
    Dec 31, 2013 @ 10:31
    julius
    3

    Do we always need the Umbraco context to get nodes from cache?

    Hello again :-)

    Is there a way to get nodes from the cache without depending on the Umbraco context? I am writing a class library that hopefully will make it easier to retrieve content from the tree, read fields from nodes and return images/content picker relations/etc as strongly types entities.

    I am now forced to force client programmers of my lib to always provide an Umbraco.Context, because the UmbracoHelper needs an Umbraco.Context instance to work. If you create the UmbracoHelper without a Umbraco.Context instance as its constructor parameter it will throw the this error if you use the helper to fetch nodes:

    No Umbraco.Web.UmbracoContext reference has been set for this Umbraco.Web.UmbracoHelper instance

    What is the rationale behind this? The Umbraco cache is just an XML document so why would a context instance be required to access it?

    As a side note, I must say I am really impressed with the improvements in the Umbraco 7 interface. It is much more modern, clean and responsive. Good job.

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Dec 31, 2013 @ 13:42
    Sebastiaan Janssen
    0

    I don't have an answer of the top of my head, but it is always helpful to show some code so we can see how you're trying to get those nodes.

  • julius 107 posts 289 karma points
    Dec 31, 2013 @ 14:23
    julius
    2

    The real question boils down to "why do we need the UmbracoContext when creating an UmbracoHelper?".

    // The helper below works, but we have to be in a controller in order to have access to the UmbracoContext
    UmbracoHelper helper = new UmbracoHelper(UmbracoContext);

    // The code below compiles, but the helper can't be used for fetching nodes. It throws an exception when you try
    // to fetch nodes with it: "No Umbraco.Web.UmbracoContext reference has been set for this Umbraco.Web.UmbracoHelper
    // instance"
    UmbracoHelper helper = new UmbracoHelper();

    So the fetching of nodes via the UmbracoHelper is dependent on the UmbracoContext. Why is that? And can I access the node tree (cache!) without having to supply the UmbracoContext somehow? 

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Dec 31, 2013 @ 15:44
    Sebastiaan Janssen
    2

    UmbracoContext is documented as: Class that encapsulates Umbraco information of a specific HTTP request

    There's a LOT of things going on in Umbraco to serve up content seemingly simply coming from and XML file. There's things like routing to consider, 404's, redirects, preview mode, debug mode, etc., etc., etc.

    People trying to unit test Umbraco face similar problems that you do. We're working on improving that, but currently it is what it is. https://groups.google.com/forum/?fromgroups=#!topic/umbraco-dev/vEjdzjqmtsU

    There's some hints (but it looks like it's still work in progress) in this issue here: http://issues.umbraco.org/issue/U4-1717 Further reading: http://jlusar.es/unit-test-umbraco-rendermvccontroller and http://our.umbraco.org/forum/developers/api-questions/37255-How-can-I-unit-test-a-class-inheriting-from-SurfaceController (has an example using reflection to create UmbracoContext).

    You might have more success with the NodeFactory (http://our.umbraco.org/wiki/reference/api-cheatsheet/working-with-nodefactory to get nodes by id) but I don't know exactly what dependencies need to be satisfied for that one. It's probably not going to help much for you.

    If you just want access to the raw data, it would be easiest to just open an XML reader on the umbraco.config file of course. ;-) The other hack I can think of is to just run the site in a seperate IIS and Web API to get content out of it, plenty of sites running that way (http://our.umbraco.org/documentation/Reference/WebApi/).

  • julius 107 posts 289 karma points
    Jan 01, 2014 @ 16:00
    julius
    0

    Ok, thanks for the info. I will keep my lib tied to the Umbraco context for now and I will see what is coming in the future. 

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jan 02, 2014 @ 00:19
    Bo Damgaard Mortensen
    9

    Hi Julius and Sebastiaan,

    You might already be aware of this, but I think the following line:

    UmbracoHelper helper = new UmbracoHelper(UmbracoContext);

    should read:

    UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);

    No? :-)

    All the best,

    Bo

Please Sign in or register to post replies

Write your reply to:

Draft