Copied to clipboard

Flag this post as spam?

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


  • Olov 27 posts 101 karma points
    Jul 01, 2010 @ 11:08
    Olov
    0

    Problems with DataProvider.Load<T>()

    After working with another CMS a lot something I was missing when i started an Umbraco project a few days ago was a CurrentPage property. So I created a base class for my masterpages that exposes that property, when leaving the office yesterday running 4.1 RC it was working but when catching up with my RSS last night i noticed that 4.5 is released so I upgraded it this morning and it stopped working... :(

    Each masterpage class looks like this:

    public partial class Startpage : Foo.Web.UI.MasterPage<Foo.Startpage> { 
    ...
    }

    And the MasterPage<T> base class looks like this:

    public class MasterPage<T> : System.Web.UI.MasterPage where T : DocTypeBase, new()
        {
            public T CurrentPage
            {
                get
                {
                    int nodeId = Node.GetCurrent().Id;

                    return FooDataContext.Instance.GetPage<T>(nodeId);
                }
            }

    The FooDataContext.Instance is picked from the video by Aaron Powell here: http://vimeo.com/9790069 and looks like this:

            public static FooDataContext Instance { 
                get
                {
                    var ctx = HttpContext.Current.Items["FooDataContext"] as EoslDataContext;

                    if (ctx == null)
                    {
                        ctx = new FooDataContext();

                        HttpContext.Current.Items["FooDataContext"] = ctx;
                    }

                    return ctx;
                }
            }

     

    When I access any property on CurrentPage it throws this exception (Stack trace):

    [ArgumentNullException: Value cannot be null.
    Parameter name: attribute]
    System.Xml.Linq.XAttribute.op_Explicit(XAttribute attribute) +90598
    umbraco.Linq.Core.Node.<>c__DisplayClass1`1.<Load>b__0(XElement d) +76
    System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source, Func`2 predicate) +172
    umbraco.Linq.Core.Node.NodeDataProvider.Load(Int32 id) +250
    Foo.Web.Masterpages.Startpage.OnLoad(EventArgs e) in C:\projects\foo\Foo.Web\Masterpages\Startpage.Master.cs:17
    System.Web.UI.Control.LoadRecursive() +74
    System.Web.UI.Control.LoadRecursive() +146
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

    Am I missing something obvious?

  • Olov 27 posts 101 karma points
    Jul 01, 2010 @ 11:11
    Olov
    0

    I'm unable to edit my post, the CurrentPage property looks like this:

    public T CurrentPage
           
    {
               
    get
               
    {
                   
    int nodeId = Node.GetCurrent().Id;

                   
    return FooDataContext.Instance.DataProvider.Load(nodeId);
               
    }
           
    }

    And the relevant part of the stack trace for accessing a property on CurrentPage is this:

       System.Xml.Linq.XAttribute.op_Explicit(XAttribute attribute) +90598
    umbraco.Linq.Core.Node.<>c__DisplayClass1`1.<Load>b__0(XElement d) +76
    System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source, Func`2 predicate) +172
    umbraco.Linq.Core.Node.NodeDataProvider.Load(Int32 id) +252

  • Olov 27 posts 101 karma points
    Jul 01, 2010 @ 12:08
    Olov
    0

    Solved my own problem by creating an method instead of Load<T>(), GetPage<T>() that seems to be working:

    public T GetPage<T>(int nodeId) where T : DocTypeBase, new()
    {
        return FooDataContext.Instance.LoadTree<T>().Where(x => x.Id == nodeId).FirstOrDefault();
    }

    But still it's confusing that Load<T>() doesn't work...

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 01, 2010 @ 14:04
    Aaron Powell
    0

    Well I never anticipated this usage for LINQ to Umbraco, and if you're trying to use Load<T> you'll loose a lot in performance (it doesn't work with the cache).

  • Olov 27 posts 101 karma points
    Jul 01, 2010 @ 14:09
    Olov
    0

    So whats the recommended way to get a strongly typed "CurrentPage" ?

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 01, 2010 @ 14:21
    Aaron Powell
    0

    There isn't. LINQ to Umbraco doesn't have a concept of 'current page', it doesn't understand hierarchy. That's now how it's designed.

  • Olov 27 posts 101 karma points
    Jul 01, 2010 @ 14:38
    Olov
    0

    What about caching the data inside the getter of CurrentPage and listen to Document.BeforeSave, Document.BeforeMove etc and clear the cache on those events?

Please Sign in or register to post replies

Write your reply to:

Draft