tags:

views:

146

answers:

4

So here's the concept. I'm trying to write a C# backend for CouchDB (keep reading, this has little or nothing to do with CouchDB).

Here's the basic idea:

1) The DB Driver uploads an arbitrary string to the DB Server.

2) The DB Driver tells the DB server to run the code and produce a view

3) The DB Server loads my C# backend, hands it the input string

4) The DB Server hands the function to the C# backend

5) The DB Server hands the C# backend data to execute the function over

6) The C# Backend executes the code and result the data to the DB Server

7) The DB Server stores, or otherwise sorts and sends the results back to the DB Driver

So the problem I'm having is with the remote code execution. All my models are C# objects. So it'd be really nice to be able to write the code on the front end, and have the code transfered to the back end for execution. Without having to copy DLLs etc. Now currently in CouchDB this query language is JavaScript so it's super simple to just copy the code into a string.

Any options like this in C#? Can I get a string version of a function? Should I create a external server that hosts these DLL assemblies and serves them up to the CouchDB backends? Should I compile them on the front end, and embed the .cs files as a resource? I'm just brainstorming at this point. Any thoughts/ideas/questions are welcome.

A: 

It sounds like what you're looking for is a Javascript style eval method for C#. Unfortunately that does not exist today and is not trivial to implement. The two main approaches for dynamically executing C# code is

  1. Building lightweight methods via Reflection.Emit
  2. Compiling assemblies on the fly (via CodeDom or straight calls to csc).

Another option to consider here is to take advantage of a dynamic language. Starting with C# 4.0, it's very easy to interop with dynamic languages. Instead of the backend being passed C# code, why not pass Python or Ruby and take advantage of the DLR?

JaredPar
One of my main reasons for moving off the normal CouchDB back-end of JavaScript is my general distaste for dynamic languages. The DLR is a interesting option, but then I'm basically back in the same boat of not catching bugs at compile time.
Timothy Baldridge
A: 

Define "function." Is this arbitrary code (which must be dynamically compiled and executed), or a list of methods that could be attached to a class?

If the latter (which is what you should be doing - allowing arbitrary dynamic code execution not only violates the .NET development model (in my opinion - no flames please), it's a huge security risk), you can use a web service.

Web services and WCF services are an excellent way to distribute processing between machines.

David Lively
A: 

There's a couple of projects I've used in the past that might help you out:

Vici Parser

Vici Parser is a .NET library for late-bound expression parsing and template rendering. It also includes an easy to use tokenizer that can be used for parsing all kinds of file formats (a full-featured JSON parser is included).

This Codeproject article

This example shows an "Eval" function for C# and implements different usages of this evaluate function. The library is written in C# and can be tested with the MainForm in the solution.

Other alternatives include using XSLT scripting (something Umbraco CMS makes use of) and perhaps something like LUA.NET which would enable you to sandbox what the code can do.

Chris S
A: 

Would you be able to achieve this using Expression trees (System.Linq.Expressions)?

Perhaps some means of passing a serialised expression tree to the back end, compiling this into a lambda/Func<> oin the server side and then executing it?

Have not played with them except for some curiosity research when Linq was unveiled, but it may help.

ahin4114
That may end up working the best....we'll see.
Timothy Baldridge