Wednesday, December 15, 2010

Using XML literals in VB.Net with Linq

The XML literal support is the second thing I have seen special in Visual Basic which is really amazing.The first feature was the support for multiple indexer properties .XML literals helps us to create XElement by just writing plain XML in the code.This simple definition will not help anybody to learn what is XMl literals and their importance in dealing with xml.So let’s consider an example of XML serialization.
I have a class say Employee with 2 properties Name as string and ID as integer.The requirement is to get a XML representation of the employee collection.

Public Class Employee
Public Property Name As String
Public Property Id As Integer
End Class

If I pass a list of employee class I need out put like.

<Employee Name="Joy" Id="Joy" />
<Employee Name="George" Id="George" />

Here is the traditional code.

Private Function GetXMLTraditional(ByVal employees As List(Of Employee)) As String
Dim xEle As New XElement("Employees")
For Each emp In employees
Dim xEleEmp As New XElement("Employee")
xEleEmp.SetAttributeValue("Name", emp.Name)
xEleEmp.SetAttributeValue("Id", emp.Id)
Return xEle.ToString()
End Function

Look at the code which uses XML literals.

Private Function GetXMLUsingXMLLiterals(ByVal employees As List(Of Employee)) As String
Return <Employees>
<%= From emp In employees _
Select <Employee Name=<%= emp.Name %> Id=<%= emp.Name %>/>
End Function

I don’t think I need to explain anything further about the advantages.If you are a ASP.Net developer you might already understood the use of “<%= %>”

Friday, December 10, 2010

Providing Solution v/s creating Architecture

You may feel that this is something similar to one of the post “Technology v/s concepts and architecture” in my WPF blog.This is just a story from which you can understand what I would like to convey by the post Solution v/s architecture.

Last week when I was almost alone in office ,one of the Infrastructure & network support guy called me and asked “Can you write a program for me?” Yes buddy of course that is why I am here.He explained me his need.He need to restart 4 windows services after installing one particular software in a considerable number of machines.If I give a program he can just run that to restart services in one machine immediately after he installs and he can go to another machine for installation.

Then the .Net architect in me stood up and said .I will write a C# program which is configurable to restart any number of services.He said “OK”.My next question was “Which format you prefer for the configuration? xml or csv?”.What?? he continued “I need a program to restart 4 services.Let me know when you finish”.Oh poor guy he don’t know what he wants.But it’s my duty to give him highly sophisticated program.

Fortunately or unfortunately I had to attend one production support issue.Since it was complex our lead architect had to involve in the issue on the day.It was a out of memory exception which was not reproducible in our dev machines which have 4GB RAM.So it was decided like client needs to upgrade their machine as the data pulled is very large.But our lead architect started tackling in another way.He started asking some basic questions such as what is the chunk size we used in WCF services,is it really OOME or exception raised due to LOH fragmentation etc… which gave a new perspective to the issue.After 15mins the diagnostics session ended and I came to the previous problem.

May be due to the new light I got from my lead,I started thinking about the requirement from the network guy? Do I really need to write C# program to restart 4 windows services?It needs a .Net runtime. If I target to .Net 4.0,can I assure him that it will work in all the machines?Now the answer comes ’no’.I should think about some thing simple.

When .Net seems over engineered the option normally goes to scripting.Yes that is enough.Then which scripting? .bat files or vb script which will read the config to restart services?Oh.Again I am complicating.There is one more scripting technique which is nothing but WindowsPowerShell.Seems I got an alternative solution.The next phase is to discuss with the client about the alternatives.I called him and asked one thing.”Are you going to use this tool in machines which have Windows power shell?” Yes he replied.OS is either Vista or Win7.

Yes.This is the solution.Create a RestartService.ps1 file and write the below 4 lines.Ask him to change the service name using notepad and run using Windows Powershell.

Restart-Service BthServ
Restart-Service BthServ
Restart-Service BthServ
Restart-Service BthServ

Architect for solution.Don’t solve for architecture.

Seems I taught somebody to do my job which may be a threat in future:)

Monday, December 6, 2010

Calling explicitly implemented function using object of concrete class

The question : Are you able to call explicitly implemented method of an interface using object of concrete class?

This was one of the topic for discussion in our office, last week.Look at the below code.

public interface I1
public void Foo();
public class C1 : I1
#region I1 Members
void I1.Foo()
Console.WriteLine("In Foo method");

If we have a structure like above can we call the method Foo as follows?

C1 c = new C1(); c.Foo();

In the initial look we may think what is wrong in it? But this is not possible.Most of us were really new to this particular scenario because we are all working in VB.Net for last one year.There were so many comments such as bug in .net,.net is not good etc…But after a while we got the solution from another question.What was that question?

In the above example add one more interface say I2 with same method Foo() and implement the same in class C1.Now if we try to call method Foo() by using variable of class C1 obviously there will be an ambiguity and the solution to avoid that state is the limitation of this call.

Everybody became happy like ending of an old Indian movie.

In VB.Net this is avoided by using the implements function Foo1() in class can implement an interface function Foo() and hence the concrete class variable can call the interface method as Foo1().See the code snippet below.

Public Interface I1
Sub Foo()
End Interface
Public Interface I2
Sub Foo()
End Interface
Public Class C1
Implements I1, I2
Public Sub Foo() Implements I1.Foo
End Sub
Public Sub Foo1() Implements I2.Foo
End Sub
End Class

Happy coding...