# Monday, 05 July 2010

I’m working on my presentation for the Alaska .NET Users Group. My goal is to build an app that consumes a web service, and displays the data. After getting the web service piece going, the next step was to display that data in a NSTableView.

I battled for hours and hours trying to figure out how to create a NSTableViewDataSource, and it turns out I was making things way to complicated. I did some searching on the web, but couldn’t really find anything except a post by John Moshakis on the Mono-osx mailing list which gave me some hope that someone had figured it out.

I emailed John, and he was gracious enough to send me some source code to work from. It turns out, I what I had wasn’t far off the mark, I just needed a few pointers.

The DataSource Code

  1. using System;
  2. using System.Collections;
  3. using MonoMac.Foundation;
  4. using MonoMac.AppKit;
  5. using MovieTimes.BusinessLogic;
  6. using System.Collections.Generic;
  7.  
  8. [Register("MyItemDataSource")]
  9. public class MyItemDataSource : NSTableViewDataSource
  10. {
  11.     public List<MyItem> MyItems { get; set; }
  12.  
  13.     static MyItemDataSource () {
  14.     }
  15.  
  16.     [Export("numberOfRowsInTableView:")]
  17.     public int numberOfRowsInTableView (NSTableView aTableView) {
  18.         if (MyItems == null)
  19.             return 0;
  20.         return MyItems.Count;
  21.     }
  22.  
  23.     [Export("tableView:objectValueForTableColumn:row:")]
  24.     public NSObject objectValueForTableColumn (NSTableView aTableView, NSTableColumn aTableColumn, int rowIndex) {
  25.         return new NSString(MyItems[rowIndex].Name);
  26.     }
  27.  
  28.     public void Items(List<MyItem> items) {
  29.         MyItems = items;
  30.     }
  31. }
  32.  
  33. public class MyItemDataSourceDelegate : NSTableViewDelegate {
  34.     public event EventHandler<MyItemChangedEventArgs> MyItemChanged;
  35.  
  36.     public MyItemDataSourceDelegate ():base()  {
  37.     }
  38.  
  39.     public override void SelectionDidChange(NSNotification notification) {
  40.         var table = notification.Object as NSTableView;
  41.            
  42.         var ds = table.DataSource as MyItemDataSource;
  43.            
  44.         var rowNum = table.SelectedRow;
  45.            
  46.         if (rowNum >= 0 && rowNum < ds.MyItems.Count)
  47.             OnMyItemChanged(new MyItemChangedEventArgs(ds.MyItems[rowNum]));
  48.     }
  49.        
  50.     protected void OnMyItemChanged(MyItemChangedEventArgs e) {
  51.         if (MyItemChanged != null)
  52.             MyItemChanged(this, e);
  53.     }
  54. }
  55.    
  56. public class MyItemChangedEventArgs : EventArgs {
  57.     public MyItem MyItem { get; set; }
  58.        
  59.     public MyItemChangedEventArgs(MyItem i) {
  60.         MyItem = i;
  61.     }
  62. }

The code is kind of long, and I’ve removed all the normal guard code you would use for the sake of brevity. But lets dissect the code. The first part, lines 8 – 31 is the actual data source. It simply listens for a few messages (numberOfRowsInTableView and objectValueForTableColumn) and allows items to be added to the data source (line 28). The next part, lines 33 – 54 fire off an event when the selected item has changed in the NSTableView instance.

The Window Code

  1. public override void WindowDidLoad () {
  2.     base.WindowDidLoad ();
  3.  
  4.     var myDel = new MyItemDataSourceDelegate();
  5.     myDel.ItemChanged += ItemChanged;
  6.  
  7.     this.someTable.Delegate = myDel;
  8.     this.someTable.DataSource = new MyItemDataSource();
  9. }

In the window controller, paste the above code to have code run when you window loads. For WinForms people, public override void WindowDidLoad is the equivilent of protected override void OnLoad().

So what does the window controller code do? First off, we create our delegate (note, delegates in Cocoa are not the same thing as in C#), and wire up to the changed event, then we assign the newly created delegate and a data source to our table.

Any questions, thoughts or feedback? Hit me up in the comments, or follow me on twitter.

C# | Mac OS X | Mono | MonoMac
Monday, 05 July 2010 08:00:00 (Alaskan Daylight Time, UTC-08:00)
# Friday, 02 July 2010

When Miguel De Icaza first announced MonoMac back in April I was pretty excited! Its been my goal for the past two years to write a half-decent, native looking, Mac OSX application with .NET. I’ve looked at the various Cocoa binding strategies for Mono and always found them lacking. Some like CocoaSharp haven’t been updated in ages, and the WinForms stuff on Mac looked plain fugly. Like Windows 95 fugly.

I found Michael Hutchinson’s quick-start post on writing a simple .NET app using MonoMac. Because I had previous experience using Interface Builder, I was able to muddle my way through building his simple app. But at the end, I wanted something more. “Hello World” is great to show you the most basic syntax of a language, and maybe how to use the compiler, but it honestly does nothing for me.

So, I set out to write real, although somewhat contrived image viewing application. Having an extensive background writing WinForms, ASP.NET and some WPF apps, the hardest part for me was trying to figure out the Cocoa equivalent to things like PictureBox, and FolderBrowserDialog. But Google as always was my friend.

I’m not going to dive into how to write the app tonight, but I will post a screen shot, and the ImageViewer source!
image

Nits

Yes Cocoa is based on an MVC architecture, and yes I should have put some of the code into a business class, but I didn’t.

.NET | C# | Mac OS X | Mono | MonoMac
Friday, 02 July 2010 07:22:00 (Alaskan Daylight Time, UTC-08:00)
# Tuesday, 29 June 2010

A while back, I was tasked with solving a defect in our system. The defect was “Changes to an image can be reverted only one time.”

As I started to look into the code, I saw something like this:

  1. private Image _originalImage;
  2. private Image _image;
  3.  
  4. public void Show(Image image) {
  5. _image = image;
  6. _originalImage = (Image) image.Clone();
  7. }
  8.  
  9. private void Revert() {
  10. _image = _originalImage;
  11. }
 

Looking at the code, I suspected I knew what was wrong, but I wanted to confirm it. So I ran the app, made modifications to the image, clicked revert, made modifications to the image again, set a break point and clicked revert again. Once Visual Studio broke, I popped over to the immediate window and typed this:


&_image
Which returned: 0x1234abd0
&_originalImage
Which also returned: 0x1234abd0

Now I knew what the issue was, the original image was getting clobbered due to a missed .Clone() on line 10. A simple _image = (Image)_originalImage.Clone(); fixed the issue.

.NET | C#
Tuesday, 29 June 2010 06:45:00 (Alaskan Daylight Time, UTC-08:00)
# Monday, 28 June 2010

The other day, Visual Studio 2008 kept crashing due to an issue with one of our user controls. When I opened a WinForm which contained the control, VS would initialize everything, get to an exception and crash. The exception was caused by the user control running code that it should not have in the designer. A quick search turned up this post on stackoverflow

  1. if (LicenseManager.UsageMode == LicenseUsageMode.Runtime) {
  2.   ...
  3. }

This code works in most scenarios, but failed for me. More searching turned up a post by Andre de Cavaignac

  1. public bool IsDesignerHosted {
  2.   get {
  3.     Control ctrl = this;
  4.     while (ctrl != null) {
  5.       if (ctrl.Site == null)
  6.         return false;
  7.       if (ctrl.Site.DesignMode == true)
  8.         return true;
  9.       ctrl = ctrl.Parent;
  10.     }
  11.     return false;
  12.   }
  13. }

I found that mostly worked as well. I ended up having to combine them to detect correctly 100% of the time if the control was designer hosted. I created it as an extension method, and here is the result

  1. /// <summary>
  2. /// Used to detect whether the control is being hosted on the surface of a form at design-time,
  3. /// since the DesignMode property no longer works once the control has been compiled and
  4. /// hosted on the surface of another design-time control.
  5. /// </summary>
  6. public static bool IsDesignerHosted(this Control ctrl) {
  7.   get {
  8.     while (ctrl != null) {
  9.       if (ctrl.Site == null)
  10.         return false;
  11.       if (ctrl.Site.DesignMode)
  12.         return true;
  13.       ctrl = ctrl.Parent;
  14.     }
  15.  
  16.     return (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
  17.   }
  18. }
C# | WinForms
Monday, 28 June 2010 17:45:00 (Alaskan Daylight Time, UTC-08:00)
# Friday, 03 April 2009

Background

For a project I'm doing, I have a task model for the various pieces. In the beginning, I was manually creating a List<ITask>. As I kept adding tasks to run, I started thinking about hacking some code together to rifle through my assembly and pull back all the classes which implement ITask.

Then I remember hearing about Managed Extensibility Framework (MEF). I did some searching, found the MEF home page, and even read the MEF overview. But none of that told me what I really wanted to know, what's the fastest way to get started using MEF as a component loader?

I did some more searching and found the dnrTV episode "Glenn Block on MEF, the Managed Extensibility Framework" and after 20-30 minutes they finally got down to how to create a plugin for your app.

But what I really wanted, and I bet a lot of others, is a quick start guide for creating a plugin.

Solution

Download the latest version of MEF, as of this writing its Preview 4. Grab the System.ComponentModel.Composition.dll from the bin folder and stash it somewhere. Make a reference to said dll in your project.

On your plugin class, add Export attribute:

[Export(typeof(IPlugin))]
public class Foo : IPlugin { ... }

In your plugin consumer, create a property to hold your plugins, and add the Import attribute:

[Import(typeof(IPlugin))]
internal IList<IPlugin> _myPlugins { get; set; }

Now, tell MEF where to get the plugins at (line 2), and where you want MEF to fulfill any plugins (line 5):

private void LoadPlugins() { var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(catalog); var batch = new CompositionBatch(); batch.AddPart(this); container.Compose(batch); }

 

I put my call to the LoadPlugins method in the constructor.

Now, spin through your plugins and do the work:

Console.WriteLine("Found {0} plugins", _myPlugins.Count);
foreach (var plugin in _myPlugins) {
    Console.WriteLine(plugin.Name);
}

 

Download the complete source to this (really, only about 10 extra lines to glue things together) and have fun!

kick it on DotNetKicks.com
.NET | C# | HowTo | MEF
Friday, 03 April 2009 20:37:33 (Alaskan Standard Time, UTC-09:00)
# Sunday, 15 March 2009

A long time, and many many moons ago I took wrote some code to interface our build server with a network power switch we had laying around the office. We used this to turn on and off lava lamps to indicate the status of the build. Some might ask why we didn't use the X10 support that is already in CCNET, and the answer mostly is cost, and the fact that X10 wouldn't work in our environment.

That was 2.5 years ago. Since then, our team has become more distributed. We have one guy working in Ann Arbor, MI, and occasionally have others telecommuting. So not everyone can see the status of the lamps. Also in the 2.5 years since that code was written, a little thing called Twitter has become very popular. I did some research, and found Tom Fannings nAnt Twitter task and briefly considered using it.

But in the end, I just couldn't resist adding my own developer gold plating and thought it would be neat if we could also issue commands to the build server via tweets. So with that feature in mind, I had to write it myself.

To start out with, I used Yedda's C# Twitter library. The Yedda library is a pleasure to work with, it makes sending a tweet as simple as

new Twitter().UpdateAsXML(_username, password, messsage);

One thing the Yedda library didn't have, was the ability to query for your Twitter replies. A quick look through the source, and the Twitter API docs and I realized this would be trivial. The details of how I did it aren't important to this post, but if your curious, you can look at lines 567 - 627 of the Yedda source included with this post.

I'm not going to dive to much into how the whole project works, but here is a high level. The software runs as a Windows Service, leverages the CCTrayLib assembly for Cruise Control.NET to do all the heavy lifting. It polls the Cruise Control.NET server every 5 seconds, and fires events when things happen. The two events we want are the Polled and BuildOccurred events.

These events allow us to intern kick off our own events based on the state of the build. Based on the state, we grab the appropriate actions to run as defined in the BuildActions.xml file. This maps a build state to a set of actions. In the case of a "Building" action, we send a Twitter, with a message template of "{PROJECT} is building", and turn ports 1 on, and 2 off on our ePower switch. Easy enough.

But how do we take in commands? I pondered this for a minute than realized it would be trivial to leverage the Twitter replies API for this. But what about security, we can't have just anyone sending commands to our build server. This is where the Twitter friends API comes in handy. In order to issue commands to our build server, the account our build server uses has to have you as a friend, not just a follower.

The first action I implemented was the force build command. The idea for the grammar came from a joke reply @orand sent our Twitter bot. After that, I thought it might be nice to be able to get the list of projects, get a projects status, and ask for help. So that leaves us with a total of 4 commands.

A small bunny trail

When I first wrote the command parser, it looked something like this:

if (msg.StartsWith("force build "))
    ProcessForceBuildCommand(msg, user);
else if (msg.StartsWith("get projects"))
    ProcessProjectListsCommand(msg, user);
...

I thought about it for a while, and thought there had to be an easier way. We are using .NET 3.5 after all, with all its lamba, LINQ and new and improved delegate goodness. I did some research and came upon the Action<T> (and Func<T>) delegate type. And came up with this implementation for registering commands

var commands = new Dictionary<string, Action<string, string>>
    {
        {"force build ", ProcessForceBuildCommand},
        {"get projects", ProcessProjectListsCommand},
        {"get project status ", ProcessProjectStatusCommand},
        {"help", ProcessHelpCommand}
    };

Once we have all the commands registered, we can use some lambda and LINQ magic to act upon the commands issued

var key = _commands.Keys.Where(msg.StartsWith).First();
if (!string.IsNullOrEmpty(key))
    _commands[key].Invoke(msg, user);
else
    SendTweet(string.Format("@{0} I'm sorry, I don't understand you. Maybe you should ask for help", user));

Get to the point, I want to see the source code

The code overall is fairly well structured (if I do say so myself), although there is one area where I did violate the separation of concerns rule, the TwitterManager class knows more about Cruise Control.NET than it should. But, given that this is a very simple internal project, and not for public consumption, I'm mostly OK with that :)

I've included the source to our entire build monitor, I hope you find it useful.  We are using a very old version (1.1) of Cruise Control.NET in our environment. If your using a more recent version, you will probably need to swap out the Cruise Control CCTrayLib and Remote assemblies for something more recent, and invert some of the commented out code in the SetupCruiseControl method of BuildServerMonitor.cs

If you have any questions, or find/fix any bugs, please feel free to leave a comment, or send me a tweet, my username on Twitter is akcoder.

Download the Afhcan.BuildMonitor

.NET | C# | HowTo
Sunday, 15 March 2009 19:59:22 (Alaskan Standard Time, UTC-09:00)
# Friday, 09 May 2008

The Problem

For the last few days I've been trying to figure out how to do this SQL in linq:

   1: select from customers where substring(customers.Name, 1, 1) IN ('a', 'b', 'c', 'd');
The problem is the list (a, b, c, d) is variable, on one page I need all the results where foo.Name starts with a or b, on another it could be b, c, d, e and f.  This is trivial to do in SQL, you just break out a little dynamic sql and your done.  But being a noob to LINQ, I'm not so sure how to go about doing this.

I had a lot of failed queries, and failed code, before giving up and just looping through the letters for that particular page and manually building up what I wanted.

But that's kind of a hack. OK, its not kind of a hack, its a big hack.

The Solution

This LINQ goodness (using the Customers db in LINQPad) will pull back all the customers who's names begin with a t, d, or j (note the .ToList() on line 4, I'll discuss it next).

   1: string[] letters = { "t", "d", "j" }; 
   2:  
   3: var customers = (from c in Customers
   4:     select c).ToList(); 
   5:  
   6: var results = from c in customers
   7:     join letter in letters on c.Name.First().ToString().ToUpper() equals letter.ToUpper()
   8:     select c; 
   9:  
  10: customers.Dump("Customers");
  11: results.Dump("Filtered by first letter");

And LINQPad renders this for us:

image

Limitations

LINQ to SQL doesn't support querying a SQL database with if one of your sources is an in-memory store, except if you use the Contains operator.  So, to work around that, you have to pull back ALL the results from the db, and then convert it to a List before you use it in the above query.

I'm my opinion, this is kind of a painful limitation, but for my particular purpose, I'm willing to live with it because my particular database will only ever have around 100 or so rows, so pulling everything back in memory isn't such a big deal...

P.S. if you are reading this in a feed reader, let me know how it renders, I've updated my code snippet plugin to try and fix the previous rendering problems.

.NET | C# | LINQ
Friday, 09 May 2008 01:13:08 (Alaskan Daylight Time, UTC-08:00)
# Sunday, 25 November 2007

Background Information

Aspect Oriented Programming has been on my list of things to read up on for almost a year now.  It likely would have stayed on my list of things to do well into the future if it hadn't been for a recent .NET Rocks episode.  I can't recall if it was the Pablo Castro on Astoria, or the Tim Sneath and Ian Ellison show, but one of them off-hand mentioned PostSharp.  As I usually do when links are thrown about on the show, I scribbled it on my hand for later review. Update DNR just released a show with Gael Fraiteur of PostSharp fame!

Once I got to the PostSharp site, I found their sample video on creating a trace AOP attribute.  They had me hooked.  I wasn't so much interested in doing a logging or trace attribute, but was more interested in doing a few validation attributes.  More specifically a NotNullAttribute.

The Problem

What got me interested in doing the NotNullAttribute was thinking back to how many times in our code base we would have something like this:

1 public void AddUser(Organization org, User user) 2 { 3 if (org == null) 4 throw new ArgumentNullException("org"); 5 if (user == null) 6 throw new ArgumentNullException("user"); 7 8 org.AddUser(user); 9 _ordDal.Save(org); 10 }

Granted this is a somewhat short method, but we have methods which take 6 parameters, and only 2 actual lines of code.  That's 12 lines of code taken up for argument checking, and 2 lines of code.  That would put our crap to code ratio at 6/1.

The Solution

Wouldn't it be much nicer if you could do something like this:

1 public void AddUser([NotNull] Organization org, [NotNull] User user) 2 { 3 org.AddUser(user); 4 _ordDal.Save(org); 5 }

 

PostSharp is almost there to let you write code just like that.  Currently, there is no support for an OnParameterAspectBoundaryAttribute in PostSharp, so we have to do things just slightly differently:

[NotNullHelper] public void AddUser([NotNull] Organization org, [NotNull] User user) { org.AddUser(user); _ordDal.Save(org); }

We have to have a helper class were all the actual code lives.

How PostSharp Works

PostSharp works by weaving all your AOP attributes with your source code after the compiler compiles your code.  This article by Gael Fraiteur describes in more detail how the PostSharp process works.  After the code is post-compiled by PostSharp, you end up with something very ugly that looks like this:

public void AddUser([NotNull] Organization org, [NotNull] User user) { MethodExecutionEventArgs ~laosEventArgs~2; try { object[] ~arguments~1 = new object[] { org, user }; ~laosEventArgs~2 = new MethodExecutionEventArgs(methodof(OrgManager.AddUser, OrgManager), this, ~arguments~1); ~PostSharp~Laos~Implementation.NotNullHelperAttribute~3.OnEntry(~laosEventArgs~2); if (~laosEventArgs~2.FlowBehavior != FlowBehavior.Return) { org.AddUser(user); _orgDal.Save(org); ~PostSharp~Laos~Implementation.NotNullHelperAttribute~3.OnSuccess(~laosEventArgs~2); } } catch (Exception ~exception~0) { ~laosEventArgs~2.Exception = ~exception~0; ~PostSharp~Laos~Implementation.NotNullHelperAttribute~3.OnException(~laosEventArgs~2); switch (~laosEventArgs~2.FlowBehavior) { case FlowBehavior.Continue: case FlowBehavior.Return: return; } throw; } finally { ~PostSharp~Laos~Implementation.NotNullHelperAttribute~3.OnExit(~laosEventArgs~2); } }

I don't know about you, but I would have one HELL of a time trying to debug this, thankfully PostSharp touches up the PDB files, so when your debugging, you only see and debug the source code you've written, both your attribute, and your business logic.

The Drawbacks

PostSharp modifies your code! Well, not directly, but it is still modifying your code (as seen above), but thankfully it touches up the PDB files so all the mess under the hood is hidden from you.

Conclusion

This is but one of the many things you can do with AOP and PostSharp.  Next up, I plan to write some validation attributes.  Look for those in the next few weeks.  In the mean time, download the source code for the NotNullAttribute.

I'm sorry if this post looks terrible in your feed reader, I still haven't been able to figure out why the code looks terrible.  If anyone has any suggestions, let me know.  I'm using DasBlogCE as my blog engine.

kick it on DotNetKicks.com
.NET | AOP | C# | PostSharp
Sunday, 25 November 2007 20:37:23 (Alaskan Standard Time, UTC-09:00)
# Sunday, 07 October 2007

If you've read Raymond Chen's blog long enough, then you know trying to change system stuff directly in Windows registry is discouraged, if not frowned upon.  So when I kept hacking away at the registry trying to get some Windows Firewall exceptions for XP and Vista created, I decided to take a step back and see what Windows's API's are out there to do this.

Doing some Google searches doesn't reveal much (which is why I decided to blog this), except these two hidden gems Syslog daemon for Windows Eventlog, and Adding a port to the XP Firewall.  Both of these gave me pointers in the right direction to create this gem:

  1 private static void ExceptionToFirewall(bool add, string imageFileName, string name)
  2 {
  3 	Type netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr");
  4 	INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType);
  5 
  6 	INetFwProfile curProfile = mgr.LocalPolicy.CurrentProfile;
  7 	if (add)
  8 	{
  9 		Type NetFwAuthorizedApplicationType = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication", false);
 10 		INetFwAuthorizedApplication app = (INetFwAuthorizedApplication)Activator.CreateInstance(NetFwAuthorizedApplicationType);
 11 
 12 		app.Name = name;
 13 		app.ProcessImageFileName = imageFileName;
 14 		app.Enabled = true;
 15 		app.RemoteAddresses = "*";
 16 		app.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
 17 
 18 		curProfile.AuthorizedApplications.Add(app);
 19 	}
 20 	else
 21 	{
 22 		curProfile.AuthorizedApplications.Remove(imageFileName);
 23 	}
 24 }
 

