Last active
October 15, 2019 08:42
-
-
Save Tewr/cb6be9234af6875a76555cc3609e7785 to your computer and use it in GitHub Desktop.
Testing Serialize.Linq for remoting proxy
This file contains hidden or 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
using Newtonsoft.Json; | |
using Serialize.Linq.Extensions; | |
using Serialize.Linq.Nodes; | |
using System; | |
using System.Linq.Expressions; | |
namespace TestingLinqSerializer | |
{ | |
class Program | |
{ | |
public static JsonSerializerSettings settings = new JsonSerializerSettings() | |
{ | |
TypeNameHandling = TypeNameHandling.Objects | |
}; | |
static void Main() | |
{ | |
var pretendThisIsRemoteService = new Service(); | |
ExecuteOnRemote(x => x.Foo("foo"), pretendThisIsRemoteService); | |
var bazArg = new BarArg | |
{ | |
OneNumber = 456, | |
Bar = "bar" | |
}; | |
var result1 = ExecuteOnRemote(x => x.Bar(bazArg), pretendThisIsRemoteService); | |
Console.WriteLine($"result1: {result1}"); | |
var result2 = ExecuteOnRemote(x => x.ItsAServiceProperty, pretendThisIsRemoteService); | |
Console.WriteLine($"result2: {result2}"); | |
Console.ReadKey(); | |
} | |
static object ExecuteOnRemote(ExpressionNode node, IService service) | |
{ | |
return (node.ToExpression() as LambdaExpression).Compile().DynamicInvoke(service); | |
} | |
static string ExecuteOnRemote(string nodeJson, IService service) | |
{ | |
ExpressionNode node = JsonConvert.DeserializeObject<ExpressionNode>(nodeJson, settings); | |
var res = ExecuteOnRemote(node, service); | |
return JsonConvert.SerializeObject(res, settings); | |
} | |
static TResult ExecuteOnRemote<TResult>(Expression<Func<IService, TResult>> thing, IService service) | |
{ | |
var request = JsonConvert.SerializeObject(thing.ToExpressionNode(), settings); | |
var execution = ExecuteOnRemote(request, service); | |
return JsonConvert.DeserializeObject<TResult>(execution, settings); | |
} | |
static void ExecuteOnRemote(Expression<Action<IService>> thing, IService service) | |
{ | |
var request = JsonConvert.SerializeObject(thing.ToExpressionNode(), settings); | |
ExecuteOnRemote(request, service); | |
} | |
} | |
public class Service : IService | |
{ | |
private string bar; | |
public void Foo(string bar) { | |
this.bar = bar; | |
this.ItsAServiceProperty = 123; | |
} | |
public string Bar(BarArg arg) | |
{ | |
return this.bar + arg.Bar; | |
} | |
public int ItsAServiceProperty { get; set; } | |
} | |
public interface IService | |
{ | |
int ItsAServiceProperty { get; set; } | |
string Bar(BarArg arg); | |
void Foo(string bar); | |
} | |
public class BarArg | |
{ | |
public string Bar { get; set; } | |
public int OneNumber { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment