Saturday, November 21, 2009

Programming in Scala – Part 2/?

In my previous post, we got started with simple Scala HelloWorld and moved on to write a bubble sort in Scala. This time let us look at some differences between a “var” and a “val” in scala. I hope you all know what “immutable” means – simply put Strings in Java/.NET are immutable. Anytime you modify a string, a new object of string is created – they cannot be changed in place. Well, in scala when you declare a variable with a “val” it would be immutable. Look at the following scala code.

object ValVar
{
def main(args: Array[String])
{
val immutableValue = 200
//immutableValue = 20 -> gives compiler error
var mutableValue = 200
mutableValue = 20
println("Immutable : "+immutableValue+"\n Mutable : "+mutableValue)
}
}


The decompiled program would look like shown in Java



import java.rmi.RemoteException;
import scala.Predef.;
import scala.ScalaObject;
import scala.ScalaObject.class;
import scala.StringBuilder;
import scala.runtime.BoxesRunTime;

public final class ValVar$
implements ScalaObject
{
public static final MODULE$;

static
{
new ();
}

public ValVar$()
{
MODULE$ = this;
}

public void main(String[] args) {
int immutableValue = 200;

int mutableValue = 200;
mutableValue = 20;
Predef..MODULE$.println(new StringBuilder().append("Immutable : ").
append(BoxesRunTime.boxToInteger(immutableValue)).
append("\n Mutable : ").
append(BoxesRunTime.boxToInteger(mutableValue)).toString());
}

public int $tag()
throws RemoteException
{
return ScalaObject.class.$tag(this);
}
}


Clearly, it does not look like "immutable" variables are declared final in the Java code. So it appears to me that Scala compiler does the job of making sure that the val'bles are immutable. For those curios to see what happens if you attempt to change a val'ble, see the screenshot below. Also it would be interesting to note that the scala compiler takes care of optimizing our string concatenation to make use of String builder, just like the javac!



image



So what did we learn?: If you wish to keep changing the values for a variable, then use "var" and if you want immutable variables, use "var".



Now that we know how to create both variables and val’bles, let us look at some fancy stuff that we could do with lists.



Whats your range?



Let us say, we need all odd numbers between 20 and 2000 which are divisible by both 5 and 7. If you were like me, we write the program to look like shown below.



object RangeAction1
{
def main(args: Array[String])
{
for(i <- 20 to 2000)
{
if( i % 5 == 0 && i % 7 == 0)
println(i)
}
}
}


Can we do any better? This looks too long now that I have been imagining things, increasing expectations about scala being so nice.



object RangeAction1
{
def main(args: Array[String])
{
(20 to 2000).filter(i=>i%5==0&&i%7==0).foreach(i=>println(i))
}
}


When we say “20 to 2000”, it returns a Range object. Look in the documentation to see what all magic could we do with range. Similarly if we were to work with lists, we could do something similar. Now to add 1 cent to the 3 cents we covered so far, what if i want the range to start with 20 and end with 2000 but increment by 10 and exclusive of 2000.




(20 until 2000 by 10).filter{i=> i % 5 == 0 & i % 7 == 0}.foreach{i=> println(i)}


Also, I wanted to be more like a regular programmer and put my closure inside {} instead of (). More fun later!

Friday, November 20, 2009

Scala for dummies like me!

Well! what should I be saying? I am that kind of person who keeps shifting from one interest to another. Once I am extremely interested in .NET (and I still am, but .NET is now an ocean – would take me too long to catch up with everything) and now I want to explore Scala – all new programming language on the JVM (new to me!) which has been receiving rave reviews. So, I went ahead and ordered Programming in Scala book on a1books.com (which by the way is a great site) but unfortunately I did not receive my copy yet. So here i am with all intention to do something with Scala but am out of resources (no offense but there are no simple tutorial on Scala which is interesting and which doesn’t make me fall asleep in 2 minutes).

Lets first get started and write a Hello World program.

class HelloWorld
{
def main(args: Array[String]){
println("Krishna Vangapandu - Hello Scala world!");
}
}




When you compile this and execute it, you would get a “java.lang.NoSuchMethodException: HelloWorld.main is not static”. Well, the mistake that I did was to put a “class” – but it should be object. So the hello world would be



object HelloWorld
{
def main(args: Array[String]){
println("Krishna Vangapandu - Hello Scala world!");
}
}


So what's the difference between "class" and an "object". Obviously there is no problem for the compiler, only the runtime blows! So what does the documentation say about this? Well even better lets use a decompiler to decompile the .class file we obtained and see how the scala code would when written in java. By the way, I am using this decompiler - which i should say is freaking awesome.


"class HelloWorld" decompiled.




import java.rmi.RemoteException;
import scala.Predef.;
import scala.ScalaObject;
import scala.ScalaObject.class;

public class HelloWorld
implements ScalaObject
{
public void main(String[] args)
{
Predef..MODULE$.println("Krishna Vangapandu - Hello Scala world!");
}

public int $tag()
throws RemoteException
{
return ScalaObject.class.$tag(this);
}
}


"object HelloWorld" decompiled.



import java.rmi.RemoteException;

public final class HelloWorld
{
public static final void main(String[] paramArrayOfString)
{
HelloWorld..MODULE$.main(paramArrayOfString);
}

public static final int $tag()
throws RemoteException
{
return HelloWorld..MODULE$.$tag();
}
}


But what the hell is a $tag() method? Well, I looked into the source code which had a comment on the $tag method which says



This method is needed for optimizing pattern matching expressions which match on constructors of case classes.  




Well, then what is the HelloWorld actually doing? Well, looks to me it was using the HelloWorld$ which was also generated by “scalac”. I cannot dig into what is  going on here, may be sometime in the future.



So far, what I understood is that “object” creates a final class whereas “class” creates a ScalaObject and methods inside would all be instance methods. So anything declared “object” can act only as a static-only container.



Lets do a simple bubble sort program in Scala. What should we be knowing to write a button sort?




  1. Assuming we pass the numbers to sort from command line, how do we convert strings to numbers ?


  2. How do we loop the array?



object BubbleSort
{
def main(ip_args: Array[String]) //we shall get the input numbers to sort into "args"
{
/*
we have a collection of strings, we should get a collection of numbers.
so we use the map which says for each i in the ip_args,
return the value after converting into expression. we get a 1 to 1 returned array.
*/
val args = ip_args.map { i => Integer.parseInt(i)}

/*
Looping : for index j which starts from 0 and ends at args.length-1 (inclusive)
*/
for(j <- 0 to args.length-1)
{
for(i <- 0 to args.length-2-j)
{
if(args(i) > args (i+1))
{
//we do an ascending order sort.
// Swap routine is shown belo.
val temp = args(i) //this is how we define variables in scala
args(i) = args(i+1)
args(i+1) = temp
}
}
}
//print all the numbers
for(i <- 0 to args.length-1)
println(args(i))
}
}


I do agree that even without the comments the code does not look as concise as it should be. But right now, we are just getting started – we would slowly look at how we can write concise code when we think functional programming (I am saying this with my past experience with Groovy and C# Closures – by functional, 90% of the time I mean closures – which I know is not accurate).I have also observed that when you compile the BubbleSort.scala, you end up getting more than one .class files - which I believe is for the anonymous method (closure) we used.



That's it! for this post. See you soon with functional programming using Scala!

Thursday, November 12, 2009

Working with JQuery and list boxes

Code (read comments):

<html> 
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
</script>
<script type="text/javascript">
/*
when the document is ready (after all the HTML page is loaded, this shall be executed)
we attach the event handlers.
*/
$(document).ready(function(){ attachEventHandlers(); });
function attachEventHandlers(){
/*
In this method we attach the function(){} to the change event on all the <select> items.
The code inside the function(){} shall be executed when item selections are changed on the select box.
*/
$("select").change(function(){
//using "this" to access the current listbox. the jQuery wrapper would be $(this)
var selectedItems = $(":selected",this);
$("#itemsCount").text(selectedItems.length); //set the items selected count.
var toAppendHtml = ""; //lets store the html that we shall put inside the itemsSelected element.
//for each selected item we add a new line with selected item's text and value to the toAppendHtml.
selectedItems.each(function(){
toAppendHtml += $(this).text() +" : "+$(this).val()+"<br/>";
});
//finally put everything in there as html content to itemsSelected.
$("#itemsSelected").html(toAppendHtml);
});
/*
Alternatively you can use ExternalFunction. The external function should have one parameter "e" called the event object.
$("select").click(ExternalFunction);
*/
}
/*
function ExternalFunction(e)
{
//now within this function, the element which raised the event can be accessed using "this", or "e.currentTarget".
//So the statement "this == e.currentTarget" will always be true.
alert($(":selected",this).length);
}
*/
</script>
<style type="text/css">
select{
width: 100px;
height: 200px;
}
</style>
</head>
<body>
<select name="items" id="items" multiple="true">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
<option value="4">Item 4</option>
<option value="5">Item 5</option>
<option value="6">Item 6</option>
<option value="7">Item 7</option>
</select>
<p/>
<div>total items selected : <span id="itemsCount">0</span></div>
<span>Selected Items are </span>
<div id="itemsSelected"/>
</body>
</html>

Thursday, November 05, 2009

Problem running Scala

I was just trying to run scala on my machine and failed to do so with an error message “…..\java.exe unexpected at this time.” Look at the screenshot shown below.

image

Well, the problem was that environment variables for JAVA was set up using JAVA_HOME variable. The JAVA_HOME environment variable was pointing to the JDK directory and the Path was modified to include the “%JAVA_HOME%\bin” directory.

I removed the JAVA_HOME and then modified the path to specify the complete path to the JDK bin folder and it works now. :)

I might be talking more about Scala in the future, the concurrency scala supports appears to be interesting.

Monday, August 31, 2009

Visual Studio Test System : “Test Run Deployment Issue : The location of the file or directory … is not trusted”

image

I just came across this issue when trying to run tests from Visual Studio test system. To resolve this, simple go to the source of the DLL (in my case, I placed them under LIB directory inside my solution directory), right click on each of the libraries that were taken from external sources (like Log4Net, Moq, etc.) and view the Properties. In the properties window, you should be seeing “Unblock” option as shown below.

image

Simply click “Unblock” button and release the library’s security constraint. Now perform a clean on the solution and rebuild the solution. Your tests should run without any deployment issues.

good luck!

Monday, July 27, 2009

ConfigStation for WCF – prototype for minimal configuration based WCF services

To start off, I would like to stress that I am not a WCF expert and if you go around my blog, you can notice me writing about lot of different things – WPF, DLR, Web Development and what not. So what I present is just something that I made recently as a part of a bigger project that I plan to release. Apparently, this is pretty good start for what I envision for avoiding Configuration Hell in WCF services.

The StockTrader sample from Microsoft comes with a great library – Configuration Service 2.04. The library is pretty good and provides wonderful functionality but the major problem with that, for me, is its strong dependency on the SQL Server backend. In short, the configuration service maintains a service configuration repository which is used to provide centralized configuration repository, load balancing, fail-over in WCF based SOA applications. And I always wanted such a repository which would minimize my effort in developing distributed applications using WCF.

I believe, WCF should allow very simple way to develop services and should provide an easier means to configure them. One way away from configuration through App.Config is to code the configuration, but there seems to be a big lack of proper documentation and decent-real-world samples explaining code-based WCF configuration. Anyway, I would envision hosting a service to be as simple as :

var serviceHost = new AutoConfiguredServiceHost<ServiceImpl>();

With no or minimal configuration, the service host should be clever enough to determine what the configuration defaults are. Similarly, consuming the service should be as simple as :

var client = new RemoteServiceProxy<IService>();

The proxy should be created by accessing the repository to figure out implementation for IService and then use the configuration obtained to create the proxy.

With these goals in mind, the config station has been developed and here is what I have so far.

Sample : Test Service which hosts the ConfigRepository as well as a sample WCF Service implementation. The host method is shown below.

 class Program
{
static void Main(string[] args)
{
using (var configHost = new ConfigStationHost())
{
var hostFacade = new ServiceHostFacade<TestImpl>();
var host = hostFacade.Host;
host.Open();
Console.WriteLine("Test service launched.Enter to Stop");
Console.ReadLine();
//host.Close();
host.Abort();
}
}
}


If you look at the using block, I am creating an instance of ConfigStationHost – which actually hosts the ConfigStation – a repository WCF service. At the moment, this service requires App.Config based configuration of the service – which can be easily removed and which would be my next enhancement to the project. In this example, I am actually hosting the ConfigStation within the same process as my actual WCF service, which is not required at all. You can host the ConfigStation in a totally separate program – all you have to do is create the instance of ConfigStationHost (see required configuration below) and dispose it when you are done.



The configuration for the Test Service is shown below.



 
<configuration>

<appsettings>
<add value="net.tcp" key="ServiceScheme" /> <!-- you can set this to http as well or even msmq ...-->
<add value="9989" key="ServicePort" />
</appsettings>

<system.servicemodel>
<services>
<service name="ConfigStation.Repository">
<endpoint contract="ConfigStation.ServiceContracts.IRepository" binding="wsHttpBinding" address="http://localhost:8731/ConfigStation/Repository" />
</service>
</services>

<!-- This demo acts as a client to ConfigStation, so it is all good-->
<client>
<endpoint name="ConfigStation" contract="ConfigStation.ServiceContracts.IRepository" binding="wsHttpBinding" address="http://localhost:8731/ConfigStation/Repository">
</endpoint>
</client>

</system.servicemodel>
</configuration>


In the shown configuration, the <service> element configuration is used to host the ConfigStation repository in the current process. The <client> configuration is  the WCF client configuration which is used to access the ConfigStation service hosted. The TestService makes interacts with the ConfigStation using WCF and the ConfigStation is treated as a WCF service hosted somewhere remote. So, if we were to host the configstation separately the only configuration required would be that of the <service> defined. The TestService would then have the AppSettings and the <client> configuration – which is pretty easy to set and even easier for me to remove.



Now, the ServiceScheme dictates what communication protocol (BINDING, in terms of WCF) would be used when exposing the service and what binding would be used by the clients consuming this service. The ServicePort tells what port the service should be hosted on. Note that WCF allows hosting multiple services on the same port as long as their address is different (Except for MSMQ, i think).



The test client to consume the TestService is a different program, whose configuration is shown below. The program contains a WCF client to the TestImpl service whose details are obtained from the ConfigStation. Thus, the client process requires configuration which points to the ConfigStation.



<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:8731/ConfigStation/Repository" name="ConfigStation"
binding="wsHttpBinding"
contract="ConfigStation.ServiceContracts.IRepository">
</endpoint>
</client>
</system.serviceModel>
</configuration>


As you can see, the above is the only configuration required – which would be eliminated once I enhance the ConfigStation. The actual code to access the client is shown below.



namespace Test.Client
{
class Program
{
static void Main(string[] args)
{
var cf = new ClientProxyFacade<ITest>();
ITest test = cf.Interface;
var td = test.SayHello();
Console.WriteLine("Remote Server returned : " + td.Message);
}
}
}


You just create the ClientProxyFacade Of ITest, the service contract used by TestImpl. Then the interface is obtained via the “Interface” property. Then you can execute any method exposed by the Service Contract.



The library is available on codeplex – making this my first public release of open source software, of any kind. In the process, I would like to stress that the library uses the amazing ServiceModelEx library from Juval Lowy, IDesign. I actually tried to contact Juval whether or not to use his library but guess he is too busy so I took the liberty to publish the project having seen a WCF project on google code doing the same. In case I breach any license, please go easy on me and let me know so that I can fix my mistake.



I appreciate any positive feedback and any expert advice on the library. I am glad to learn and fix any changes requested. :) Hope this helps a few of us devs who like to play with some convention based WCF programming. I would be talking more details on the actual implementation, on how the library performs auto-generation of the service configuration and how bad the current repository implementation is, in my next post.

Wednesday, July 22, 2009

Powershell Script to delete bin/obj folders

Shown below is a simple powershell script that can be used to clean up a solution folder. The script excludes the "Libraries" directory and deletes any folder named bin or obj in the specified location.


Get-childitem c:\Temp\MyProjectSolutionFolder -recurse -include *.exe,*.dll,*.pdb,*.exe.config, bin,obj | where {$_ -notmatch 'Libraries'} | Remove-Item -Recurse


This script saves my time a lot and also does not require me to install any tools that does the same but adds registry entries for explorer context menu.

NOTE: Please verify thoroughly before blindly running the script. Use the -WhatIf flag to simulate the execution of the command instead of actually running it.

Saturday, June 20, 2009

Bing : Not a search engine, but decision engine??

I am an active contributor to the MSDN WPF Forums and most of the times I go to the forums through the search engine. And being an ardent supporter of Microsoft, I made Bing my default search engine on my home desktop. So I searched for “MSDN Social” and look what one of the sponsored result is!

image

seriously we are in the 21st century. And BING “decided” for me that I want to visit BAD GIRLS IN MY AREA! when all I wanted to do was to visit MSDN Social Forums. Bing – get a life.

Friday, June 05, 2009

GridViewColumn CellTemplate does not work?

Well, today I came across this interesting question on WPF regarding setting the DataTemplate of a GridViewColumn not working properly. Everything looks fine in the code (assuming his DataTemplate generating method was fine) which got me interested to figure out what the issue is.

And then I made up this sample application whose XAML is shown below.

<Window x:Class="Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<StackPanel>
<ListView Name="lv" ItemsSource="{Binding Items}">
<ListView.View>
<GridView></GridView>
</ListView.View>
</ListView>
</StackPanel>
</Window>


The code-behind is simple. It sets the DataContext to itself and then fetches sample data using GetData() call. Then the GridView for the ListView is obtained and a new gridviewcolumn is added programatically. The CellTemplate is set on the GridViewColumn using SampleTemplate() method. The SampleTemplate() method constructs a DataTemplate programatically using the FrameworkElementFactory class and returns the same.





 
Private Sub Window_Loaded(ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs)
Me.DataContext = Me 'set the datacontext
Items = GetData()
Dim gv As GridView = CType(lv.View, GridView)
Dim gvc As New GridViewColumn
gvc.Header = "Test"
'Display member binding
gvc.DisplayMemberBinding = New Binding(".")
gvc.CellTemplate = SampleTemplate()
gv.Columns.Add(gvc)
End Sub

Private Function SampleTemplate() As DataTemplate
Dim dt As New DataTemplate
dt.DataType = GetType(String)
Dim fef As New FrameworkElementFactory(GetType(TextBox))
Dim bd As New Binding(".")
fef.SetBinding(TextBox.TextProperty, bd)
dt.VisualTree = fef
Return dt
End Function

Public Property Items() As IEnumerable(Of String)
Get
Return GetValue(ItemsProperty)
End Get

Set(ByVal value As IEnumerable(Of String))
SetValue(ItemsProperty, value)
End Set
End Property

Public Shared ReadOnly ItemsProperty As DependencyProperty = _
DependencyProperty.Register("Items", _
GetType(IEnumerable(Of String)), GetType(Window1), _
New FrameworkPropertyMetadata(Nothing))

Function GetData() As IEnumerable(Of String)
Dim x As New ObservableCollection(Of String)
x.Add("Item 1")
x.Add("Item 2")
x.Add("Item 3")
Return x
End Function




And when I run the application all I see is …



image



Ok the binding works and I see my data but somehow the celltemplate is not being reflected. So, I was able to reproduce the issue the question presented.



So I dig around and looked in the documentation for CellTemplate where I found the following.



image



So it basically means that if DisplayMemberBinding is used, then CellTemplate would be ignored and so on with the CellTemplateSelector. So I comment the line that sets DisplayMemberBinding and run again.



image







Yes! its working as expected.



So whats the moral of the story? Actually there are three morals:



1. First of all, when using CellTemplate, do not set DisplayMemberBinding.

2. Refer to the documentation, especially the remarks and code samples section for a method or property. MSDN folks did an amazing job (ofcourse the WPF guys as well).


3. MSDN WPF Forums is amazing and overall Microsoft.NET folks rocks big time.

Thursday, June 04, 2009

What’s wrong with this code?

image

The breakpoint at line 18 never hits.

This is a really silly bug :D.

Leave comments if you know what is wrong.

.NET Funda : MemoryStream ToArray() vs. GetBuffer()

Look at the following program.

 class Program
{
static void Main(string[] args)
{
MemoryStream ms = new MemoryStream();
//write 10 bytes to the memory stream.
for (byte i = 65; i < 75; i++)
ms.WriteByte(i);

System.Console.WriteLine("MemoryStream.GetBuffer() : {0}",
ms.GetBuffer().Length);
System.Console.WriteLine("MemoryStream.ToArray() : {0}",
ms.ToArray().Length);

System.Console.WriteLine("GetBuffer() BytesToString : {0}",
FromBytesToString(ms.GetBuffer()));
System.Console.WriteLine("ToArray() BytesToString : {0}",
FromBytesToString(ms.ToArray()));

ms.Close();
System.Console.WriteLine("GetBuffer() BytesToString : {0}",
FromBytesToString(ms.GetBuffer()));
System.Console.WriteLine("ToArray() BytesToString {0}",
FromBytesToString(ms.ToArray()));
}

public static string FromBytesToString(byte[] b)
{
return ASCIIEncoding.Default.GetString(b);
}
}


In the above code, we use a MemoryStream and write 10 bytes (from A(65) to J(74)).  Running the program gives the following result.



image



The first two print statements displays the length of the byte[] array returned by both GetBuffer() and ToArray(). Now, both these methods returns the byte[] written to the memory stream but the difference is that GetBuffer() returns the allocated byte buffer used by the memory stream and this also includes the unused bytes where as ToArray() returns only the used bytes from the buffer.



Since a memory stream is initialized with a byte array of size 256, even though only 10 bytes have been written to the stream, the buffer returned is of size 256 (of which only 10 bytes are used). Printing the Bytes to string, thus displays a long series of empty lines where as ToArray() returns only 10 bytes that were used and displays the string exactly of size 10.



Note that both these methods work even when the stream is closed (demonstrated by the last two print lines in the code above).



The usage becomes important especially when you are trying to perform in-memory deserialization of some object from the memory stream for reasons like the binary data would be totally different from what was written actually, when using  GetBuffer().