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 (http://www.acarrilho.com/).

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

Fluent UrlHelper

var u = new UrlBuilder();
var url = u.BaseUrl("http://www.google.pt/")
    .Parameters(p => p
        .Add("name", "Andre Carrilho"))
        .Add("age", "28")
        .Add("likes_global", "oh_YEAH")
    .Build();

Fluent MailHelper

var mailsWithDisplayNames = new Dictionary<string, string>
            {
                {"Another Person", "another.person@anothermail.com"},
                {"Yet Another Person","yet.another.person@anothermail.com"}
            };

var justMails = new List<string>
    {
        "wow.another.person@anothermail.com",
        "ok.another.person@anothermail.com"
    };

var mailHelper = new MailHelper("smtp.gmail.com", 587);
mailHelper
    .From("Andre Carrilho", "me@mymail.com")
    .To(to => to.Add("Andre Carrilho", "anotherme@mymail.com"))
    .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")
    .IsBodyHtml(true)
    .Credentials("someUser", "somePass")
    .Port(1234)
    .Ssl(true)
    .Send();

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("http://www.google.com");

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

Fluent Http

var response = new FluentHttp()
    .ContentType("text/xml")
    .Credentials("username", "password", "domain")
    .Encoding(Encoding.UTF8)
    .KeepAlive(true)
    .Method("POST")
    .PostData("put some xml or json or whatever you want :)")
    .PostDataEncoding(Encoding.ASCII)
    .Timeout(60000)
    .Url("www.google.com")
    .UserAgent("Mozilla/5.0")
    .Proxy(p => p
        .AddBypassRule("")
        .AddBypassRule("")
        .Address("")
        .BypassLocally()
        .Credentials("proxyUsername", "proxyPassword", "proxyDomain"))
    .Headers(h => h
        .AddRequestHeader("Accept-Language:da")
        .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"))
    .Send();

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");

Enjoy

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