Created
December 8, 2014 20:59
-
-
Save hurricanepkt/92de58e684ab4946e8be to your computer and use it in GitHub Desktop.
Persisting calculations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ActionResult Index() | |
{ | |
var f = new ScrapData {Linqed = Linqed.Invert}; | |
var col = MongoStuff.GetCollectionSafe<ScrapData>("Scraps"); | |
col.Save(f); | |
return Content(JsonConvert.SerializeObject(f), "application/json"); | |
} | |
public ActionResult Foo() | |
{ | |
var col = MongoStuff.GetCollectionSafe<ScrapData>("Scraps"); | |
var z = col.AsQueryable().First(); | |
var ctx = new compassSQLContext(); | |
var pol = ctx.stdReports.First(); | |
int retVal; | |
switch(z.Linqed){ | |
case Linqed.Invert: | |
retVal = pol.lng_premium * -1; | |
break; | |
} | |
return Content(retVal.ToString()); | |
} | |
} | |
public class ScrapData | |
{ | |
[BsonId] | |
public ObjectId Id { get; set; } | |
public Linqed Linqed { get; set; } | |
} | |
public enum Linqed | |
{ | |
Invert | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EvalProvider | |
{ | |
public static Func<T, TResult> CreateEvalMethod<T, TResult>(string code, string[] usingStatements = null, string[] assemblies = null) | |
{ | |
Type returnType = typeof(TResult); | |
Type inputType = typeof(T); | |
var includeUsings = new HashSet<string>(new[] { "System" }); | |
includeUsings.Add(returnType.Namespace); | |
includeUsings.Add(inputType.Namespace); | |
if (usingStatements != null) | |
foreach (var usingStatement in usingStatements) | |
includeUsings.Add(usingStatement); | |
using (CSharpCodeProvider compiler = new CSharpCodeProvider()) | |
{ | |
var name = "F" + Guid.NewGuid().ToString().Replace("-", string.Empty); | |
var includeAssemblies = new HashSet<string>(new[] { "system.dll" }); | |
if (assemblies != null) | |
foreach (var assembly in assemblies) | |
includeAssemblies.Add(assembly); | |
var parameters = new CompilerParameters(includeAssemblies.ToArray()) | |
{ | |
GenerateInMemory = true | |
}; | |
string source = string.Format(@" | |
{0} | |
namespace {1} | |
{{ | |
public static class EvalClass | |
{{ | |
public static {2} Eval({3} arg) | |
{{ | |
{4} | |
}} | |
}} | |
}}", GetUsing(includeUsings), name, returnType.Name, inputType.Name, code); | |
var compilerResult = compiler.CompileAssemblyFromSource(parameters, source); | |
var compiledAssembly = compilerResult.CompiledAssembly; | |
var type = compiledAssembly.GetType(string.Format("{0}.EvalClass", name)); | |
var method = type.GetMethod("Eval"); | |
return (Func<T, TResult>)Delegate.CreateDelegate(typeof(Func<T, TResult>), method); | |
} | |
} | |
private static string GetUsing(HashSet<string> usingStatements) | |
{ | |
StringBuilder result = new StringBuilder(); | |
foreach (string usingStatement in usingStatements) | |
{ | |
result.AppendLine(string.Format("using {0};", usingStatement)); | |
} | |
return result.ToString(); | |
} | |
} | |
var HelloWorld = EvalProvider.CreateEvalMethod<int, string>(@"return ""Hello world "" + arg;"); | |
Console.WriteLine(HelloWorld(42)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var f = new ScrapData {Linqed = (stdReport s) => s.lng_premium*-1}; | |
var col = MongoStuff.GetCollectionSafe<ScrapData>("Scraps"); | |
col.Save(f); | |
public class ScrapData | |
{ | |
[BsonId] | |
public ObjectId Id { get; set; } | |
public Func<stdReport, double?> Linqed { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment