myfavoritemovies.us

April 27, 2009

Pex and Code contracts

Filed under: Research — @ 8:57 pm

Last week I saw couple of things in Microsoft Dev Labs that looks very interesting and worth watching and give it a try. They are

I love TDD and it indeed saved lot of my time on maintenance. I can go on and on about it, but that is for one another post. But to be fair, even though I love TDD everyone knows it take time to adopt and sometimes I get lazy as well. One line code change I am going to make why would I want to write 20 mine code to test 🙂

Anyway, the reason I was excited about above mentioned two things, it looks like they could help alleviate the pardon of writing too many repetitive tests, when you can generate most of the input validation through those two. I am going to experiment with those two in coming weeks and do an update about these two products.

MVPs, please blog more…

Filed under: Uncategorized — @ 8:45 pm

I am sucker of reading technical blogs. It is like reading free chapters from recent books for most of the blogs. I follow blogs in C#, F#, .Net, Ruby and some Java. Most of the blogs that I follow, they tent to write good blogs as often as possible. For last month or so, I finish reading all my RSS feeds in a week or so my unread feed is less than 5 most of the time. So I decided to venture out and find some more blogs that I can read and use.

My obvious choice was Microsoft web site. I searched for C# blog and Google took me to Microsoft C# MVP list. I was so happy to see number of MVPs and I recognized couple of the MVPs since I follow their blogs. I really thought I hit the mother load of good blogs, but to my surprise most of them don’t blog at all or they are blogging somewhere else that Microsoft web site doesn’t have the correct blog address. Either way, I would like to see MVPs to blog more often so that they can share the wealth of knowledge they have with the community. If they are blogging then Microsoft need to update the MVP list with proper blog address. To my surprise some of them didn’t even blog for the last month and they have MVP status? I hope I am wrong.

Please blog more…

Technorati Tags: ,

March 27, 2009

CLR and DLR

Filed under: DLR — @ 3:00 pm

Now that IronRuby and IronPython are getting good exposure, I thought it is good to revisit what is DLR. DLR stands for Dynamic Language Runtime similar to CLR as Common Language Runtime. Before we go into DLR lets look at what is CLR.  The main idea behind CLR is that, remove most of the common functionalities that static programs is going to use, implement it in CLR so that when creating new static type language the developers can concentrate on the language features rather than worrying about the common language features like garbage collection (GC) or just in time compilation (JIT). Now lets look at the DLR, it is exactly same as CLR in concept, DLR has common features of dynamic languages compared to static language features in CLR. So that language developers can implement language specific  when designing and developing the dynamic language.  It also means that if we would develop new dynamic language on top of DLR, we could use libraries written in static languages like C# which run on top of CLR.

It is my understanding that, IronPython was developed first and when IronRuby work started, John Lam and others separated the dynamic language features out and created the DLR. Initially they attempted IronPython, IronRuby, VB and Javascript on top of DLR. But after initial attempt VB and Managed Javascripts were dropped and only IronPython and IronRuby stayed on top of DLR.

As per Wiki the dynamic language provide the following

I would like to go back and write more on this so that I get my head around the basic fundamentals correct 🙂

Technorati Tags: ,,

February 27, 2009

C# 4.0 New features – Optional/Default values in Method calls

Filed under: C# 4.0 — @ 7:18 pm

I been wanting this feature in C# for some time. I am happy they are going to implement it in C#4.0. Even though C# has some very new cool features, I think this is one of the feature that ordinary programmer would use everyday. There are times I do not want pass a parameter to a method. If the parameter is not passed, I want it to get a default values. In C#3.0 we can not do it. The solution to this problem is to write wrappers for each missing parameters and then assign the default value and then call the actual method. It was ok, but too many Unit tests and add more lines of code for possible bug somewhere.

With this new feature we can get the best of what other languages were enjoying for sometime. Lets look at a example

public class Personal

{

        public int GetEmployeeListing(string name = “*”,

                       int age = 20,

                      char sex = ‘f’)

      {

      …

     }

   static void Main()

  {

      Console.WriteLine(GetEmployeeList(age:30));

      Console.WriteLine(GetEmployee(sex:’m’, age=25);

  }

 

As you can see in the above example, ‘GetEmployeeListing’ takes 3 parameters and all of them have default values. So in the Main when we invoke the write WriteLine method, we passed only the age field, thus using name and sex to default value. The second call shows one additional benefit of this definition, parameters are not absolute anymore once you name them. In this case, we change the order and passed sex and age so when the program it name will take the default value.

Of all the features I read, this one will be the one used heavily but again, it is too soon to guess anything.

February 24, 2009

IE8 Beta crash!

Filed under: Uncategorized — @ 3:18 pm

I have been using IE8 Beta for some time now, I really enjoy all the cool new features they have implemented in it. The Addon features for social networking was very good and I used them a lot. Anyway, the point I was getting at is the IE8 crashes. If you cut and past the URL text in to address bar, IE8 crashes.  One good news is IE8 is ability to restore to its last known state and that is also a very good feature I love. Since I wasn’t doing lot of URL pasting, I didn’t pay attention to it at all. I have safari (I know what you are thinking Safari???, I use Mac at home) so I was ok.

Yesterday I was doing some research and I had the same crash. So I decided to hunt down the problem that was causing the problem. I faithfully googled it and got the answer. In case you are facing the problem, here is what you need to do

1. Go to Tools –> Options

2. Under Content –> Choose Settings under AutoComplete

3. Check Web Address

That is it. No more crashes.

have fun.

September 26, 2008

? and ?? feature in C#

Filed under: C# 3.0 — @ 1:47 pm

In C# 3.0 there is a new feature assign and check the null values that lot of people doesn’t know it even exists. Well I didn’t know till couple of months ago. Even though I knew, there were couple of good circumstances I could have used it but never used it. So yesterday I decided to look at the code and see if I can save some typing by using this operators.

Lets jump into an example and look at the operator in action. I like examples more than theory so here it goes…

? is currently used as a trinary operator as we all know. Good example would be

string resturnResult = (value == null)?””:value;

in the previous statement, we are first checking to see if the value is null then assign an empty string to the returnResult else just assign the value in the value variable.

Now the use of ?? operator. The same statement we look at before would be

string returnResult = value ?? “”;

It does the same thing as the earlier statement we look at.  ?? operator will check and see if the value string is null and if it is null then it will assign the value to its right, else assign the value. Isn’t it elegant? I like this code better than the earlier one. We can argue about readability but once you start using it you will learn to read it as well.

Now going back to one other operator ‘?’ Currently it is used in the trinary operator, now it is a new face as well. Something like the following

int? age;

This basically says, the integer variable age could take null value. Why would someone want to do that, to be honest I haven’t used it. So in my limited knowledge on this subject, I could see the use in database side. You might run a stored proc and get user information from table and assign values to these variable which could take null values. If some rows doesn’t have age value set in the columns, that could come as null. So when the value is retrieved and it could be assigned to age variable without throwing an exception. Even though that is the intend, I haven’t sold on this idea yet. So if you any of you have used this operator, please drop me a note.

Thanks and happy coding…

September 16, 2008

Exception handling in NUnit

Filed under: NUnit,Tips and Tricks — Tags: , — @ 2:52 pm

This is very simple but I thought, it is worth a note (or for my note taking).  Before I go into handling exception, I need to tell you something I read couple of days ago. I can not remember which blog (I follow lot of blogs) but it kinda stuck with me. When you code you try to follow the following four things

  • Make it correct
  • Make it clear
  • Make it concise
  • Make it fast
I was tossed it to my friend Jim at work (I consider him very sharp and talented and look up to him). Here is what he has to say 
  • Write a test
  • Make it pass
  • Refactor
  • Optimize if needed <- condition seldom met, may be 1/10000 loops
  • repeat the loop
If you look closely both of them are same. What Jim specified in the loop above more of TDD in mind and the earlier one is much more general and it can adapt to any environment. But I thought it is worth sharing.
Lets go back to NUnit thing I was talking about. In one of coding situation, I need to throw an exception with just a string. Something like the following
throw new Exception(“Test exception”);
Now when you test it in NUnit, you need to make sure you use
[ExpectedException(“System.Exception”, ExpectedMessage=”Test exception”)]
thats it, nothing more. See this is very simple blog.

September 3, 2008

Some cool string manipulation in C#

Filed under: C# 3.0 — @ 2:04 am

Today I was writing a simple code which would readin a commandline parameters like

name param1=value1 param2=value2 param3=value

from the values it is very obivious the frst parameter is a individual name and others are named parameters with associated values. If we could do it in our normal mode, we would first assign the first parameter to the unique name field and then we would split each following arguments and put it in a array or arraylist or some sort.

Since it is key value pair the best bet would be Dictionary or Hash and I prefer Dictionary. For some reason I never came across a situation I had to use hash yet. Anyway, so I was going to write the split code and method and then I realised I could do the split and assign it o an arry in single statement and here is how I did it.

string[][] pharsed = argument.Skip(1).Select(s => s.split(‘=’)).ToArray();

IDictionary<string, string> dictionary = new Dictionary<string, string>();

foreach(string[] vaue in pharsed)

{

     dictionary.add(vaue[0], vaue[1]);

}

Thats it, what I liked was the ease to skip and split on the fly. I haven’t figured out, how to create Dictionary on the fly in that single statement. Once I find out how to do it, I will update it in here for my notes…

Have fun.

Powered by Qumana

September 2, 2008

Factory pattern through reflection.

Last week I came across a situation where, I need to implement bunch of classes which perform same task differently when it is called. (Simply put, factory pattern). So I thought, based on GOF it is a simple implementation.

Create a abstract factory class and then create sealed worker class which implement the actual task. Simple enough. In the main program have a switch which checks the incoming parameter and based on the parameter, we create instance of a proper worker class and vola we got the implementation done.

Now the point to remember here is, if we need to add a new worker class, what are all the elements involved? First you need to add a new case statement for the new worker class initation and then add the new worker class itself. Two code changes. Not a big deal. But if you happen to have too many implementation or in other words too many worker implementation then your switch statement could grow larger and larger.

This limiation forced me to think if there is any other way of creating classes on the fly. I talked to one my collegue and he pointed me to one his implementation he is currently using that is using reflection. So here is how it is implemented.

First create the interface you would like the factory workers to implement. Something like the following;

IWorker.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TraditionalFactory
{
public interface IWorker
{
void SayName(string name);
}
}

You can see all we are trying to implement is a ‘SayHello’ method. Following two are two worker implementation of the Factory Interface;

Worker1.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TraditionalFactory
{
public class Worker1:IWorker
{
#region IWorker Members

public void SayName(string name)
{
Console.WriteLine("Name from first worker is {0}", name);
}

#endregion
}
}

Worker2.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TraditionalFactory
{
public class Worker2:IWorker
{
#region IWorker Members

public void SayName(string name)
{
Console.WriteLine("Name from second worker {0}", name);
}

#endregion
}
}

Based on the interface and the worker implementation there is nothing new. The code that make this one work without big switch statement is in the main program.

Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace TraditionalFactory
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Which worker to run (Worker1 or Worker2?");
string workerName = Console.ReadLine();
Type workerType = Type.GetType("TraditionalFactory."+workerName);
IWorker worker = (IWorker)Activator.CreateInstance(workerType);
worker.SayName("data passed");
Console.ReadKey();
}
}
}

This factory model implementation compltly depends on the reflection feature of C#. This also depends on the the consumer to this program aware of which worker they need to consume by name or you need to create a dictionary mapping of the user input to worker class mapping of some sort. For this implementation I am assuming the consumer knows which worker to use.

The point of interests are, first creating the type of the class we are going to instantiate. In our class we are going to find the factory interface type of execuing assembly using ‘Type.GetType’ method call. Which looks at the executing assembly or clr to see if it finds matching type. If you look at the code closely, since we already know which worker that we are interested to consume based on ‘workerName’, we look for the namespace + workerName in the GetType. (This is where reflection come into get the type of the class we are trying to consume). Once the type identified, we call in the ‘Activator.CreateInstance’ method to create the instance of the worker class through the interface and then execute the public method from the interface. Thats it, we got ourself Factory pattern implemented.

The benefit of this implementation is that, when you want to add a new worker class, you simply add the worker class, compile it and deliver the exe. No more main program change. One code change and you are done.

The problems I see with the implementation is that, you need agree on how the consumers are going to consume the worker class and make appropriate mapping before hand or make the consure to call in with appropriate worker name explicitly (like the example below).

One for my notes….

August 2, 2008

Starting Windows App from Web application with parameters!

Filed under: Tips and Tricks — @ 2:51 am

There will be times you need to invoke windows app from a web application. Not only you need to call the windows application you may need to pass parameters to the windows application.

Here is how went about to solve the problem. I created the windows application as a click once application and publised it. One important part you have to remember is to enable the windows application to accept parameters. This option is under publish tab under project properties. While you are in publish tab, choose options button and check the check box pretaining to enabling the windows application to accept paramters.

To enable the windows application to get parameters from the web application you have to add the following code segment in the windows application.

if (ApplicationDeployment.IsNetworkDeployed)
{
string queryString = ApplicationDeployment.CurrentDeployment.ActivationUri.Query
}

It is important that we add initial ‘if’ statement to make sure we try to get the parameter only if the application is in network installed. If you are testing the appliation locally then this part of the code will not work. Also after you get the queryString, you need to split the key value pair to get your required information.

So the bottom line, develop the windows application with above mentioned code snippet in the contructor of the form. Also make application aware of the parameter coming in from web application in the publish options.

Now lets look at the web side. When you deploy the windows application, normally it get deployed with the following url like

http://servername/appname/setup.exe

now from web site to launch the application, you would call the following url

http://servername/appname/appname.application

and add the parameter with following string pattern

?key=value

Thats it, now when you run the web application, the link will launch the application from the publish folder and the when the application run, the windows application will get the parameters from the web application through activation uri.

** The activation url might return zero or null value if you try to test the code with localhost. You have to test with actual computer name in the web application link.

Ads by AdGenta.com

Tags: , , ,

Powered by Qumana

Newer Posts »

Powered by WordPress