Created
April 12, 2013 13:54
-
-
Save caspian311/5372173 to your computer and use it in GitHub Desktop.
In nHibernate, when you soft delete entries and don't want to duplicated query logic to filter those out and don't want to add business rules into your hibernate configuration this is useful. Uses reflection to filter out the entities where the IsDeleted property is set to true. Makes for a manageable/testable solution. All nHibernate entities e…
This file contains hidden or 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
| namespace Sample.DataAccess | |
| { | |
| public abstract class Entity | |
| { | |
| public virtual int Id { get; set; } | |
| public virtual bool IsDeleted { get; set; } | |
| } | |
| public class Foo : Entity | |
| { | |
| public virtual string Name { get; set; } | |
| public virtual ICollection<Bar> Bars { get; set; } | |
| } | |
| public class Bar : Entity | |
| { | |
| public virtual string Name { get; set; } | |
| } | |
| } |
This file contains hidden or 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.Generic; | |
| using System.Linq; | |
| using Sample.DataAccess; | |
| namespace Sample.Web.Framework | |
| { | |
| public class EntityFilter : IEntityFilter | |
| { | |
| public IEnumerable<TEntity> Filter<TEntity>(IEnumerable<TEntity> collection) where TEntity : Entity | |
| { | |
| var trimmedCollection = collection.Where(entity => !entity.IsDeleted).ToList(); | |
| foreach (var item in trimmedCollection) | |
| { | |
| FilterChildren(item); | |
| } | |
| return trimmedCollection; | |
| } | |
| private static void FilterChildren(object entity) | |
| { | |
| foreach (var propertyInfo in entity.GetType().GetProperties() | |
| .Where(propertyInfo => propertyInfo.PropertyType.IsGenericType) | |
| .Where(propertyInfo => propertyInfo.PropertyType.GetGenericArguments().Single().BaseType == typeof(Entity))) | |
| { | |
| var itemType = propertyInfo.PropertyType.GetGenericArguments().Single(); | |
| var originalCollection = entity.GetType().GetProperty(propertyInfo.Name).GetValue(entity, null); | |
| if (originalCollection != null) | |
| { | |
| var trimmedCollection = ((IEnumerable<Entity>) originalCollection) | |
| .Where(item => !item.IsDeleted); | |
| var newCollectionType = typeof(List<>).MakeGenericType(new[] { itemType }); | |
| var newCollection = Activator.CreateInstance(newCollectionType); | |
| foreach (var item in trimmedCollection) | |
| { | |
| FilterChildren(item); | |
| var convertedItem = Convert.ChangeType(item, itemType); | |
| newCollection.GetType().GetMethod("Add").Invoke(newCollection, new[] { convertedItem }); | |
| } | |
| entity.GetType().GetProperty(propertyInfo.Name).SetValue(entity, newCollection, null); | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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.Collections.Generic; | |
| using System.Web.Http; | |
| using System.Web.Mvc; | |
| using Sample.DataAccess.Models; | |
| namespace Sample.Web.Controllers.Api | |
| { | |
| public class FooController : ApiController | |
| { | |
| private readonly IRepository _repository; | |
| private readonly IEntityFilter entityFilter; | |
| public FooController(IRepository repository, IEntityFilter entityFilter) | |
| { | |
| _repository = repository; | |
| _entityFilter = entityFilter; | |
| } | |
| public IEnumerable<Foo> Get() | |
| { | |
| return _entityFilter.Filter(_repository.QueryOver<Foo>()); | |
| } | |
| } | |
| } |
This file contains hidden or 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.Generic; | |
| using System.Linq.Expressions; | |
| namespace Sample.DataAccess | |
| { | |
| public class Repository : IRepository | |
| { | |
| protected readonly IConfiguration Config; | |
| public Repository(IConfiguration config) | |
| { | |
| Config = config; | |
| } | |
| public IEnumerable<T> All<T>() where T : Entity | |
| { | |
| return Config.Session.QueryOver<T>().List<T>(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment