Not knowing MongoDB? Read it here:
https://en.wikipedia.org/wiki/MongoDB
"MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. "
Today we talk about how to write a common class to access end points ( Hosted seperately) that allow us to access MongoDB collection. You can read / write collections through this.
Lets go straight to the code:
public class MongoService
{
public XmlDocument Select(Method method, string ServiceAddress, Dictionary<string, string> Header, Dictionary<string, object> Parameter)
{
var client = new RestClient(ServiceAddress);
client.Timeout = -1;
var request = new RestRequest(method);
foreach (var h in Header)
request.AddHeader(h.Key, h.Value);
foreach (var p in Parameter)
request.AddParameter(p.Key, p.Value);
IRestResponse response = client.Execute(request);
if (string.IsNullOrEmpty(response.Content))
return null;
var node = JsonConvert.DeserializeXNode(response.Content, "Root");
string xml = node.ToString();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
return doc;
}
public string Select<T>(Method method, string ServiceAddress, Dictionary<string, string> Header, Dictionary<string, object> Parameter)
{
var client = new RestClient(ServiceAddress);
client.Timeout = -1;
var request = new RestRequest(method);
foreach (var h in Header)
request.AddHeader(h.Key, h.Value);
foreach (var p in Parameter)
request.AddParameter(p.Key, p.Value);
IRestResponse response = client.Execute(request);
return response.Content;
}
public string Select<T>(Method method, string ServiceAddress, Dictionary<string, string> Header)
{
var client = new RestClient(ServiceAddress);
client.Timeout = -1;
var request = new RestRequest(method);
foreach (var h in Header)
request.AddHeader(h.Key, h.Value);
IRestResponse response = client.Execute(request);
return response.Content;
}
public string Save<T>(Method method, string ServiceAddress, Dictionary<string, string> Header, Dictionary<string, object> Parameter)
{
var client = new RestClient(ServiceAddress);
client.Timeout = -1;
var request = new RestRequest(method);
foreach (var h in Header)
request.AddHeader(h.Key, h.Value);
foreach (var p in Parameter)
request.AddParameter(p.Key, p.Value);
IRestResponse response = client.Execute(request);
return response.Content;
}
public string Update<T>(Method method, string ServiceAddress, Dictionary<string, string> Header, Dictionary<string, object> Parameter)
{
var client = new RestClient(ServiceAddress);
client.Timeout = -1;
var request = new RestRequest(method);
foreach (var h in Header)
request.AddHeader(h.Key, h.Value);
foreach (var p in Parameter)
request.AddParameter(p.Key, p.Value);
IRestResponse response = client.Execute(request);
return response.Content;
}
public string Delete<T>(Method method, string ServiceAddress, Dictionary<string, string> Header, Dictionary<string, object> Parameter)
{
var client = new RestClient(ServiceAddress);
client.Timeout = -1;
var request = new RestRequest(method);
foreach (var h in Header)
request.AddHeader(h.Key, h.Value);
foreach (var p in Parameter)
request.AddParameter(p.Key, p.Value);
IRestResponse response = client.Execute(request);
return response.Content;
}
}
The MongoService class has the following methods.
public XmlDocument Select(Method method, string ServiceAddress, Dictionary<string, string> Header, Dictionary<string, object> Parameter)
This return the result as an xml document.
The parameters contain;
- Method
- ServiceAddress
- Header
- Parameter
The method is the restsharp method as it is GET or POST or any other.
Service address is the service address.
Header is the Dictionary of strings for headers.
Parameter is the Dictionary of string and object values for the parameters.
Using this class will help to ease with dealing the end points.
Happy coding.
No comments:
Post a Comment