Use Composite's built-in Xml Functions for a set of Data

Topics: General, Standard packages, Troubleshooting
May 24, 2012 at 11:51 PM

In C#, is it possible to take a set of data and call Composite's built-in Data Functions that Generate Xml?

I have a function that is like ArticlesInDateRange(DateTime fromDate, DateTime, toDate)

public static XElement ArticlesInDateRange(DateTime fromDate, DateTime toDate) {            
  using (DataConnection connection = new DataConnection()){
     var articles = from a in connection.Get()
         where (a.ArticleDate >= fromDate && a.ArticleDate < toDate)
         select a;
               
  // What do I do here to return a similar structure 
  //as the GetArticleXml Function that Composite has built-in to data types?

  }            
}

Ideally, I'd like to call the same Function that Composite does for the Xml. 

 

May 25, 2012 at 12:51 AM

This should work:

 

// Grab a function object to execute
IFunction function = FunctionFacade.GetFunction("Your.Function.Name");

// Add parameters to this:
var parameters = new Dictionary<string,object>();

// Execute the function, passing all query string parameters as input parameters
var functionResult = FunctionFacade.Execute<IEnumerable<XElement>>(function, parameters, null);

 

The generic type for Execute should match the type the function returns - or just use <object>.

May 25, 2012 at 12:58 AM

I must be tired - I misunderstood the question.

I suggest you just new up XElements and XAttributes, like

public static IEnumerable<XElement> ArticlesInDateRange(DateTime fromDate, DateTime toDate) {            
  using (DataConnection connection = new DataConnection()){
     var articles = from a in connection.Get()
         where (a.ArticleDate >= fromDate && a.ArticleDate < toDate)
         select new XElement("Article",
             new XAttribute("Id", a.Id),
             new XAttribute("Title", a.Title),
             new XAttribute("Etc", a.Etc));
  return articles;
  }            
}
Notice the different return value.

May 25, 2012 at 1:28 AM
Edited May 25, 2012 at 1:48 AM

-- DELETED --

Whoa.. don't know why it did that. That was an email reply.

Here's the message:

Well, the other code was interesting as well. ;-)

I guess there is no way to access the built-in GetArticleXml function that Composite creates when you create an IData type?

The other option I thought of was create a

Alternatively, I could apply an AND CompoundPredicateFilter on the GetArticleXml with two FieldPredicate filters on the ArticleDate field.

I should probably look at how the Blog does it as I am going to be reading the year and month from the path info and be displaying all the articles for that month.

May 25, 2012 at 1:48 AM

There... i fixed that message. Weird.

May 25, 2012 at 2:45 AM
Edited May 25, 2012 at 2:46 AM

The Get(datatype)Xml functions that get auto-created when you make new types come from this class http://compositec1.codeplex.com/SourceControl/changeset/view/17098#45990 - there is actually a DataToXElements<T> class nested in there which might be useful :)

The blog sources is over at http://c1packages.codeplex.com/