Thursday, October 22, 2009

Follow-Up: Make Web Service Configurable

Base upon the comments of a reader on my last weekly source code posting is that there is an additional 2x ways to set the URL location of a Web Service:

string newUrl = "http://server/path/to.asmx";
MyService service = new MyService();
service.Url = newUrl;

Or in new C# 3.0 you can use object initializers:

string newUrl = "http://server/path/to.asmx";
MyService service = new MyService {Url = newUrl};

I still prefer using a constructor.

Wednesday, October 21, 2009

Tuesday, October 20, 2009

Weekly Source Code: Make Web Service Configurable

Sorry for the delay, been crazy at work.

In Visual Studio.Net you call web services like the old style ASP.Net web service (not new WCF) by adding a reference to the project using Add Web Reference option. In the pop up dialog you point it to the web service URL, Visual Studio.Net will then generate the required C#/VB.Net stub classes.

But its not to say that the web service will be at the same URL location expecially if you move between development - > QA -> production environments. How do you make the web service URL configurable?

The web service stub class Reference.cs (for C#) or Reference.vb (for VB.Net) has a URL property, you can always set this property in your code but its a nice way to rather do it when you call the constructor e.g.:

MyService service = new MyService(webserviceUrl);

You do this by adding the following constructor to the MyService class:

MyService(String theUrl)
{
Url = theUrl;
}

The downside (before Visual Studio.Net 2008) is that if you update the web reference (after adding an additional web method to the web service etc) the code get regenerated meaning your code will break and you will have to add the constructor again.

Fortunately Visual Studio.Net 2008 introduce partial classes which allow splitting definition of a class across multiple files.

The Reference.cs / Reference.vb class also is a partial class so you can add an additional partial class with the same name and same namespace:

public partial class MyService
{
MyService(String theUrl)
{
Url = theUrl;
}
}

Now you can update the web service reference without worrying about such problems.

So what is partial classes? Yes they allow you to split the definition of a class across multiple source files but why? In this sample it show that its for code generation mainly since you normally generate code then add your own additions (like a constructor in this case) but if you re-generate your custom code is lost. Partial classes (and later on I will introduce partial methods) resolve this problem. Why did MS add it? Look at LINQ to SQL which also generate code, now you can add custom logic like validation with partial methods without ever worrying that re-generation will break your logic.

Monday, August 31, 2009

Back From Holiday

Back from 3 weeks holiday :-(
Ready to work...

Wednesday, July 22, 2009

Java Programmer Certification, Back to MWEB and Going On Holiday

Hi :-)

A lot happened the last couple of weeks, mainly just busy with work.
I'm studying for my Java Programmer Certification (SCJP) base on JDK 6, on 1 August 2009 I'm also going back to MWEB at N1 city to work has a Java/C# developer. Still employed by Soliditech.

From 9 August 2009 my beautiful wife and I are going on a 3 week holiday

So I'm off to chat after my holiday with more interesting topics.

Thursday, June 25, 2009

Weekly Source Code: LINQ for Java Part 2

I hope everybody had a nice week, even if the winter is upon us - freezing cold with rain etc. Time to get the blankets / heaters out.

Last week I introduced JoSQL, this week I continue to show a sample JoSQL query... we start off with a keep it simple (KISS) sample with some points to remember.

Sample Query:

The goal of this JoSQL query is to get a list of contracts that all have a given product:

List contracts = .... // here we get a list of contracts

Query q = new Query();
q.parse("select * from com.sales.Contract where productId = :p");
q.setVariable("p", 5);
QueryResults qr = q.execute(contracts);
List contractResult = qr.getResults ();
  1. All JoSQL classes are kept in the org.josql package, Eclipse IDE will import these nicely for you.
  2. We start by creating a Query object instance this is where our query starts.
  3. q.parse will check your query for any issues, an exception will occure if the query is incorrect. Note that we need to provide the full package location of our Contract class else JoSQL wouldn't find the class. This can cause a problem if you refactor your code, like move the Contract class to a different package location but don't update the JoSQL query.
  4. The following line we set the :p variable using setVariable method.
  5. We then execute the query and put the results into qr which is an instance of QueryResults.
  6. contractResult will contain the result.
JoSQL uses reflection so "productId" in the query is a getter method like "getProductId()" on the Contract class.

Friday, June 19, 2009

Weekly Source Code: LINQ for Java

Its been a long couple of weeks due to my current work load, working average 12 hours+ per day isn't fun. So I finally got some free time over a lunch break to continue with my weekly source code.

At work Java is our main programming language but its not so advance like C# (not to start a language war here!) one of them are LINQ but LINQ is also available in Java!


What Is LINQ?

Language Integrated Query (LINQ) was added to C# 3.0 / VB.Net 9.0 in MS Visual Studio.Net 2008. Its a query language part of the underline language for easy query of information like a database, online resources like a webservice or in our case collections (list, arrays, hashmaps etc). Different LINQ providers existing for collections, databases, Amazon, Facebook, Twitter, LDAP etc. Visit Wikipedia for more about LINQ.

Let's Meet: JoSQL

JoSQL is LINQ for Java that allow you to easily query collections (lists, arrays etc) using a SQL line syntax.

References

Disadvantages:

  • No compile time checking since the LINQ code will only be evaluated when query.parse() method get called at run-time.
  • You need to indicate the full package + class name in your query
  • If you wish to use "NOT IN" or "IN" in the WHERE clause then you need to override the toString() method of the class since it needs to know which field to use to do the comparison with.

I will write more JoSQL samples in the next weekly source code. Also my solution to the toString() problem.