Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 59 lines (51 sloc) 1.878 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
using System;
using System.Linq.Expressions;
using FubuCore.Reflection;
using FubuFastPack.Domain;

namespace FubuFastPack.Querying
{
    public class SingleEntityFilter<T> : IDataSourceFilter<T> where T : DomainEntity
    {
        private readonly T _domainEntity;

        public SingleEntityFilter(T domainEntity)
        {
            CanView = true;
            _domainEntity = domainEntity;
            DenyingRestriction = null;
        }

        public void WhereEqual(Expression<Func<T, object>> property, object value)
        {
            if (!CanView) return;
            var entityValue = ReflectionHelper.GetAccessor(property).GetValue(_domainEntity);

            if (ReferenceEquals(entityValue, null))
            {
                if (!ReferenceEquals(value, null)) CanView = false;
            }
            else
            {
                if (!entityValue.Equals(value)) CanView = false;
            }
        }

        public void WhereNotEqual(Expression<Func<T, object>> property, object value)
        {
            if (!CanView) return;
            var entityValue = ReflectionHelper.GetAccessor(property).GetValue(_domainEntity);
            if (ReferenceEquals(entityValue, null))
            {
                if (ReferenceEquals(value, null)) CanView = false;
            }
            else
            {
                if (entityValue.Equals(value)) CanView = false;
            }
        }

        public void ApplyRestriction(IDataRestriction<T> restriction)
        {
            restriction.Apply(this);
            if (!CanView && DenyingRestriction == null) DenyingRestriction = restriction;
        }

        // could be useful for diagnostics
        public IDataRestriction<T> DenyingRestriction { get; set; }

        public bool CanView { get; private set; }
    }
}
Something went wrong with that request. Please try again.