NameValue, now with Distinct() support

One of the more useful little classes in my utility box is NameValue. It’s so veryeasy to fill with useful data from a Linq query, and once filled so easy to move betweenlayers, in a way that a projection simply can’t be used. The simple little NameValueclass bind just fine to a drop down menu, and will bring you tea after you finishwith that stupid girl that didn’t like your pony/monkey chimera.

I recently had to add equality checks and an override for == and != in order to makemerging two lists of name value pairs together possible. Now .Distinct() works ona List<NameValue>! There’s some goodmaterial out there for how to make a quality equality implementation.

[Serializable] public class NameValue{ public object Name{ get { return _Name;} set {_Name = value;} } protected virtual object _Name{ get; set;} public object Value{ get { return _Value;} set {_Value = value;} } protected virtual object _Value{ get; set;} public override bool Equals(object obj){ if (obj == null) return false; if (this.GetType() != obj.GetType()) return false; // safebecause of the GetType check NameValuenv = (NameValue)obj; // usethis pattern to compare reference members if (!Object.Equals(this.Value,nv.Value)) return false; if (!Object.Equals(this.Name,nv.Name)) return false; return true;} public override int GetHashCode(){ return Name.GetHashCode() ^ Value.GetHashCode();} public static bool operator ==(NameValuen1, NameValue n2) { return n1.Equals(n2);} public static bool operator !=(NameValuen1, NameValue n2) { return !n1.Equals(n2);} }

The Mystery of the Missing Permission

While writing Unit Tests for my membership provider code, I tried adding a permissionI had defined on one test user to a different test user. I was completely freakedout, since user1 suddenly no longer had that permission.

What kind of witchery is this? Does Collection<T>.Add() remove items from thesource collection?

No. It doesn’t. I tried it all with simple collections. List<string>.Add() doesn’thave this effect. So what’s going on?

The problem has to be in my persistence layer. My User and Permissions objects areprovided by LLBLGen. So it’s gotta be something in there. (A light dawns). I lookback at my table definitions. Sure enough, a permission as a m:1 relationship withit’s user. A permission entity can’t belong to more than one user. And LLBLGen issmart enough that when I take the object in the graph and add it to a different user,it not only updates the relationship(FK), but removes it from the original object.

But how do I get the result I wanted? A simple duplication of an existing permission?I adapted the deepcopy method I found over on Stack Overflow into a general extension method andused it to dupe the permission as I added it. Clean copy in the object graph=>nochanges to the relationship(FK)=>success.

public static class DeepCloneExtension{ public static TDeepClone<T>(this Tobj) { using (varms = new MemoryStream()){ var formatter = new BinaryFormatter();formatter.Serialize(ms, obj); ms.Position = 0; return (T)formatter.Deserialize(ms);} } }

IIS FTP 7 Provides Easy Virtual Host FTP

While it didn’t ship with Server 2008, Microsoft added a new FTPServer for the new version off IIS. Unfortunately it is not an SFTPserver, at best supporting FTP/S. However the new authentication options stillmake it a winner. You can provide a virtual FTP host for each web site you host withIIS 7. Just remember to login with [siteFQDN]|[username]…like 

erudition.radianttiger.com|josh

>

The pipey notation tells the server which vhost you belong to. Not elegant. Just waybetter than it was previously.

Next I’m excited to play with provider-basedauthentication. Having a pluggable architecture for logging in makes this FTPserver really powerful.