DNJ RPC : The transpartent RPC model

DNJ RPC allows you to call your server methods from javascript. 

let's say you have the following C# class where you declare some functions 

namespace MyNameSpace
{
public class ServerProcesses
{
public static string SayHello()
{
return "Hello World from : " + Assembly.GetExecutingAssembly().FullName;
}
public string Say(string something)
{
return something;
}
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}

}
}

 

DNJ RPC will let you call them from javascript using this javascript code 

<script>
$(document).ready(function(){

$('#Button1').bind('click', function(){
$.DNJRPC('#SomeTargetSelector').MyNameSpace.ServerProcesses.SayHello();
});
$('#Button2').bind('click', function(){
$.DNJRPC('#target').MyNameSpace.ServerProcesses.Say('Some string here');

});
$('#Button3').bind('click', function(){
var a = GetSomeValueForA();
var b = GetSomeValueForB();
$.DNJRPC('#result').MyNameSpace.ServerProcesses.Add(a , b);
});

});
</script>

Note that the NameSpace was preserved !  

to achieve this, you have to declare the allowed methods in a json configuration file  (lets say dnj/config/dnjrpc.conf.js)

{
"Functions": [
{
"Name": "SayHello",
"Assembly": "MyNameSpace",
"Type":"MyNameSpace.ServerProcesses"
},
{
"Name": "Say",
"Assembly": "MyNameSpace",
"Type":"MyNameSpace.ServerProcesses"
},
{
"Name": "Add",
"Assembly": "MyNameSpace",
"Type":"MyNameSpace.ServerProcesses"
}
]
}
 

 Note that for both C# add methods, we only declared one entry ! parameters type will be detected automatically

 

then you use DNJ scripts loader to generate javascript stubs and namespaces. 

<script type="text/javascript" src="DNJResources.axd?conf=dnj/config/dnjrpc.conf.js"></script> 

 

click here to see a live example. (see page source for details)