Robert Wray: January 2008 Archives

Getting the values from a Datatable column

|

I couldn't find any better, reusable way to get all the values from a specific column of a datatable, strongly typed than the snippet below:

        internal class DataColumnConverter<T>
        {
            internal T[] GetValuesAsArray(DataTable data, string columnName)
            {
                List<T> values = new List<T>();
                foreach (DataRow r in data.Rows)
                {
                    values.Add((T)r[columnName]);
                }
                return values.ToArray();
            }
        }

Things it's missing:

  • Type Cast checking on the type of items in the DataTables' column that's specified
  • Checking that the column specified exists
  • Handling of a column which contains nullable values (Could <T> be passed as, for example int? to cater for this? maybe! Or perhaps null's could be stripped out...)

ASP.net: Viewstate

|
ASP.net is much maligned, but if carefully managed, it can be a very useful part of a web developers toolbox for ensuring performance and UX in a web app. A couple of *very* useful links to posts about viewstate below:

- Truly Understanding Viewstate. Quite a long posting, with a lot of comments attached which also have some value. Well, well worth a read.
- Thoughts on the ASP.NET ViewState. More interesting content with a couple of useful/insightful comments.
- Master Page and PreInit. Not exactly entirely related to Viewstate, but it mentions a "trick" for getting access to controls on a page that has a Master Page associated, in the PreInit stage of the page lifecycle, something that otherwise blows a raspberry at the developer.

Visual Studio: The annoyance of Web References

|
Web References in Visual Studio are annoying. Yes, they take the pain out of binding to a web service, providing generated code that gives you type-safety. But, and this is a big but, the classes that Visual Studio generates are marked as public rather than internal. If you're writing a library that wraps your web service, this provides any caller of the library with direct access to the web service, not so bad I hear you say as anyone who knows the Url can add it as a Web Reference and code directly against it. The big annoyance is that it makes your libraries interface messy and exposes what's essentially an implementation detail. Imagine for a moment that you have an application where the layers are deployed to separate machines, and also geographically dispersed, using a model similar to the one below.
WebDiagram.png
In this model you'd want to hide the web service as it may not ever be used (and really is an implementation detail!), the lack of an option to define the visibility of the Web Reference makes this impossible without hand editing the generated code.

Related Links
- The bug/feature request logged on MS Connect site. (Marked Closed/WontFix, grr!)
- Programatically adding Web References, I've not looked in too much detail, but maybe it's possible to use this to add an internal web reference?

You have to Start to Shutdown?

|
I can't find the blog entry, but somewhere over at The Old New Thing (book | bio1) I remember it being mentioned that the reason the action of clicking the "Start" button to Shutdown your PC seems so counter-intuitive is that not doing so was even less intuitive. Anyway - that's a complete aside.

A more recent entry mentions in passing someone's whining about the fact that there are a lot of ways to shutdown Windows Vista, particularly the length of the fly-out menu that gives the "advanced" options. Apparently it's too many to choose from. Given that by the time you get to that menu you already know (99% of the time2) what option you want, it's not so much a choice, is it? The same people who whinge about complexity are the ones that then whinge about things being "hidden" when simplification occurs. Grumble, grumble.


----
1 As with anything else on Wikipedia, this could be an utterly incorrect fiction that bears no actual similarity to Raymond Chen.
2 Yeah, a made-up statistic. Most of them are though? No? Just ask the British Government about their statistics ;)

Checklists for System Administrators

|
Whilst mooching around the web, I came across this fantastic list of checklists for System Admins. Bloody brilliant stuff which is well worth a read by any SA.

In other news

- Useful guidelines for Hard Binding / String Freezing in .net from Microsofts P&P bods.
- Windows is not an MFC delivery channel:Feel free to substitute any other framework/DLL/library for "MFC".

Interestingly some comments are made regarding the fact that .net is delivered with Vista/Windows Server 2008, however, this is easily explained by the fact that system level components (IIS / IIS admin, anyone?) are built with .net. In fact, if you use the "Server Core" role, .net 3.0 isn't a part of the installed system, so it's not strictly true that Vista/2k8 are a delivery channel for .net, more that it's a dependency that some OS components have taken.

Accessing private methods for Unit Testing

|

There are a couple of ways to handle getting at un-exposed methods for the purposes of Unit Testing, as described below. Method 2 is, I believe, pretty similar to the way that the Unit Testing tools in the higher editions of Visual Studio operate, albeit with the ability to automatically regenerate the code. Which do you prefer?

Method 1 – InternalsVisibleTo

Requires adding [assembly: InternalsVisibleTo("CompanyName.xxxxxx.xxxxxx.Test")] to the AssemblyInfo.cs file for the CompanyName.xxxxxx.xxxxxx project, then declaring any required methods/properties as internal rather than private.

Pros

-       Intellisense provided by Visual Studio when writing Unit Tests

-       Immediate Compile Time failures when attempting to build Unit Tests if the interface on class has changed when Unit Tests are compiled

Cons

-       Other classes in the same assembly have access to internal parts of a classes interface.

-       The runtime DLLs contain a "reference" to the Test DLLs (albeit one that doesn’t do anything unless the Test DLLs attempt to reference the runtime DLLs)

 

Method 2 – Reflection (Method Invocation)

Requires using reflection, so for example to call the FacadeManager.Disconnect() method, the following code would be required (can be shortcut)
 

Type t = FacadeManager.Instance.GetType();

MethodInfo Disconnect = t.GetMethod("Disconnect", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);

Object[] Parameters = new Object[]{(object)"Disconnecting for Test _008_DisconnectMethodWorks"};

      Disconnect.Invoke(FacadeManager.Instance, Parameters);

This can be wrapped in a testing support class, a la:

 

class FacadeManagerProxy

{

    private FacadeManager _facadeManagerInstance;

    private Type _t;

    private MethodInfo _disconnect;

    private MethodInfo _connect;

    public FacadeManagerProxy(FacadeManager facadeManagerInstance)

    {

        _facadeManagerInstance = facadeManagerInstance;

        _t = _facadeManagerInstance.GetType();

        _disconnect = _t.GetMethod("Disconnect", BindingFlags.Instance | BindingFlags.NonPublic);

        _connect = _t.GetMethod("Connect", BindingFlags.Instance | BindingFlags.NonPublic);

    }

    public void Disconnect(string reason)

    {

        Object[] Parameters = new Object[] { (object)reason };

 

        _disconnect.Invoke(_facadeManagerInstance , Parameters);

    }

    public void Connect()

    {

        _connect.Invoke(_facadeManagerInstance, null);

    }

}

Clearing mangled Templates on Movable Type 4

|
After months of pain because I screwed up my MT4 templates, I finally found a way to sort out the problem in the Six Apart forums. There's the details, just in case the URL is ever broken/changed in the future:

For anyone experiencing similar difficulties, here's what has to be done, blog-by-blog:

(1) Export the blog using MT4's export feature.
(2) Copy the blog folder from my server to my computer as a backup, and because export doesn't retain any associated media files (videos, photos, podcasts) embedded within your blog posts.
(3) Delete the blog from within MT4.
(4) Delete the blog folder from my server, because deleting within MT4 apparently just deletes the blog from your database.
(5) Create a new blog, to be published in the same location as the old one.
(6) Transfer all of the media files BACK to the server (be sure you retain the same folder structure, or all your blog links will be broken).
(7) Import the blog entries and comments using MT4's import feature.
(8) Create all the custom changes that need to be made (new sidebars, widgets, etc).
(9) Assign an MT4 style theme to the new blog.
(10) Republish the blog from within MT4.
(11) Tweak the CSS to make the blog my own and not so cookie cutter.