November 2006 - Posts

Practical .NET 2 and C#2

Some pretty good .NET v2.0 sample chapters from Practical .NET 2 and C#2 by Patrick Smacchia.

http://www.practicaldot.net/en2/Main.htm

Chapter 4: CLR
Chapter 5: Processes, threads and synchronization
Chapter 6: Security
Chapter 14: Unsafe code, exceptions, anonymous methods, iterators
Appendix B and C: .NET 2 enhancements and Introduction to design patterns

If you ever want to find some good information about a subject just add sample chapters onto the search. As you search different subjects you generally end up with a full book made up of different chapters from different books.

Posted by dsmyth
Filed under:

iTextSharp - .NET PDF Generator

iTextSharp is an open source PDF generator for .NET. Although I haven't looked at it in great detail but it looks like, as well as a PDF Writer, it also comes with HTML and RTF Writers. There are plenty of examples on how to use it.

Also check out NFop, an open source XSL-FO parser.

Posted by dsmyth | 1 comment(s)
Filed under:

Visual Studio 2005 Web Deployment Projects

"Visual Studio 2005 Web Deployment Projects provide additional functionality to build and deploy Web sites and Web applications in ASP.NET 2.0 and Visual Studio 2005. This add-in includes a tool to merge the assemblies created during ASP.NET 2.0 pre-compilation, and provides a comprehensive UI within Visual Studio 2005 to manage build configurations, merging, and using pre-build and post-build tasks with MSBuild."

Read more and download from here

Basically this is an addin for VS2005 that lets you compile ASP.NET v2.0 applications using MSBuild. You can compile the whole site to one assembly for easy deployment, or you can compile each folder to it's own assembly, and so on. You can specify sections of the web.config file that should change from development to release, excellent for connectionString sections where you use a local database for development and another database when deployed on your ISP. There are more options too so it's worth reading the white papers.

Another article on compilation of ASP.NET v2.0 applications can be found here. It also covers the options available.

Posted by dsmyth
Filed under:

ASP.NET v2.0 Compiler Front End

Here is a nice front end to the ASP.NET compiler.

West Wind ASP.NET 2.0 Compiler Utility

Posted by dsmyth
Filed under:

Article on using Threads in C#

Here's an absolute blinder of an article on threading in C#

Threads In C#

Posted by dsmyth | 1 comment(s)
Filed under:

Visual Studio 2005 Tools for Office SE

"The final version of Visual Studio 2005 Tools for the 2007 Office System (VSTO 2005 SE) is now available for download." 

You can use the tool to develop managed addins for the five Office 2003 applications and the six Office 2007 applications.

For more information look here.

To download look here.

Posted by dsmyth
Filed under:

DotNetSlackers article...

I was informed over the weekend there by Sonu that my article on Hashing, MACs and Digital Signatures has went live. You may have seen it already in the articles section of DotNetSlackers. The article gives a bit of background to what these techniques are and why you use them and there is sample code. It presumes you have some basic knowledge of encryption.

Have a look, hope it's helpful to you. Read the article here.

Posted by dsmyth
Filed under:

Programmatically Modify Site-Map Nodes in Memory

I usually go on holiday over the festive season and whenever I do people always ask me to put the pictures I take online. So to keep them all happy I've been adapting the personnal starter kit's photo album and have stumbled on a really annoying problem with the sitemap feature. The sitemap xml file doesn't allow duplicate urls.

The reason this is annoying is crystal clear in my mind but might be a bit long winded to explain...

Picture the following breadcrumb and page navigation

Albums -> Place -> Photos 

albums.aspx -> place.aspx?place=1 -> photos.aspx?album=8

The reason this is annoying is crystal clear in my mind but might be a bit long winded to explain...

Picture the following breadcrumb and page navigation

Albums -> Place -> Photos 

albums.aspx -> place.aspx?place=1 -> photos.aspx?album=8

The reason this is annoying is crystal clear in my mind but might be a bit long winded to explain...

Picture the following breadcrumb and page navigation

Albums -> Place -> Photos 

albums.aspx -> place.aspx?place=1 -> photos.aspx?album=8

This is no problem as the navigation is linear like the breadcrumb control. Each url is unique and can be put into a site map xml file as long as the urls are resolved with the supplied querystrings*. However, what if you want to change 'Place' in the breadcrumb with the name of an actual place.

Albums -> US -> Photos 

albums.aspx -> us.aspx -> photos.aspx?album=8

Albums -> UK -> Photos 

albums.aspx -> uk.aspx -> photos.aspx?album=9

You can replace page.aspx with custom urls easily with Url Rewriting but, and here it is, doing this essentially creates a fork in the navigation, albums to uk or us back to photos. The fork means you can not represent this navigation in a sitemap because you need a sitemapnode for the 'US' and one for the 'UK' and both of these would need a photo sitemapnode child, and that means there would be two photos.aspx urls.

I tried all sorts of approaches to try getting around this problem and then I came across this article.....

How to: Programmatically Modify Site-Map Nodes in Memory 

.... although the article covers the ins and outs of solving this problem it doesn't tell you the best way to do it. The article recommends that you handle the SiteMapResolveEventHandler in the page but this only works if the page is the last item in the breadcrumb, i.e. the breadcrumb will change to 'UK' on uk.aspx however that breadcrumb node will change to 'Page' again when photos.aspx is visited.

In order to get around this you need to apply the breadcrumb change throughout the whole site and that means handling SiteMapResolveEventHandler in the Global.asax and mapping the event handler in the Application_Start event.

In here you need check the current url and decide what sitemapnode to change and what title and url to change it too. This gets rather long winded as a change on Place.aspx involves working with the current site map node while a change on Photos.aspx involves changing the current site maps nodes parent.

I'm still trying to get my head around all this but there appears to be quite a bit more to site map navigation than I had originally thought, and it's all because sitemap files don't handle duplicate urls!!

*resolving a url allows you to keep the sitemapnode's url containing it's querystrings in the breadcrumb control. For example your sitemapnode may have a url of search.aspx however the actual url used would be search.aspx?value=1. If you didn't resolve your url in this case then the breadcrumb would point to the search.aspx file, as that how it appears in the sitemap file, and would disregard the querystring.

Posted by dsmyth
Filed under:

A refactoring technique...

I'm not sure if I have done this before but I've just read about it and it's worth remembering.

Scenario: A class has a group of similar methods that vary by an enumeration or common value.

public IList LookupRetiredEmployees(int minAge) {
 IList retVal = new ArrayList();
 foreach (Employee emp in masterEmployeeCollection) {
 if ((emp.Age >= minAge) && (emp.Status == EmpStatus.Retired))
  retVal.Add(emp);
 }
  return retVal;
 }
}

public IList LookupActiveEmployees(int minAge) {
 IList retVal = new ArrayList();
 foreach (Employee emp in masterEmployeeCollection) {
 if ((emp.Age >= minAge) && (emp.Status == EmpStatus.Active))
  retVal.Add(emp);
 }
  return retVal;
 }
}

Reduce the methods to a single method that takes the enumeration or common value as a parameter.

public IList LookUpEmployees(int minAge, EmpStatus status) {
 IList retVal = new ArrayList();
 foreach (Employee emp in masterEmployeeCollection) {
 if ((emp.Age >= minAge) && (emp.Status ==status))
  retVal.Add(emp);
 }
  return retVal;
 }
}

Posted by dsmyth
Filed under:

QueryString Properties

Over the past couple of weeks I've been working on a .NET v1.1 web application. Most of the pages of the application were being passed query strings, which where used throughout methods of the page by calling code similar to this....

int id;
if (Request.QueryString["id"] == null)
{
id = int.Parse(Request.QueryString["id"]);
}

The above code was being used in a number of methods throughout the page, everytime 'id' was needed the code was used, which resulted in quite a lot of code duplication and bloat. I've not worked on many web applications so I'm not sure how many developers do the same thing. If your are doing this then maybe creating a read only property for the query string might be better. For example:

public int EmployeeID
{
     get
     {
           if (Request.QueryString["employeeId"] != null)
           {
                    return int.Parse(Request.QueryString["employeeId"]);
           }
          else
          {
                   return 0;
          }
    }
}

The methods that need the 'employeeId' query string can be reduced to 'this.EmployeeID', for example:

getEmployee(this.EmployeeId);

This approach has a load of advantages, you could decode/decrypt/validate/format the query string in the property, it saves a little bit of memory, and it makes code much more readable.

*NOTE: you might need to use the fully quantifed HttpContext.Current.Request.QueryString.

Posted by dsmyth
Filed under: