Code Project published my last work about a new .NET library MockData for generating fake data.
You can read it at http://www.codeproject.com/Tips/697006/Creating-mock-data-for-software-testing
I look forward to hearing your feedback
Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts
Thursday, December 26, 2013
Friday, December 13, 2013
C# Ruby Style
Among other awesome features introduced in C# 3.0 are amazing extension methods. It looks like taking on board dynamic-language-oriented people. Now this Ruby-style can be done in C#:
1.To(7).DoSomething();
Woohoo! It is amazing ... the feature is quite powerful and if you think about that a little bit intimidating for a static-language mind. You will be definitely more comfortable with the idea of such a freedom.
How you can do it ? With a small piece of smart code
public static class Extension
{
public static IEnumerable<int> To(this int from, int to)
{
return from < to
? Enumerable.Range(from, to - from + 1)
: Enumerable.Range(to, from - to + 1).Reverse();
}
public static void DoSomething(this IEnumerable<int> values)
{
foreach (int i in values)
{
Console.WriteLine(string.Format("{0} Count : {1}","Do Something",i));
}
}
}
And you can write
class Program
{
static void Main(string[] args)
{
1.To(7).DoSomething();
Console.ReadKey();
}
}
Enjoy yourself
{
public static IEnumerable<int> To(this int from, int to)
{
return from < to
? Enumerable.Range(from, to - from + 1)
: Enumerable.Range(to, from - to + 1).Reverse();
}
public static void DoSomething(this IEnumerable<int> values)
{
foreach (int i in values)
{
Console.WriteLine(string.Format("{0} Count : {1}","Do Something",i));
}
}
}
And you can write
class Program
{
static void Main(string[] args)
{
1.To(7).DoSomething();
Console.ReadKey();
}
}
Enjoy yourself
Saturday, December 7, 2013
Strategy Pattern
Directly from the Strategy Pattern Wikipedia article:
The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them.
Let's see how it works using C# language. In my sample i show you as we can change the Sort Algorithm using a Context class to implement this swap.
namespace StrategySample
{
public interface IStrategy
{
List<string> SortData(List<string> inputData);
}
// We declare a class to implement QuickSort ordering algorithm
public class QuickSort : IStrategy
{
public List<string> SortData(List<string> inputData)
{
List<string> sortedData = new List<string>();
// do QuickSort algorithm to sort data
return sortedData;
}
}
// We declare a class to implement BubbleSort ordering algorithm
public class BubbleSort : IStrategy
{
public List<string> SortData(List<string> inputData)
{
List<string> sortedData = new List<string>();
// do BubbeSort algorithm to sort data
return sortedData;
}
}
// We declare a class to implement the context for sorting
public class SortContext
{
IStrategy _strategSort;
public SortContext(IStrategy strategy)
{
_strategSort = strategy;
}
public List<string> Sort(List<string> inputData)
{
return _strategSort.SortData(inputData);
}
}
}
Now let's see the pattern in action
static void Main(string[] args)
{
SortContext context;
List<string> data = new List<string>();
List<string> sortedData = context.Sort(data);
// Create the inputData
data.Add("string one");
data.Add("string two");
// We apply QuickSort algorithm
context = new SortContext(new QuickSort());
sortedData = context.Sort(data);
foreach (var item in sortedData)
{
Console.WriteLine(item);
}
// We apply BubbleSort algorithm
context = new SortContext(new BubbleSort());
sortedData = context.Sort(data);
foreach (var item1 in sortedData)
{
Console.WriteLine(item1);
}
}
Basically we inject the Interface implementation into the Context Strategy class and we use polymorphism to run the different behavior.
The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them.
Let's see how it works using C# language. In my sample i show you as we can change the Sort Algorithm using a Context class to implement this swap.
namespace StrategySample
{
public interface IStrategy
{
List<string> SortData(List<string> inputData);
}
// We declare a class to implement QuickSort ordering algorithm
public class QuickSort : IStrategy
{
public List<string> SortData(List<string> inputData)
{
List<string> sortedData = new List<string>();
// do QuickSort algorithm to sort data
return sortedData;
}
}
// We declare a class to implement BubbleSort ordering algorithm
public class BubbleSort : IStrategy
{
public List<string> SortData(List<string> inputData)
{
List<string> sortedData = new List<string>();
// do BubbeSort algorithm to sort data
return sortedData;
}
}
// We declare a class to implement the context for sorting
public class SortContext
{
IStrategy _strategSort;
public SortContext(IStrategy strategy)
{
_strategSort = strategy;
}
public List<string> Sort(List<string> inputData)
{
return _strategSort.SortData(inputData);
}
}
}
Now let's see the pattern in action
static void Main(string[] args)
{
SortContext context;
List<string> data = new List<string>();
List<string> sortedData = context.Sort(data);
// Create the inputData
data.Add("string one");
data.Add("string two");
// We apply QuickSort algorithm
context = new SortContext(new QuickSort());
sortedData = context.Sort(data);
foreach (var item in sortedData)
{
Console.WriteLine(item);
}
// We apply BubbleSort algorithm
context = new SortContext(new BubbleSort());
sortedData = context.Sort(data);
foreach (var item1 in sortedData)
{
Console.WriteLine(item1);
}
}
Basically we inject the Interface implementation into the Context Strategy class and we use polymorphism to run the different behavior.
Welcome
This blog is based on my past 18+ years of experience in software development industry. I have gone through different stages in my career starting from trainee software developer till senior management.
I do not want to keep my learnings to myself, so I had created this a small blog, I thought of revising it and adding few more learnings which may benefit many other software engineers and developers working in this lovely industry.
I'm not going to dictate any of the points, but all the practices listed here contributed a lot in my software development career, so if you think they make some sense for you then try to adopt few. If you have any +/- comments, kindly feel free to write me back
Etichette:
C#,
MVC,
Software Development
Subscribe to:
Posts (Atom)