Project Description

The intent of this project is to share some common functionalities that I have came across in multiple projects. Some of the tasks include working with paths, XML and Json (mostly serialization), Http, REST helper based on the WCF Rest Starter Kit Preview 2, fluent API for building URL's and sending mails. Check out some of the features on my blog (

Some samples on how to use some of these features bellow:

Fluent UrlHelper

var u = new UrlBuilder();
var url = u.BaseUrl("")
    .Parameters(p => p
        .Add("name", "Andre Carrilho"))
        .Add("age", "28")
        .Add("likes_global", "oh_YEAH")

Fluent MailHelper

var mailsWithDisplayNames = new Dictionary<string, string>
                {"Another Person", ""},
                {"Yet Another Person",""}

var justMails = new List<string>

var mailHelper = new MailHelper("", 587);
    .From("Andre Carrilho", "")
    .To(to => to.Add("Andre Carrilho", ""))
    .Bcc(bcc => bcc.Add(mailsWithDisplayNames))
    .Cc(cc => cc.Add(justMails))
    .Body("Trying out the MailHelper class with some Html: <p style='font-weight:bold;color:blue;font-size:32px;'>html</p>")
    .Subject("Testing Fluent MailHelper")
    .Credentials("someUser", "somePass")

Json and Xml Serialization

Imagine you have an object similar as the following:

var obj = new SampleObject
                    Id = Guid.NewGuid(),
                    Name = "André",
                    AnotherObject = new SampleObject.SubObject
                                            Age = 24,
                                            Birthdate = DateTime.Now

Instantiate the DataContactSerializerHelper class:

var serializer = new DataContractSerializerHelper<SampleObject>();

To serialize the object to a string or a file

// Serialize an object to a json string
var jsonString = serializer.SerializeToJsonString(obj, Encoding.UTF8);

// Serialize to a json file
serializer.SerializeToJsonFile(@"c:\temp\jsonObject.json", obj, Encoding.UTF8);

// Serialize to xml string
var xmlString = serializer.SerializeToString(obj, Encoding.UTF8);

// Serialize to xml file
serializer.SerializeToFile(@"c:\temp\xmlObject.xml", obj, Encoding.UTF8);

To deserialize from a string or a file:

// Deserialize from a json string
var deserializedJsonObj = serializer.DeserializeFromJsonString(jsonString, Encoding.UTF8);

// Deserializes from a json file
var deserializedJsonObj = serializer.DeserializeFromJsonFile(@"c:\temp\jsonObject.json", Encoding.UTF8);

// Deserialize from xml string
var deserializedXmlObj = serializer.DeserializeFromString(xmlStringj, Encoding.UTF8);

// Deserializes from a json file
var deserializedXmlObj = serializer.DeserializeFromFile(@"c:\temp\xmlObject.xml", Encoding.UTF8);

To make http request

var http = new HttpHelper();

// Send a get request
string getResponse = http.SendGet("");

// Send a post request
string postResponse = http.SendPost("", "sample post data");

Fluent Http

var response = new FluentHttp()
    .Credentials("username", "password", "domain")
    .PostData("put some xml or json or whatever you want :)")
    .Proxy(p => p
        .Credentials("proxyUsername", "proxyPassword", "proxyDomain"))
    .Headers(h => h
        .AddRequestHeader("Accept-Language", "en;q=0.8")
        .AddRequestHeader(HttpRequestHeader.AcceptCharset, "ISO-8859-1,UTF-8;q=0.7,*;q=0.7")
        .AddResponseHeader(HttpResponseHeader.CacheControl, "private"))

Rest Helper

// Making a GET request
RestHelper.HttpRestGet<SampleObject>("baseUrl", "relativeUrl");

// Making a POST request
RestHelper.HttpRestPost<SampleObject, bool>("baseUrl", "relativeUrl", sampleObj);

// Making a PUT request
RestHelper.HttpRestPut<SampleObject, bool>("baseUrl", "relativeUrl", sampleObj);

// Making a DELETE request
RestHelper.HttpRestDelete<bool>("baseUrl", "relativeUrl");


Last edited Jun 4, 2010 at 1:20 AM by andrecarrilho, version 7