To use this, you'll need to add a reference to COM component HNetCfg.FwMgr (Guid "{304CE942-6E39-40D8-943A-B913C40C9CD4}", file path C:\windows\system32\hnetcfg.dll).

One note, don't use the IpVersion property of INetFwAuthorizedApplication, under Windows Vista it throws a NotImplimentedException.

kick it on DotNetKicks.com 

del.icio.us Tags: , ,

C# | Firewall | Windows
Sunday, 07 October 2007 18:50:55 (Alaskan Daylight Time, UTC-08:00)
# Thursday, 13 September 2007

Usually I post solutions I've discovered or created to problems I've encountered.  Now, I've run into a problem with the XmlSerializer and I need your help solving it.  Scroll to the bottom for an update.

The Problem

I have a class called Foobar.  Foobar inherits from List<string>, and contains one member Title.  When I use the XmlSerializer to serialize and deserialize Foobar, all the items in the List are make it through safely, but the member Title does not.

The Code

using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; internal class Program { private static void Main() { Foobar f = new Foobar(); f.Add("1"); f.Add("2"); f.Title = "Title"; MemoryStream mem = new MemoryStream(); XmlSerializer xs = new XmlSerializer(typeof(Foobar)); xs.Serialize(mem, f); mem.Seek(0, 0); mem.Seek(0, 0); Foobar deser = (Foobar)xs.Deserialize(mem); Console.WriteLine(deser); } } [Serializable] public class Foobar : List<string> { public string Title; }

