Skip to content

Instantly share code, notes, and snippets.

@cbcwebdev
Created September 8, 2011 20:16
Show Gist options
  • Save cbcwebdev/1204550 to your computer and use it in GitHub Desktop.
Save cbcwebdev/1204550 to your computer and use it in GitHub Desktop.
var criteria = session.CreateCriteria<Location>()
.Add(Expression.Eq("Heirarchy.Token", token))
.SetFetchMode("Metadata", FetchMode.Eager)
.SetFetchMode("Heirarchy.Children", FetchMode.Eager)
.SetFetchMode("Heirarchy.Metadata", FetchMode.Eager)
.SetResultTransformer(new DistinctRootEntityResultTransformer());
public class Heirarchy<T> where T : class
{
public Heirarchy()
{
Children = new List<T>();
}
public virtual T Parent
{
get;
private set;
}
public virtual IList<T> Children
{
get;
private set;
}
public virtual bool IsRoot
{
get
{
return Parent == null;
}
}
public virtual bool HasChildren
{
get
{
return Children.Count > 0;
}
}
}
public class Location : Entity
{
public Location()
{
InitMembers();
}
private void InitMembers()
{
Heirarchy = new LocationHeirarchy();
Metadata = new List<Metadata>();
}
public virtual string Name
{
get;
set;
}
public virtual string Route
{
get;
private set;
}
public virtual LocationHeirarchy Heirarchy
{
get;
private set;
}
public virtual IList<Metadata> Metadata
{
get;
private set;
}
}
public class LocationHeirarchy : Heirarchy<Location>
{
private string _token;
public virtual string Token
{
get
{
if (string.IsNullOrEmpty(_token))
{
var parent = this.Parent;
var tokenString = string.Empty;
while (parent != null)
{
tokenString += parent.Route;
if (parent.Heirarchy.Parent == null)
{
tokenString += "/";
}
parent = parent.Heirarchy.Parent;
}
_token = EncyptionUtils.MD5Hash(tokenString);
}
return _token;
}
private set
{
_token = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment