Created
May 20, 2010 17:13
-
-
Save anonymous/407818 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using NHibernate; | |
using NHibernate.Linq; | |
public interface IEntity<TIdentity> | |
{ | |
TIdentity Id { get; } | |
} | |
public interface IRepository<TEntity, TIdentity> : IQueryable<TEntity>, ICollection<TEntity>, | |
IDictionary<TIdentity, TEntity> | |
where TEntity : class, IEntity<TIdentity> | |
{ | |
TEntity Load(TIdentity id); | |
void Refresh(TEntity entity); | |
new void Clear(); | |
new int Count { get; } | |
new bool IsReadOnly { get; } | |
new IEnumerator<TEntity> GetEnumerator(); | |
} | |
public class Repository<TEntity, TIdentity> : IRepository<TEntity, TIdentity> | |
where TEntity : class, IEntity<TIdentity> | |
{ | |
readonly ISessionFactory _factory; | |
public Repository(ISessionFactory factory) | |
{ | |
_factory = factory; | |
} | |
protected ISession Session | |
{ | |
get { return _factory.GetCurrentSession(); } | |
} | |
protected IQueryable<TEntity> Queryable | |
{ | |
get { return Session.Query<TEntity>(); } | |
} | |
#region IEnumerable | |
IEnumerator<KeyValuePair<TIdentity, TEntity>> | |
IEnumerable<KeyValuePair<TIdentity, TEntity>>.GetEnumerator() | |
{ | |
return Queryable.ToDictionary(e => e.Id).GetEnumerator(); | |
} | |
public IEnumerator<TEntity> GetEnumerator() | |
{ | |
return Queryable.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
#endregion | |
#region IQueryable | |
public Expression Expression | |
{ | |
get { return Queryable.Expression; } | |
} | |
public Type ElementType | |
{ | |
get { return Queryable.ElementType; } | |
} | |
public IQueryProvider Provider | |
{ | |
get { return Queryable.Provider; } | |
} | |
#endregion | |
#region ICollection | |
public void Add(TEntity item) | |
{ | |
Session.Save(item); | |
} | |
public void Add(KeyValuePair<TIdentity, TEntity> item) | |
{ | |
Add(item.Key, item.Value); | |
} | |
public void Clear() | |
{ | |
Session.CreateQuery(string.Format("delete {0}", typeof(TEntity).Name)) | |
.ExecuteUpdate(); | |
} | |
public bool Contains(KeyValuePair<TIdentity, TEntity> item) | |
{ | |
return Contains(item.Value); | |
} | |
public void CopyTo(KeyValuePair<TIdentity, TEntity>[] array, int arrayIndex) | |
{ | |
throw new NotImplementedException(); | |
} | |
public bool Remove(KeyValuePair<TIdentity, TEntity> item) | |
{ | |
return Remove(item); | |
} | |
public bool Contains(TEntity item) | |
{ | |
return Queryable.Contains(item); | |
} | |
public void CopyTo(TEntity[] array, int arrayIndex) | |
{ | |
throw new NotImplementedException(); | |
} | |
public bool Remove(TEntity item) | |
{ | |
Session.Delete(item); | |
return true; | |
} | |
public int Count | |
{ | |
get { return Queryable.Count(); } | |
} | |
public bool IsReadOnly | |
{ | |
get { return false; } | |
} | |
#endregion | |
#region IDictionary | |
public bool ContainsKey(TIdentity id) | |
{ | |
var param = Expression.Parameter(typeof(TEntity), "e"); | |
var matchId = Expression.Lambda<Func<TEntity, bool>>( | |
Expression.Equal(Expression.Property(param, "Id"), | |
Expression.Constant(id)), | |
param); | |
return Queryable.Count(matchId) > 0; | |
} | |
public void Add(TIdentity id, TEntity value) | |
{ | |
this[id] = value; | |
} | |
public bool Remove(TIdentity id) | |
{ | |
return Session.CreateQuery(string.Format("delete {0} where id = :id", typeof(TEntity).Name)) | |
.SetParameter("id", id) | |
.ExecuteUpdate() > 0; | |
} | |
public bool TryGetValue(TIdentity id, out TEntity value) | |
{ | |
value = this[id]; | |
return value != null; | |
} | |
public TEntity this[TIdentity id] | |
{ | |
get { return Session.Get<TEntity>(id); } | |
set { Session.Save(value, id); } | |
} | |
public ICollection<TIdentity> Keys | |
{ | |
get { return Queryable.Select(e => e.Id).ToList(); } | |
} | |
public ICollection<TEntity> Values | |
{ | |
get { return Queryable.ToList(); } | |
} | |
#endregion | |
#region IRepository | |
public TEntity Load(TIdentity id) | |
{ | |
return Session.Load<TEntity>(id); | |
} | |
public void Refresh(TEntity entity) | |
{ | |
if (Equals(entity.Id, default(TIdentity))) | |
return; | |
Session.Evict(entity); | |
Session.Load(entity, entity.Id); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment