Tuesday, 21 December 2010

Team Foundation Server 2010 (1)

I have been working as part of a team of six developers for just over a year now. This has been the first time in my career that I have been part of such a team and in that time we have created a lot of components. These are scattered across a range of applications - web apps, web services, databases, SSIS packages and standalone .exe files that we run from servers on a schedule.

We're at the stage now where controlling change amongst the team is difficult. We are using Visual Source Safe as our source code repository and it's not brilliant. I'm never 100% confident that when I try and build a new version of the application that I'm working on and publish it to our test environment that it will have all the most up to date code in it. So instead of moving forward with functionality I'm spending time bug fixing changes so that I can get a demo working for customers.

I'm also finding the whole build process very unsatisfactory. At the moment I have a Visual Studio 2008 solution with a dev project that we're all contributing to and a test project that contains just the files I need to run a particular app. Creating a build involves knowing which of the files have changed, copying them into the test project, remembering to change the namespaces and then publishing to the test server. It's a slow and irritating process.

I also have the attitude of team members to deal with. Communicating with people when you all sit in a space that's about 10m square should be simple but it's not. I'm not getting told when things change and only find out about changes when I try and publish a version of the app and it stops working because a datatype has changed in the database.

All this is stopping us progressing. Fortunately we have just acquired some MSDN developer licenses that allow us to install Visual Studio Pro 2010 and Team Foundation Server. I have been reading plenty of stuff on the web about how these development tools will help to resolve the problems that I'm facing and move the team forward. So I'm going to spend the next few months concentrating on developing the practices and procedures necessary to improve our efficiency and quality. I would also like to explore the opportunities that Agile programming will bring to the team and see how well it fits with what we do because I think we can learn from that too. The next set of blog posts will all record my findings as I carry out this piece of work.

Graduated!

Last week (Wednesday 15th December, 3pm to be precise) I attended my graduation ceremony at the Liverpool Philharmonic Hall and received my certificate from the vice chancellor. I am now officially a Master of Science! It was a great day and made a nice change to be in a room full of happy people all celebrating something.

Wednesday, 27 October 2010

ASP.NET ASCX controls and ViewState

I've written a scripting engine into the ASP.NET app that I am developing. It's pretty neat - the user can choose from a list of scripts to load and work through, the script is loaded into a panel and they can follow it through.

Identified a problem yesterday whereby the second and subsequent scripts that got loaded had a strange little error - the first server control in the script didn't work on its first event (i.e. click, selectedindexchanged). It would work the second time the control was used - but that's no good for the users.

So I set about trying to solve it. After about seven hours of reading, trying things out and getting really frustrated I did solve it thanks to a couple of articles on the web. I made a load of mistakes so hopefully someone will read this and avoid making the same ones.

First, I had the scripts declared globally, i.e. Dim scriptname As Global.namespace.scriptname. Wrong wrong wrong!

What you need to do is create a ViewState called LastLoadedControl and set this to be the physical path of the control. This will reside in memory and allow you to persist the control between postbacks of the parent form. Then create a Private Sub called LoadUserControl(). This should check to make sure that the LastLoadedControl isn't null, then load the control and attach it to the Panel / Placeholder that you're using to display the controls.

You need to call this LoadUserControl method in the Page_Load event every time. Also, in the method that you're using to drive the user interaction (i.e. the list of scripts to pick from) you need to declare the physical path of the script, set the LastLoadedControl ViewState to this value, then call the LoadUserControl method.

Check out the first example in this blog (it's in C# but easily converted) for a really good example: .

A couple of warnings. First, even though the example above solved my problem, it was causing an error whereby the aspx page was trying to load the same control into the Panel / Placeholder twice and falling over as a result. What you have to do in the LoadUserControl method is specifically set the name of the control otherwise the ViewState of the control (i.e. its values) won't carry across between postbacks. Obviously you can see a scenario where you're creating the same control twice. To get around this you have to remove the control from the Panel / Placeholder every time. The example says to use Panel.Controls.Clear() but this didn't work for me. What I did was check that the Panel had > 1 control, then did Panel.Controls.RemoveAt(1) to eradicte the control and then replace it.

Second warning - some people say to do the control loading in Page_Init, rather than Page_Load. Problem with that is the Page_Init can't reference any Panels / Placeholders etc. so it's a bit of a waste of time.

POSTSCRIPT: just tried loading something into Page_Init in the .ascx control rather than the Page_Load event to stop the value of a control changing every time and it worked. I wonder if it's because I had the Page_Init set to Private before, and now I have changed it to Protected it seems to work ok. My bad.

Tuesday, 26 October 2010

Cross - Domain Scripting and iFrames

I've been doing some work over the last couple of days to embed data from a third party API into the .NET web app front end that we're working on. The API isn't open to everyone - you have to access it via Javascript and it can only be accessed on the server that all the component parts run on. As API's go it's pretty useless when you want to hook into it from somewhere else.

So our javascript guru showed me how to make an app in an iframe talk to the parent app that's hosting the iframe and away I went. I built a .jsp page on the API server that delivers all the data I want and then modified the .aspx page to include some labels to display the data.

I ran into a couple of problems. The first concerns the iframe load time. I originally tried to write some javascript that would load the values from the divs on the .jsp page into server controls on the .aspx page but this failed because the iframe was always the last thing to load and all the server controls saw was NULL values.

The js guru suggested a kind of timer function that would check when the iframe had finished loading before running a script to get the values. But it turned out that the simplest solution was to make the .jsp page populate the values itself - you can make the content of the iframe control what gets written to the screen in the parent as much as you can do it the other (and more logical) way around. In this case the connotation of parent and child is wrong - they're more like siamese twins joined together and able to influence each others movements.

Another useful thing to know is that you can only make two apps on two different servers talk to each other if you suffix the server name in the URL with the name of the domain that they both share. So if my domain is www.markp3rry.com then you will need to set the servers to http://server1.markp3rry.com:8080/ and http://server2.markp3rry.com:8090/ (for example).

There's a heck of a lot of scuttlebut written on the web about this topic so tread carefully. A lot of the examples for extracting information from an iframe in both javascript and jquery script were making me tear my hair out. You have been warned!

Monday, 13 September 2010

The Pomodoro Technique

http://www.pomodorotechnique.com

"A way to get the most out of time management". Charlie Brooker mentioned this on the Guardian website (in amongst a rant against Google's crazy new instant search) and I have found it a very useful way of getting some work done. I've been struggling recently with my concentration levels; in a hot, noisy office where people are talking to you it can be difficult to focus on the task in hand - especially when said task is repetitive and / or dull.

Pomodoro basically says "get a stopwatch, work for 25 mins then grab a 5 minute break and repeat". That's not all it says and I urge you to read the website for more information. I think I remember reading somewhere else that the maximum concentration span of an undergraduate in a lecture is 25 minutes which makes the 50 minute lectures I used to attend 100% too long and this technique backs that up. It's been useful for me in breaking the day down into smaller slots with a beginning (and, more importantly, an end) and has helped me to work through what I have needed to do today. Maybe it will work for you too?

Wednesday, 8 September 2010

Creating a JSON - enabled .NET Web Service

Assuming that you're writing VB.NET. The blogging app won't let me put less than / greater than tags in so make sure you add these.


1. Create a new ASP.NET Web Service Application project in Visual Studio 2008 / 2010. Call it 'JSONWebService'.


2. Rename the default 'Service1.asmx' to 'json.asmx'. Also rename the code behind class from 'Service1' to 'json' and change the binding on the front .asmx page.


3. There should be a commented line in the template code that the project creates that looks like "system.web.script.services.scriptservice()_" . Uncomment this.


4. Add a new Public Class to your code behind. The example I am going to use will provide data on staff contact information (i.e. phone number, email address) but you can fit in whatever data you want. So I will do Public Class StaffInformation. Create four public string variables within the Class; Public forename As String, Public surname As String, Public phonenumber As String, Public email As String.


5. Add a new Web Method to the json Class. Call it GetEmployees so you'll need to do Public Function GetEmployees(ByVal id As String) As List(Of StaffInformation). Make sure you add the WebMethod() line of code above the Function declaration - you can copy / paste that from the HelloWorld method that the project template creates.


6. You'll also need to add this line inside tags below the WebMethod declaration: System.Web.Script.Services.ScriptMethod(UseHttpGet:=False, ResponseFormat:=Script.Services.ResponseFormat.Json)


7. Couple of important points here. The WebMethod is returning a List - that's pretty vital to the JSON output because from what I have read .NET struggles to convert more complicated objects such as DataTables. I had never used this before until I sorted this example out and it's a useful look into how OO works in VB.NET. The ScriptMethod in point 6. is also important because a) it tells the web service to return data in JSON notation, not XML and b) it forces the AJAX call to get made using HTTP POST and not GET. POST is essential when you write the AJAX call in jQuery.


8. The GetEmployees function is going to read some data from a database, put it into the List object and then return the List. Something clever then serialises the List into JSON.


9. Create SqlConnection, SqlCommand and SqlDataReader objects and set the first two up. In my example I did a simple SELECT statement against a table to return the four fields I'm looking for (as specified in the custom List).


10. Declare a new instance of the List (i.e. Dim MyList = New List(Of StaffInformation).


11. Execute the SqlDataReader. Loop through the dataset with it and do the following:


12. Dim item = New StaffInformation With {.field1 = reader(0).ToString(), .field2 = reader(1).ToString(), etc.}


13. MyList.Add(item)


14. Return MyList at the end of the Function.


15. You also need to modify the Web.Config file to allow AJAX HTTP calls. Inside the system.Web node add webServices, then protocols inside that, and then two new entries inside protocols: 'add name="HttpGet"' and 'add name="HttpPost"'.


16. That's it. Compile, publish to IIS and test with a jQuery AJAX call.

Friday, 3 September 2010

jQuery with ASP.NET Web Service

Had a very productive day today hooking up an HTML web page with the jQuery library to an ASP.NET web service. I've written previously about my research into this and while the WCF RESTful stuff looked good I wasn't convinced it was the best way forward to this. We've decided to use a sickness absence report as a trial for developing some skills in Javascript development and today I was looking for a better way to expose web service methods that jQuery could easily communicate with.

After several hours of searching and some dead ends I stumbled across this article http://www.dotnetcurry.com/ShowArticle.aspx?ID=320&AspxAutoDetectCookieSupport=1 that finally showed me how to do all the things I have been reading about. The standard ASP.NET web service gets a few additions to it (such as System.Web.Script.Services.ScriptService / ScriptMethod) and you can tell the individual ScriptMethod to return data as JSON. That's great because the numerous jQuery AJAX examples all deal with JSON instead of XML. The article also shows you how to return a List of data from VB.NET (something I had never done before - I have always used DataSet / DataTable) which allows you to create a custom List with a number of attributes and then add as many items to this List as you like. The list gets populated with data from the SQL result set.

You can then write what is now becoming a pretty standard jQuery AJAX function, get a result set object, loop through it with a FOR loop and write the output into an HTML table, which then gets displayed in a div on the page (that's another thing I love about jQuery; you can actually modify whole divs).

There are plenty of examples in C# on the web but this is the first complete working example I found in VB.NET. Check it out!