The Solution

One way I came up with to solve this problem is make Foobar not inherit from List<string>, and make a member called Items that is a List<string>, but that's kind of hokey.  I've searched Google and can't find a solution.  So I'm appealing to my dear readers for a better solution.

Update

Looks like I was being way to specific in my Google search, searching on "XmlSerializer List" gives us this http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=566175&SiteID=1 as the first item.  In the post,

Elena Kharitidi says "XmlSerializer does not serialize any members if a collection. Only collection items get serialized. This is by design, basically a decision was made to handle collections as arrays not as classes with multiple properties, so collections should look like arrays on the wire, therefore they do not have any members other then collection items, and can be “flattened” by adding the [XmlElement] to the member of the ICollection type."

Long and short, I need to use the hokey solution I came up with (or something like it), or write my own serializer.

Thursday, 13 September 2007 07:22:29 (Alaskan Daylight Time, UTC-08:00)
# Tuesday, 23 January 2007

I've started work on the search piece of our application.  Searching (no pun intended) for some inspiration on how users might want to search within our application, I brought up the current WinForms version, and then the ASP.NET version.

I realized that we need to make searching easier in WinForms client, currently when searching for a patient in our WinForms client, you are presented with a separate text box for first name, last name, SSN, date of birth, and health record number.  Our ASP.NET client presents just one text box to search all of those fields.

Tying the Domain Model to the Data Access Layer

So I started thinking about all the pieces in our domain model that we might want to allow the user to search.  Then I realized that to allow searching across all the fields in an entity would tie the data access layer (DAL) to  the domain model ala something like this:

public IList<Patient> Search(string text) { ICriteria criteria = session.CreateCriteria(typeof(Patient)); Disjunction or = Expression.Disjunction(); text = string.Format("%{0}%", text); or.Add(Expression.Like("FirstName", text)); or.Add(Expression.Like("MiddleName", text)); or.Add(Expression.Like("LastName", text)); crit.Add(or); return criteria.List<Patient>(); }

Variations of this code would have to be repeated for every data access class in our DAL.  You could create a string list of the properties on the entity to search and pass them to a method that would build your criteria; but at the end of the day, your still tying your domain model to your data access layer.

Custom Attributes

I started doing some thinking about how unit testing frameworks such as  work and realized I could use .NET custom attributes and some reflection to solve the problem.

I came up with an attribute called Search, currently it takes in one boolean parameter called Enabled.

1 using System; 2 using System.Reflection; 3 4 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] 5 public class SearchAttribute : System.Attribute 6 { 7 public bool Enabled; 8 9 /// <summary></summary> 10 /// </summary> 11 /// <param name="Enabled">if true, property will be included in object-level searches</param> 12 public SearchAttribute(bool Enabled) 13 { 14 this.Enabled = Enabled; 15 } 16 }

I could have written it as:

1 using System; 2 using System.Reflection; 3 4 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] 5 public class SearchAttribute : System.Attribute { }

But I wanted the flexability of adding named parameters in the future.  Who knows, maybe this is a lame reason, and I should just refactor the code if I want to add parameters to it in the future.

Adding the Attribute to Your Domain Model

The interesting thing about a custom attribute, is if it ends in Attribute (i.e. SearchAttribute), its name gets changed to Search by the C# compiler(?), although you could still use SearchAttribute as the name when you add it to your properties.  If you look at the IL, its still called SearchAttribute:

.custom instance void MyNamespace.SearchAttribute::.ctor(bool) = ( bool(true) )

But if you change the attribute name to SearchAttrib, or SearchAttributes, then you have to use the full name when decorating the properties in your class.  This really isn't germane to the topic at hand, I just thought it was neat! :)

Example:

1 public class Patient 2 { 3 [Search(true)] 4 public string FirstName 5 { 6 get { return _firstName; } 7 set { _firstName = value; } 8 } 9 ... 10 }

Pulling it All Together

The custom attribute is great and all, but it doesn't inherently buy us anything.  We need to add a little bit more to make all this work.  This is a simplified version of what I ended up putting in my DAL base class:

1 private IList<Patient> Search(string text) 2 { 3 Type type = typeof(Patient); 4 ICriteria criteria = _session.CreateCriteria(type); 5 6 Disjunction or = Expression.Disjunction(); 7 foreach (PropertyInfo propInfo in type.GetProperties()) 8 { 9 //SearchableAttribute is AllowMultiple = false, so we only need the first item 10 Attribute[] attribs = Attribute.GetCustomAttributes(propInfo, typeof(SearchAttribute)); 11 if (attribs.Length == 0) 12 continue; 13 14 if (((SearchAttribute)attribs[0]).Enabled) 15 or.Add(Expression.InsensitiveLike(propInfo.Name, string.Format("%{0}%", text))); 16 } 17 criteria.Add(or); 18 return criteria.List<T>(); 19 } 20

Again, this is a simplified version of what's in our DAL base class, the actual signature looks more like this: private IList<T> Search<T>(string text) so that I don't have to write a version of this for each class in our domain model I want to enable searching for.

Everything above should be pretty self-explanatory unless you aren't familiar with the NHibernate Criteria API; in which case the Expression.Disjunction bit on line 6 is how you do an OR (a OR b OR c)when searching.  You can also do Expression.Conjunction if you want AND searching (a AND b AND c).

The original version of this code had a second foreach loop which spun through all the attributes on each property looking for the SearchAttribute.  I thought to myself that their had to be a better way and did a little bit of poking around, and discovered much to my delight that the static method Attribute.GetCustomAttributes allows you to specify what kind of attribute you are searching for!

Limitations

In the code above, you'll get an Exception if you try to search against non-text columns in your database so you will need to that into account when putting the Search attribute in your Domain Model; or add some more smarts to the Search method to take into account different data types.

Comments

If you've read down this far, then I'd love to get a comment from you.  Is there anything you think I could do a better job of explaining, am I an awesome guy?  Or do I suck, either way, I'd like to know, so please leave me a comment!

If you like my article, please kick it at .NET Kicks (I have no idea why the kick counter says I have zero kicks btw)!

.NET | C# | NHibernate | ORM | Searching
Tuesday, 23 January 2007 11:02:42 (Alaskan Standard Time, UTC-09:00)
# Thursday, 18 January 2007

We are in the midst of doing a total rewrite of our Software, and one of the things that has come up is date and time.  How do we do it, how do we store it, and how do we ensure that we can compare DateTime from one timezone to DateTime in another timezone.  After a lot of research, we settled on using UTC (or UCT depending on your preference).  FxCop will take care of ensuring we use UTC (for the most part).

That solves the problem, or so we thought.  Turns out, when you create a DateTime object either through the constructor, or through DateTime.Parse, its Kind defaults to DateTimeKind.Unspecified.  We need a way to ensure that all DateTime objects are always set to UTC.

What are our options?

Because we are using NHibernate, we have a few options.  The three NHibernate specific ones that immediately come to mind are using an Interceptor, a custom UserType with a SQL datetime column, and a custom UserType with a SQL varchar column; and the non-NHibernate specific one is creating our own DateTime container.  What are the pros and cons of each of these?

NHibernate Interceptor

Pro: very cross-cutting, can touch every object as it comes in and goes out to the database; if there are other data types we need to monkey with, we already have a framework in place.

Con: Very cross-cutting, can be expensive because it's touching every property on every entity as the entities are loaded and persisted

UserType with SQL datetime column

Pro: Only touching the DateTime objects that we want it to

Con: The type has to be specified for every DateTime object in every mapping file; no meta-data along with the date to stamp in the timezone it was created in

UserType with SQL varchar column

Pro: Only touching the DateTime objects that we want it to; can store the timezone and offset along with the date in the db

Con: Same as above UserType; abusing SQL data types; datetimes created at the same (relative) time in two different timezones won't be sorted correctly

Custom DateTime container

Pro: We can do anything we want

Con: Yuck! - I could write a whole paragraph on why this is yucky, but I'll leave that to your imagination

 

After some thought, I decided on the Interceptor!  Here is the class I came up with (you can also download the complete UtcDateTimeInterceptor class):

1 class UtcDateTimeInterceptor : IInterceptor 2 { 3 public bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types) 4 { 5 ConvertDatabaseDateTimeToUtc(state, types); 6 return true; 7 } 8 9 public bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types) 10 { 11 ConvertLocalDateToUtc(state, types); 12 return true; 13 } 14 15 public bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, 16 IType[] types) 17 { 18 ConvertLocalDateToUtc(currentState, types); 19 return true; 20 } 21 22 private void ConvertLocalDateToUtc(object[] state, IType[] types) 23 { 24 int index = 0; 25 foreach (IType type in types) 26 { 27 if ((type.ReturnedClass == typeof(DateTime)) && state[index] != null && (((DateTime)state[index]).Kind == DateTimeKind.Utc)) 28 { 29 state[index] = ((DateTime)state[index]).ToUniversalTime(); 30 } 31 32 ++index; 33 } 34 } 35 36 private void ConvertDatabaseDateTimeToUtc(object[] state, IType[] types) 37 { 38 int index = 0; 39 foreach (IType type in types) 40 { 41 if ((type.ReturnedClass == typeof(DateTime)) && state[index] != null && (((DateTime)state[index]).Kind == DateTimeKind.Unspecified)) 42 { 43 //Create a new date and assume the value stored in the database is Utc 44 DateTime cur = (DateTime)state[index]; 45 DateTime result = DateTime.SpecifyKind(cur, DateTimeKind.Local); 46 state[index] = result; 47 } 48 49 ++index; 50 } 51 } 52 }

Loading The Entities

For the sake of brevity, I'm going to exclude the bits of the Interceptor interface that aren't relevant to my posting.  With that said, the OnLoad event (line 3) gets fired every time an entity is loaded from the database.  We can count on the fact that all dates are stored as UTC in the database because of stuff we'll do later, so we need to convert the DateTime that NHibernate generates (which has a DateTimeKind of Unspecified) to a UTC date (line 36 - 48).  The types array holds the CLR data type of each property in the entity, and each type in the array contains both the internal NHibernate type, and the CLR type. But we don't really care about how NHibernate maps the data types internally, so we are only interested in type.ReturnedClass.

The first thing we need to do is see if the ReturnedClass is of type DateTime (line 41), if its a DateTime, then we need to see if its null, and finally double check that the Kind on the DateTime object coming back from the database is Unspecified.  This last check is a sanity check, in case this behavior changes in the future.

After all these checks are passed, we need to create a new DateTime object from the old one, and set its Kind to Utc (lines 44 and 45).  Thankfully, DateTime has the built-in method SpecifyKind which will take care of building a DateTime of the specified kind for us. And finally replace the existing DateTime object with our new one (line 46).  Shampoo, rinse, repeat for all the DateTime values in the entity.

Persisting The Entities

Now we can move on to the save and update NHibernate events (line 9, and 15 respectively).  In these, we want to make sure the values being persisted to the datastore are UTC, and that no Local times have slipped through the cracks.  If one were so inclined, they could throw an error instead of converting the DateTime to UTC...

The basic code for converting Local DateTime's to Utc (line 22- 34) is much the same as above, but with a few exceptions.  When we do all our checks (line 27), this time we make sure the DateTimeKind is Local before we perform a conversion operation on it.  It is pointless to check if the Kind is Unspecified, because there is no conversion operation we can really perform on it.  On line 29, we can use the built in DateTime method ToUniversalTime() to convert a LocalTime to UTC.

Finishing Up

How do we wire this all up?  When you open a session on your session factory, you can pass in an Interceptor, this is where you would pass in the UtcDateTimeInterceptor.  eg:

ISession openSession = ourSessionFactory.OpenSession(new UtcDateTimeInterceptor());

I want to give credit where credit is due, I got the actual idea of using an Interceptor from  where he grappled with DateTime, null, messages and web services.  If anyone using NHibernate doesn't read , I would highly encourage you to.  He is a very, very sharp fellow; and prolific blogger.

.NET | C# | NHibernate | ORM
Thursday, 18 January 2007 15:36:52 (Alaskan Standard Time, UTC-09:00)