Created
January 8, 2013 14:43
-
-
Save anonymous/4484261 to your computer and use it in GitHub Desktop.
Expirable invariants in DDD
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
public class Expirable<T> : IValueObject<Expirable<T>> | |
{ | |
public T Value { get; protected set; } | |
public DateTime ValidTill { get; protected set; } | |
protected Expirable() | |
{ | |
} | |
public Expirable(T value, DateTime validTill) | |
{ | |
Value = value; | |
ValidTill = validTill; | |
} | |
} |
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
public class NHibernateRepository<T> : IRepository<T> where T : class, IEntity | |
{ | |
protected readonly ISession session; | |
public NHibernateRepository(ISession session) | |
{ | |
this.session = session; | |
} | |
protected void InvalidateExpirable<TExpirableValue>(Expression<Func<T, IEnumerable<Expirable<TExpirableValue>>>> selector, Action<T, Expirable<TExpirableValue>> remover) | |
{ | |
var memberName = selector.GetMemberName(); | |
var query = string.Format( | |
"select u as Entity, us.Value as ExpirableValue, us.ValidTill as ExpirableValidTill from {0} u join fetch u.{1} us where us.ValidTill < :now", | |
typeof(T).Name, | |
memberName | |
); | |
var entries = session | |
.CreateQuery(query) | |
.SetParameter("now", SystemTime.Now()) | |
.SetResultTransformer(Transformers.AliasToBean<ExpiredDto<T, TExpirableValue>>()) | |
.List<ExpiredDto<T, TExpirableValue>>(); | |
foreach (var entry in entries) | |
{ | |
remover(entry.Entity, entry.AsExpirable()); | |
} | |
} | |
} |
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
public class User : IEntity, IEquatable<User> | |
{ | |
private readonly IList<Expirable<UserSession>> sessions = new List<Expirable<UserSession>>(); | |
public virtual string Username { get; protected set; } | |
public virtual string Email { get; protected set; } | |
public virtual string Salt { get; protected set; } | |
public virtual string Password { get; protected set; } | |
public virtual IEnumerable<Expirable<UserSession>> Sessions { get { return sessions; } } | |
public virtual void RemoveSession(Expirable<UserSession> sess) | |
{ | |
if (Sessions == null || !Sessions.Any()) | |
{ | |
return; | |
} | |
Expirable<UserSession> userSession = sessions.FirstOrDefault(s => s.Equals(sess)); | |
if (userSession == null) | |
{ | |
return; | |
} | |
sessions.Remove(userSession); | |
DomainEvents.Raise( | |
new UserSessionTerminatedEvent( | |
userId: Id, | |
validTill: userSession.ValidTill, | |
ipAddress: userSession.Value.IpAddress, | |
started: userSession.Value.Started, | |
token: userSession.Value.Token | |
) | |
); | |
} |
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
public class UserRepository : NHibernateRepository<User> | |
{ | |
public UserRepository(ISession session) : base(session) | |
{ | |
InvalidateExpirable(x => x.Sessions, (e, exp) => e.RemoveSession(exp)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment