Need help specifying MetaData at runtime

Last post 11-19-2009 10:46 AM by sjnaughton. 13 replies.

Sort Posts:

  • Need help specifying MetaData at runtime

    11-13-2009, 9:24 AM
    • Member
      97 point Member
    • xaeryan
    • Member since 06-18-2008, 8:18 PM
    • Posts 288

    I am trying to dynamically generate Meta Data at runtime.  It seems the InMemoryMetadataManager can do so, however I'm having difficulty using and understanding it.

    The examples I've seen are poor in that it doesn't really say what's all that different about this method.  What's the difference between putting a range on a partial class (the classic way) and a range using InMemoryMetadataManager in the global.asax (as seen in the example)?

    What I would rather, I think, is a setup where I create a class that inherits from InMemoryMetadataManager.  In that InMemoryMetadataManager class, I'd like a method that (and this is the key) uses some current attributes of the model (like what TABLE name, COLUMN name, CURRENT KEYs, etc.) to lookup metadata from a metadata reference table.   It appears this example (http://lecanutechnotes.blogspot.com/2009/01/i-read-article-from-marcin-on-asp.html) might be part of what I need, but it's a standalone class that is just called during global.asax startup, not a ProviderImplementation.

    Matt or Steve's example of a custom meta provider is sorta what I need, but add the ability to use the column/table/current key in order to lookup multiple metadata fields and apply them.  
    (http://mattberseth.com/blog/2008/08/dynamic_data_and_custom_metada.html)
    (http://csharpbits.notaclue.net/2008/10/dynamic-data-custom-metadata-providers.html)

    Does this make any sense? 

  • Re: Need help specifying MetaData at runtime

    11-14-2009, 6:23 PM
    • Star
      12,262 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,559
    • TrustedFriends-MVPs

    Sounds OK to me I have a solution that does this from a DB but the one drawback is that the InMemoryMetadataManager will not let you ScaffoldTable to false as it seems to come too late for the table to be unscaffolded.

    I also have another method that may be more flexible here: http://csharpbits.notaclue.net/2008/10/dynamic-data-custom-metadata-providers.html based on Matt Berseth's article Dynamic Data And Custom Metadata Providers from August 24, 2008 that may be of interest.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: Need help specifying MetaData at runtime

    11-16-2009, 10:33 AM
    • Member
      97 point Member
    • xaeryan
    • Member since 06-18-2008, 8:18 PM
    • Posts 288

    I'm actually already using your custom provider example.  What I'd like to add to it is the following within the GetProperties method:


    if (!propDescriptor.HasAttribute<Summary>())
                {
                    // generate the summary attribute string by looking it up via DB
                    String summary = Summary.Lookup([table], [column], [current_entity_key], [querystringvalue]);
    
                    // add it to the list
                    newAttributes.Add(new Summary(summary));
                }


    I don't know how to get the parameters for my static Summary.Lookup function call, which gets the metadata information for my custom Summary attribute from the database. 

    The scaffold limitation might be a problem later on, but for now I'm not worried about it.


  • Re: Need help specifying MetaData at runtime

    11-16-2009, 11:34 AM
    • Star
      12,262 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,559
    • TrustedFriends-MVPs

    Hi Xaeryan, all I can assume is that there is some name resolution issue what error do you get, or if no error can I assume you want to get the parameters for the static method?

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: Need help specifying MetaData at runtime

    11-16-2009, 11:47 AM
    • Member
      97 point Member
    • xaeryan
    • Member since 06-18-2008, 8:18 PM
    • Posts 288

    Sorry Steve, I'm not doing so well at explaining these for you...

    My problem is that I have no idea (in the scope of the custom provider GetProperties method) how to get the values for the name of the current table, the name of the current column, the value of the current entity key, or a querystring.  

    I see the "propDescriptor.Name", but that's the closest I've got to any of these fields.  Particularly the first three parameters I've described (table, column, entity key) are important for me to have.


  • Re: Need help specifying MetaData at runtime

    11-16-2009, 2:04 PM
    Answer
    • Star
      12,262 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,559
    • TrustedFriends-MVPs

     OK I got it Embarrassed

    Try this: 

    propDescriptor.Name

    will give you the property name aka Column name, and:

    this.GetClassName();

    Should return the current Table name.

    These are run once so QueryString's will not be available not sure what the EntityKey is is it the PK of the column?

     

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: Need help specifying MetaData at runtime

    11-16-2009, 4:01 PM
    • Member
      97 point Member
    • xaeryan
    • Member since 06-18-2008, 8:18 PM
    • Posts 288

    Yes, for example, if you are on this edit page:

    myApp\Users\Edit.aspx?User_ID=100

    The entity key would be "User_ID" with a value of 100.  Here's to hoping it is accessible from within the custom meta provider.  Embarassed

  • Re: Need help specifying MetaData at runtime

    11-17-2009, 9:15 AM
    • Star
      12,262 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,559
    • TrustedFriends-MVPs

    Sorry you can't (at least in this version) refresh the metadata it is loaded only once, and I don't think a refresh would do you would want the metadata to be read on every page post and this does not happen you WILL NOT be able to get QueryString parameters in the Metadata provider. Sad 

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: Need help specifying MetaData at runtime

    11-17-2009, 3:16 PM
    • Member
      97 point Member
    • xaeryan
    • Member since 06-18-2008, 8:18 PM
    • Posts 288

    OK, well we figured out a way to rethink our layout such that we can lookup metadata based on EntityName (this.GetClassName();) and ColumnName (propDescriptor.Name).  So thanks for those.

    sjnaughton:

    Sorry you can't (at least in this version) refresh the metadata it is loaded only once

    Does that mean I don't have to worry about it doing database lookups to get the Metadata everytime a user visits the page?  Do you know if there's any documentation on this?

    Since I don't have access to the cache layer in the provider, much like I don't have access to the QueryString, I want to make sure that some sort of "intelligent" lookup method is used.

    Every time a user loads a page, it doing one database lookup per field is not acceptable.  However, if the metadata lookups are cached...

  • Re: Need help specifying MetaData at runtime

    11-17-2009, 4:11 PM
    Answer
    • Star
      12,262 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,559
    • TrustedFriends-MVPs

    xaeryan:
    Does that mean I don't have to worry about it doing database lookups to get the Metadata everytime a user visits the page?
     Yes it will only do the lookup once the first time it need to do the tables properties/columns

    xaeryan:
    Do you know if there's any documentation on this?
    I've not looked for any I've just observed that each class and property only get called once, I do remember one of the team David Fowler said that you can reset the metadata se here: http://forums.asp.net/p/1444282/3280440.aspx#3280440

    table.ResetMetadata();

    xaeryan:

    Since I don't have access to the cache layer in the provider, much like I don't have access to the QueryString, I want to make sure that some sort of "intelligent" lookup method is used.

    Every time a user loads a page, it doing one database lookup per field is not acceptable.  However, if the metadata lookups are cached...

    Once loaded the metadata is set against the Metamodel un application restarts, except in DDv2 where you can force refresh.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: Need help specifying MetaData at runtime

    11-18-2009, 9:57 AM
    • Member
      97 point Member
    • xaeryan
    • Member since 06-18-2008, 8:18 PM
    • Posts 288

    Sweet, just checked it out in action using some breakpoints in my provider, and it's caching them alright!


    Thanks for your help!

  • Re: Need help specifying MetaData at runtime

    11-19-2009, 5:52 AM
    • Star
      12,262 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,559
    • TrustedFriends-MVPs

    xaeryan:
    Sweet, just checked it out in action using some breakpoints in my provider, and it's caching them alright!
    Hi Xaeryan, not sure what you mean Embarrassed

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: Need help specifying MetaData at runtime

    11-19-2009, 9:50 AM
    • Member
      97 point Member
    • xaeryan
    • Member since 06-18-2008, 8:18 PM
    • Posts 288

    Hehe, what I mean is that it's working perfectly.  Cool

    Thanks again!

  • Re: Need help specifying MetaData at runtime

    11-19-2009, 10:46 AM
    • Star
      12,262 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,559
    • TrustedFriends-MVPs

    That's great Xaeryan Big Smile 

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
Page 1 of 1 (14 items)
Microsoft Communities