Shaul Behr
Shaul Behr

Reputation: 38003

Looking for a generic way to handle Linq reports & return results in XML

I'm designing a reporting engine for my project. The project will have several reports, each of which can be expressed as a Linq query; some will have parameters, while others won't.

Now in order to meet my clients' requirements, I need to return the results of the queries in XML format, so that they can apply an XSL transform to make it look pretty according to their own taste.

So, my plan thus far is to have an abstract Report base class, from which all my reports will be derived. The base class will need the following methods:

public virtual void SetParameter(string ParameterName, object Value) {
  throw new NotImplementedException("This report does not accept parameters.");
}

public abstract object GetData(); // what return type?

public XElement GetXMLData() {
  // calls GetData() and uses some kind of reflection to turn the data into XML?
}

To flesh this out a little more: SetParameter() is fairly self-explanatory. GetData() is supposed to contain the Linq query, and return an IEnumerable<'a> type, where 'a is the anonymous type of the final result. But obviously you can't declare something as being of anonymous type, so I need some other generic way of relating to the results of my Linq query - one that will allow me to iterate through the returned fields and create an appropriate XML schema.

What should the return type of GetData() be? How do I get the field names/values? Alternatively, is there a better way to achieve my objective?

Upvotes: 1

Views: 362

Answers (2)

dahlbyk
dahlbyk

Reputation: 77500

Could you make your base report class generic over the desired data type?

public class ReportBase<TEntity> {
    public virtual void SetParameter<T>(string ParameterName, T Value) {
        throw new NotImplementedException("This report does not accept parameters.");
    }

    public abstract TEntity GetData();

    public XElement GetXmlData() {
      // calls GetData() and uses some kind of reflection to turn the data into XML?
    }
}

You could also restrict TEntity to be of some base type that exposes how to serialize itself, simplifying GetXmlData. Have you considered using .NET's built-in XML serialization features?

Upvotes: 1

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65371

Is your data stored in SQL server?

If so why not use SQL Server Reporting Services? It comes "free" with SQL Server. It would be a lot more productive than building a report engine from scratch.

Upvotes: 1

Related Questions