quinta-feira, 15 de agosto de 2013

Strategy design pattern

Hello,

I have to say that I'm working around design patterns since about two years ago. I think I know more that average but still long way to go.. So the best thing to do is to climb on the giant's shoulders :)

I spend a lot of time with books no so good as these 3:

So if you are think to study design pattern, make the right move choosing the right book.

But books is a long way plan let's understand strategy design in the next 15 minutes! 
Two months ago, my friend gave me this link click here (11:32). I found it a great resource to understand design patterns quick.

After the great above video - very well explained in "how to use" and "why" point of view.

Image from wikipedia


I will add one more drawback of this design pattern that is not mentioned in the video: 
  • The strategy interface is shared between all concrete strategy. So we have to be careful with our concrete, to not make some concrete strategy that doesn't need to do any action of strategy interface, in order to not violate Interface Segregation Principle.
Cheers.

segunda-feira, 27 de maio de 2013

XML Serialization with Generics

Hello,

After a long pause writing posts, I'm back to speak about one very nice feature that .Net brought to us: Generics. I'm writing about Generics now, because in this post I will implement a XML Serialization Helper class, that will be very useful to finalize my previous post about Send Email.

This will be a short post, as there are a extension information about Generics on internet. For example: An Introduction to C# Generics - 2005.

Generics provide the following benefits:
  • Code Reuse
  • Type Check on compile time
  • Performance
if we want to write some XML Serialization Helper class, this can be the API one solution:
public interface IXMLSerializationHelper
{
        string SerializeToString(Newsletter objToSerialize);
        Newsletter DeserializeToString(string objToDeserialize);
}

Problem: The problem of this solution is that, Newsletter are hard-code. If we need to serialize Product Class tomorrow, we will have to write a new API.

Solution 2:
public interface IXMLSerializationHelper
{
        string SerializeToString(Object objToSerialize);
        Object DeserializeToString(string objToDeserialize);
}

Problem: This is a improvement against solution 1, but still not the best solution as this will require boxing that will affect the performance. For more have a look here

Solution 3 using Generics:
public interface IXmlSerializationHelper<T>
    {
        string SerializeToString(T objToSerialize);
        T DeserializeToString(string objToDeserialize);
    }

public class XmlSerializationHelper<T> : IXmlSerializationHelper<T>
    {
        /// <summary>
        /// Serializing to String
        /// </summary>      
        public string SerializeToString(T objToSerialize)
        {
            var sb = new StringBuilder();
            var serializer = new XmlSerializer(typeof(T));
            using (var writer = new StringWriter(sb))
            {              
                serializer.Serialize(writer, objToSerialize);
            }

            return sb.ToString();
        }


        /// <summary>
        /// deserializing to T type
        /// </summary>
        public T DeserializeToString(string data)
        {
            var serializer = new XmlSerializer(typeof(T));
            T result;
            using (var reader = new StringReader(data))
            {
                result = (T) serializer.Deserialize(reader);
            }
            return result;
        }
    }

This solution take advantage of Generics. Examples:

var model = GetNewsletterModel();
var serializer = new XmlSerializationHelper<NewsletterModel>();
serializer.SerializeToString(model);

Or

var model = GetProductsModel();
var serializer = new XmlSerializationHelper<ProductsModel>();
serializer.SerializeToString(model);

This is all for now. I hope come back soon.

domingo, 21 de abril de 2013

Sending Email (Part 1)

Sending Email (Part 1)

Email functionality is a common feature in many applications, which can be triggered by user action (ex.: email confirmation after buying a online product) or direct email (ex.: newsletter).

Normally this is done in about 4 steps:
  1. Get template from some source: database, file, etc.. 
  2. Get a business object with information to be personalized;
  3. Configure message body to be sent;
  4. Send Email.
One important task is prepare the message to be sent (3), we have several solution, lets analyze some.

Solution 1: Make template with tags to be replaced later. Example:
var template = "Hi [Name], Here's some activity you may have missed on Facebook.";
string messageBody = template.Replace("[Name]", "Adilson");

Problem of this Solution:
  • Hard to configure the template according to the business data. Ex.: To send list of news we have to do more than find replace tags..

Solution 2: Using Razor Engine(open source project) to build templates. Example:
string template = "Hi @Model.Name, Here's some activity you may have missed on Facebook.";
string messageBody = Razor.Parse(template, new { Name = "Adilson" });

Problem of this Solution: 
  • Still less flexible..

Solution 3: Using XML and XSLT to transform documets and to get HTML.


This solution is more flexible than the previous two, as it allow us to use the power of XSLT to Transform documents. More about XSLT can be found in any search engine, but this is my suggestion: w3schools

How to get message body using solution 3:
  1. Get XSLT file (template);
  2. Get XML: serializing the business object;
  3. Call the processor given the XSLT and XML to generate the HTML(message body).

Suppose we are looking to send this newsletter:

Your Logo Here
Date Here
Issue #
Newsletter Title Here
USER GRETINGS...

NEWS TITLE 1
NEWS TITLE 2
advertisement here
TITLE 1

TEXT NEWS 1... Read Full Article
TITLE 2

TEXT NEWS 2 ... Read Full Article
Copyright © 2013 Your Company Name. All Rights Reserved.

So to implement solution 3, lets summarize the steps:
  1. Get template from some source: XSLT File;
  2. Get a business object with information to personalize: Serializing to XML;
  3. Configure message body to be sent: Applying XSLT Processor to get the message body;
  4. Send Email.
Implementation will be in the next Post.

quinta-feira, 18 de abril de 2013

.Net Developers vs Java Developers

.NET Developers vs .Java Developers

Is easy to find over internet, for example click here, forums discussing about .Net vs Java Developers. I would like add my point of view as well..

Typically, the goal of this discussion is to understand, which developers group, are better. I had similar discussion about 10 years ago, when I was at university in Portugal, discussing about which University was better!

Sometimes or most of the time, this discussion doesn't make sense, because people want to "know" the smarter groups, rather than they ability to build Applications.

For .NET developers, Microsoft is doing a great job putting a lot of effort to improve .NET (click here for more info) And this great job has some benefits for .Net Developers, as is making the live easy for them.

Java developers, doesn't have a big "daddy", like Microsoft! So they develop the ability to work together, and build thousands of Open source communities that have been doing a great job, helping and pushing the group to use the best available practices. So to talk each other effectively, as a big team, with no doubts about, they had to push for Design Patterns. Downside for Java developers is that, very easy to get lost, as there are, sometimes, many alternatives for the some thing, not good documentation and so hard to configure.

Going back to .Net developers, downside of Microsoft efforts, is that developers gain the habit of "eat a ready food". That was OK, for most of the project yeas ago, as they were small in the early years of .Net. But not Nowadays,  is not true as there are many big projects to build and maintain in a very powerful tool: .Net Framework.

Microsoft is pushing developers to work in agile environment, and build more open source communities much more than before, basically recognizing that isn't easy prescind the "eyes" and R&D of thousands of people around the world. One example is ASP.NET MVC Framework.

So as a .NET developer, for the next articles I will share my thoughts about Programming in .NET, based in my experiences and researches, with main focus on design patterns and data structure.

Thanks for your time:)