Thursday, December 25, 2008

i4o: IndexSpecification<T> for the IndexableCollection<T>

We've removed the IndexableAttribute from the i4o library and replaced it with the IndexSpecification<T>. Below I'll explain how you can add/remove/change the index for an IndexableCollection<T>.

Let's show several examples on how to create an IndexableCollection<T>.

Given any enumeration of objects you can translate into an IndexableCollection<T>. For the examples below we're going to use an enumeration of the System.IO.FileInfo class. We are going to index the list of FileInfos by file Extension and weather the file IsReadOnly or not.

Setup the list:

// Get our thing to index
string dir = @"C:\Windows\System32\";
var fileInfosFromDir = (from f in System.IO.Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories)
select new FileInfo(f)).ToList();


Can create the IndexSpecification...

...for the FileInfo's "Extension" and "IsReadOnly" properties and turn the list of FileInfos into an IndexableCollection using the IndexSpecification.

// Create the index specification
var spec = new IndexSpecification<FileInfo>()
.Add(i => i.Extension)
.Add(i => i.IsReadOnly);

// Turn the list of files into an Indexed collection of files
var indexedFileInfosFromDir = fileInfosFromDir.ToIndexableCollection(spec);

Create IndexableCollection<T> without IndexSpecification<T>:

You are not required to specify and IndexSpecification<T> when creating the IndexableCollection<T>. You can translate the list into an IndexableCollection<T> and add properties to index after the fact. EX:

var indexedFileInfosFromDir = fileInfosFromDir.ToIndexableCollection();

// Specify the properties to index dynamically (more late bound)
indexedFileInfosFromDir
.CreateIndexFor(i => i.Extension)
.CreateIndexFor(i => i.IsReadOnly);

Swap one IndexSpecification<T> for another:

If you want to completely swap out the index at run time, you can give the IndexableCollection a new IndexSpecification

var list = new List<FileInfo>();
var indexedList = list.ToIndexableCollection();

indexedList.UseIndexSpecification(
new IndexSpecification<FileInfo>()
.Add(o => o.Directory)
.Add(o => o.Name));

I think that should cover most of the general cases. Hope this helps...

Saturday, December 20, 2008

i4o: Update (Index for Objects)

Recently I was added as a developer to the i4o codeplex project by Aaron and last night I made my first commit.

There were a few pretty big changes to the library. I'll highlight some of them below, and in a couple future posts will describe in a little more detail how you can use these features.

Removed the IndexableAttribute:

This was removed for two reasons.

  • It did not allow you index objects you didn't own.

Say you want to index a collection of System.IO.FileInfo objects. Since that's owned by the .Net framework, you can't apply an attribute to the properties of the class you want to index.

The solution to that was to allow adding indexed properties dynamically, however you had to give it a string representing the property name, which leads into the second issue...

  • Didn't provide any compile time checking or refactor support for properties using the dynamic add/remove methods.

If you refactored a property and forgot to rename the string to match the refactor you would end up with a runtime error.

To resolve the two issues above I've introduced the IndexSpecification<T> (I'll describe how to use it a later post)

Performance tuning:

Although we haven't enhanced the Linq support for IndexableCollection (should come soon), I was able to eek some performance by doing some internal caching of property types and a few other tweaks resulting in the Index creation becoming about 30% faster...

Fluent interface for managing the Indexes

The last big change which fits in with the IndexSpecification<T> is the Fluent interface for dynamically adding/removing properties to be indexed. One short example could be:

image

Thursday, December 18, 2008

Leverage Live Mesh to sync Windows Live Writer blog drafts between and multiple computers.

I decided to start blogging a little more regularly and one of the first things I ran into when researching the easiest way to create/publish blogs was Windows Live Writer.

I'm sold... This program handles just about everything I could need for blogging (I'm new to it, so haven't pushed it's limitations to far).

One problem I had with Live Writer was it saved all my drafts/posts on the computer I was logged into at the time. This meant the drafts I write at home can't be accessed at work and visa/versa...

To solve this I decided to wire them through the Microsoft Mesh. If you haven't setup your mesh yet, I would highly recommend it. Just go to Mesh.com, sign up/or in with a Windows Live account, install the client application, and your meshified...

With Live Mesh, you can synchronize files with all of your devices, so you always have the latest versions handy. Access your files from any device or from the web, easily share them with others, and get notified whenever someone changes a file.

This means I can synchronize folders between my connected computers.

Below I'll show the steps I used to setup my meshified weblog posts.

  1. Find the location your Live Writer is storing your drafts/posts.

    For my Windows XP machine it was in...
    C:\Documents and Settings\JasonJ\My Documents\My Weblog Posts\

    For my work Vista pc the folder was in...
    C:\Users\JasonJ\Documents\My Weblog Posts\
  2. Next I created a folder in my mesh called "BlogDrafts".

    image To do this you can right click in the mesh folder area and select "Create folder in Live Mesh".image
  3. A window will popup asking for the name of the new folder and the location this folder will live on your hard drive.

    image 

I think that's everything... follow similar steps to get your other computers synced up to the mesh and you should be relieved that you can work on any computer and on any post.

I can't stand mass mailings... (Especially when you can't turn them off)

 

WTF?

 

image

 

UPDATE: Thanks to Justin for pointing out my tarddedness... I should have left the boxes checked. However when I tried that I got the same error :(

image

Apparently it's not a "temporary problem"... or they could define temporary since this has been there for several days now.

Saturday, December 13, 2008

T4 Text Templating in Silverlight (Thanks Oleg Sych!)

First if you're new text templating, please go read Oleg Sych's blog. This, as far as I've found, is the (Bible, Koran, TheWord, etc...) of visual studio's T4 Text Templating.

If you already know how to add a T4 template to a project, skip this how to and go to "How to make a T4 template work with a Silverlight project:".

How to add a T4 template to a Visual Studio .Net Project:

Given the following project:

Initial Silverlight Project

Bring up the add new item dialog.

Add New Item

Select a "Text File" and rename the extension to a .tt.

Add a new t4 template

You should end up with a .tt template file and under it where the generated out put of the template will land.

T4Sample.tt

You may also notice the following errors in your errors pane.

  • Compiling transformation: The type or namespace name 'CompilerError' does not exist in the namespace 'System.CodeDom.Compiler' (are you missing an assembly reference?)
  • Compiling transformation: The type 'System.CodeDom.Compiler.CompilerErrorCollection' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
    Compiling transformation: 'System.CodeDom.Compiler.CompilerErrorCollection' does not contain a definition for 'Add'
  • A namespace does not directly contain members such as fields or methods

This is where Oleg Sych comes to the rescue...

How to make a T4 template work with a Silverlight project:

Side Note: So, I had a couple emails back and fourth with Oleg about trying to get T4 to work in Silverlight. Almost all of his emails had some sort of sentence that went like this. "Try adding {SomeSpecialT4RelatedWord} and then do {this special thing} and report your findings back to me". This is where the {SomeSpecialT4RelatedWord} would stump me. So I would search Google for "T4 {SomeSpecialT4RelatedWord}" and Oleg Sych's blog was the first hit for each search. And not only was it the first hit, but when I would look the post for that {SomeSpecialT4RelatedWord} the answer was there and exactly what I needed. Thanks Oleg.

 

Fortunately, to get the T4 templates to work within a Silverlight project you only need to put an <#@ assembly #> directive telling the template where to get a reference to the System.dll.

Below is a sample template:

<#@ template language="C#" #>
<#@ assembly name="C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll" #>
namespace SampleT4
{
public class SomeClassSample
{
}
}



And below is the output:




namespace SampleT4
{
public class SomeClassSample
{
}
}



That's it!!!

Sunday, December 7, 2008

i4o & Silverlight Unit Tests (A little more work than the i4o library)

Follow up to my post on i4o & Silverlight (compiles first try)...

 

I took a stab at porting the i4o unit tests to Silverlight which was quite a bit more work than I initially expected.

After creating a Silverlight Unit Test project and linking the original test files into the Silverlight project, I compiled...

  • First the VB using statement wasn't even needed, so I removed that. using Microsoft.VisualBasic;
  • Second there is no System.Diagnostics.Stopwatch() class in Silverlight, so I basically implemented a quick one using DateTime to get the unit tests to compile in Silverlight. Here's the class, except the Frequency property has been commented out (didn't spend time to figure how to make that correct, or what is correct???)
public class Stopwatch
{
private DateTime _StartUtcDateTime;
private DateTime? _EndUtcDateTime;
private bool _IsRunning = false;

//public static readonly long Frequency { get { throw new NotImplementedException(); } }
public static readonly bool IsHighResolution = false;
public Stopwatch()
{}

public TimeSpan Elapsed
{
get
{
if (_EndUtcDateTime.HasValue)
{
return new TimeSpan(_EndUtcDateTime.Value.Ticks - _StartUtcDateTime.Ticks);
}
else
{
return new TimeSpan(DateTime.UtcNow.Ticks - _StartUtcDateTime.Ticks);
}
}
}

public long ElapsedMilliseconds { get { return Elapsed.Milliseconds; } }
public long ElapsedTicks { get { return Elapsed.Ticks; } }
public bool IsRunning { get { return _IsRunning; } }

public static long GetTimestamp()
{
return DateTime.Now.Ticks;
}

public void Reset()
{
_EndUtcDateTime = null;
_StartUtcDateTime = DateTime.UtcNow;
}

public void Start()
{
_EndUtcDateTime = null;
_IsRunning = true;
this._StartUtcDateTime = DateTime.UtcNow;
}

public static Stopwatch StartNew()
{
var w = new Stopwatch();
w.Start();
return w;
}

public void Stop()
{
_EndUtcDateTime = DateTime.UtcNow;
_IsRunning = false;
}
}


The only other issue that came up was some of the Stopwatch dependent tests happened so fast that they would fail intermittently... the quick hack/solution for this was to up the iteration count of whatever they were testing.


After all the above taking care of all the above issues, I was able to get the unit tests to pass.


image

Friday, December 5, 2008

Is my HSA trying to tell me something?

 

This is my "randomly" generated "Site Key". My login's special word...

Would anyone else feel a little uneasy about this in a health related site?

 

clip_image002

Tuesday, December 2, 2008

i4o with Silverlight (Compiles first try)

So I've been thinking lately of a problem that we will be solving at work that will require a user to follow these steps...

1. User selects some set of data to work with
2. Some number crunching has to happen to give the user a report like interface
3. User analyses that data, tweaks some value and will basically go to step 2 above...

This application is being build with Microsoft Silverlight and in thinking about how to make step 2 above as smooth and responsive as possible, I thought "Hey, what about i4o?"

A quick Google for "i4o Silverlight" and without digging too far I basically found nobody had tried it, or at least tried and told about i4o in Silverlight. So I thought I'd give a go...

 

Step 1: Go get the code from from codeplex/i4o. I downloaded the latest source bits.

Step 2: After extracting the .zip...Open up the project i4o.sln

Step 3: Add a new Silverlight class project

image

Step 4: Now use the "Add existing item" option to add files to the Silverlight project. Browse to the i4o project folder to select the 3 i4o class files. You can add those to you Silverlight project as is or use the "Add as Link" feature to share the code across the platforms.

image 

Step 5: once you've add the files to the Silverlight project final step is to "BUILD" the solution...

What's that you say? "That's all there was to it?" YA, my thoughts exactly, the project just compiled on the first try...

 

DISCLAIMER: I haven't tried to use it yet... It's late and this idea popped into my head distracting me from getting to sleep. So I tried it and am quite satisfied for now.

 

Up NEXT: Don't know if I'll do this sooner than later, if at all, but figure out how hard it would be to port the existing unit tests to see how they run in Silverlight...

 

Copyright 2008 All Rights Reserved - Revolution Theme - by Brian Gardner. Converted into Blogger Template by Bloganol dot com