C# calling REST API's using complext types (objects)

So we have been rolling out Web API's to expose functions to other systems.  This is great, but it isn't like SOAP where you have a WSDL, import it and you can code against it.  You have to do more work than that.  We don't want to be completely blind and just pass xml / json around and let each side figure out what the type looks like, so we should compile our main types to classes and share them between assemblies.  Then when we request or post a product, we can easily just have it as its native type.  Here is how.

If I wanted to post a product to a function, I would do it like this.

var client = new RestClient(Config.GetString("QASpecWebClient", true));
                var request = new RestRequest(Config.GetString("QASpecWebFunction", true), Method.POST);
                request.RequestFormat = DataFormat.Json;
                request.AddBody(product);
                var resp = client.Execute(request);

Notice how we just AddBody the product.  It is a complex type that the service in question uses as well.  Luckily RestSharp takes care of converting it to json to get it there.

If we want to request a product from a function, I would do it like this.

var client = new RestClient("http://localhost:62991/");
var request = new RestRequest("api/productapi/"+id.ToString());
request.RequestFormat = DataFormat.Json;
var resp = client.Execute(request);
var returner = JsonConvert.DeserializeObject<IProduct>(resp.Content);
return returner;

Notice how we just do the same client request setup, don't add a body, and then use the deserialze function template to our type and boom, we have a complex type with no messy parsing.  Enjoy!

 

Oh, don't forget to include Newtonsoft.Json and RestSharp! 

Serialize Object Data to an XML File C#

Saving and loading objects to and from files is one of the first things they teach you when learning to program.  The text file was the original format of choice and it is still used widely today.  Xml was created to be easy to read like text and yet structured to allow relationships to exist in storage.  Occasionally today you still run across the need to store data to something other than a database.  But writing out files from classes means writing functions on your object to read and write it from some sort of file format you must establish. 
While this isn't necessarily hard, it is time consuming and a point of failure and complexity.  I had written a few code generators that could generate a simple reader and writer to xml files given any class, but that was also cumbersome.  What I found out is you can do this inherently with the System.XML library using the XmlSerializer which is very easy to use and saves a lot of time.  You just need to dress your classes.
Here is an example, I have relatively simple set of objects that represent a set of customer statements.
[Serializable]
        public class Statement
        {
            public string AccountNumber { get; set; }
            public int Sort { get; set; }
            public string SubSort { get; set; }
            public decimal SackNumber { get; set; }
            public decimal PackNumber { get; set; }
            public decimal Key { get; set; }
            public string RateLevel { get; set; }
            public string Endorsement { get; set; }
            public string Payload { get; set; }
            public string Stuffer { get; set; }
            public string CustomerNumber { get; set; }
            public string Company { get; set; }
            public string Plan { get; set; }
            public decimal Balance { get; set; }
            public decimal MinimumPayment { get; set; }
            public decimal PastDueAmt { get; set; }
            public string Name { get; set; }
            public string Address1 { get; set; }
            public string Address2 { get; set; }
            public string Address3 { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Carrier { get; set; }
            public string Zip { get; set; }
            public DateTime FinalDate { get; set; }
            public string Prefix { get; set; }
            public string Zip2 { get; set; }
            public bool PastDue { get; set; }
            [XmlArray("MessageList"), XmlArrayItem(typeof(Message), ElementName = "Message")]
            public List<Message> Messages { get; set; }
            [XmlArray("DetailList"), XmlArrayItem(typeof(Detail), ElementName = "Detail")]
            public List<Detail> Details { get; set; }
            public Statement()
            {
                Details = new List<Detail>();
                Messages = new List<Message>();
            }
        }
        [Serializable]
        public class Message
        {
            public Message()
            { }
            public decimal LateCharge { get; set; }
            public decimal MinPayMonths { get; set; }
            public decimal MinPayTotal { get; set; }
            public decimal ThreeYearPayment { get; set; }
            public decimal ThreeYearPayTotal { get; set; }
            public decimal YTDFees { get; set; }
            public decimal TYDInterest { get; set; }
            public string MessageString { get; set; }
            public string Last4 { get; set; }
            public decimal EndRate { get; set; }
            public decimal EndFinCharge { get; set; }
            public decimal EndAmtFinanced { get; set; }
            public decimal EndTotalPayout { get; set; }
        }
        [Serializable]
        public class Detail
        {
            public Detail() { }
            public DateTime TransactionDate { get; set; }
            public string TransNumber { get; set; }
            public string TransDescription { get; set; }
            public decimal TransAmount { get; set; }
            public char TransDisputeFlag{ get; set; }
            public string SubPlan { get; set; }

            public string Filler { get; set; }
        }
As you can see I dressed child objects with a xmlroot and serializable.  I also had to make sure all types used in classes and the classes themselves have default constructors that are public and take in to parameters.  And then simply call the save method and boom, you have saved your object to an xml file and can read it with the load method later.  Enjoy!