sql server programming:
I want to be able to provide an easy way for users to dynamically create
calculations to run on data in my database (SQL 2005). The data that the
calculations will process will be in XML format stored in a field in the
database. I want the user to be able to use my application write an
equation like:
((A+B)*10)-C
The software will know how to dynamically build an assembly containing a
class that can take some XML-format data, extract values for A, B, and C,
plug them into the formula, and provide a result. All of the calculations
will have a common interface/base class, but users could create any number
of these calculations.
In addition to the dynamically compiled calculation classes, I will have a
class designer (UI control) allowing users to safely design new
calculations, and a class builder that will actually create the class and
assembly from the user's input.
The database will have a stored procedure that will take as input the
name/id of a dynamically created calculation class, and some XML data, and
spit out the result of the specified calculation. An example of the SQL
that would use the dynamic calculations is:
SELECT
cdv.ClientName,
sp_Calculator( 'AlphaDynamicCalculationClassName', cdv.XmlDataField) AS
AlphaScore,
sp_Calculator( 'BetaDynamicCalculationClassName', cdv.XmlDataField) AS
BetaScore
FROM
ClientDataView cdv
The stored procedure in the database will need to know about the base
class/interface shared by all of the dynamically-created classes. Also, the
class builder in my application layer will need to know about the base
class/interface so it can construct the descendent/implementing classes. I
understand that SQL can't access stuff in the GAC, so I guess I'll have to
have base assembly/class in the GAC, and also loaded into SQL Server? I
take it as long as I'm using strong names for my base class/interface
assembly, it'll be okay to have 2 copies of the assembly: one in the GAC and
one in SQL Server? The dynamically compiled calculation classes will only
be used in SQL Server so I don't think they need to go in the GAC, unless...
Another wrinkle is that the user may need to update calculations they've
previously created. A dynamically created calculation class will have
methods so that I can interrogate the class to find out the details of the
calculation the user specified. This way, I can display their calculation
in the calculation designer so they can edit it, and I can build a new
version to replace the old one. But if the dynamically-compiled assemblies
are only in SQL Server, is there a way for me to access those classes from
outside SQL Server (i.e. from my business layer)? Or, do I need to be
storing these dynamically compiled assemblies on disk and in the SQL
database, as I do with the base class assembly?
FYI, there are a few reasons I'm doing this. My users need to be able to
create their own calculations. They won't have the ability to write their
own T-SQL or .NET code that will be able to process the XML data to get the
results. Also, I will be providing a number of 'standard' (not dynamically
alterable) calculations that will contain proprietary logic, and storing
this logic in pre-compiled, obfuscated assemblies. This will provide more
protection than if I tried to write this code in T-SQL. I also expect that
the performance of compiled .NET code calculating a value from the XML will
exceed that of T-SQL.