Skip to content

Commit

Permalink
add ValueResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-sedykh committed Jul 21, 2015
1 parent f869f31 commit 0d68369
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 196 deletions.
15 changes: 2 additions & 13 deletions Filter/IFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Linq.Expressions;

public interface IFilter : IFilterView
{
Expand All @@ -29,17 +28,7 @@ public interface IFilter : IFilterView
/// </summary>
/// <typeparam name="TModel">Type.</typeparam>
/// <returns>Expression tree.</returns>
Expression<Func<TModel, bool>> BuildExpression<TModel>();

/// <summary>
/// Set dictionary of available values.
/// </summary>
void SetDictionary(Func<IEnumerable<SelectListItem>> dictionary);

/// <summary>
/// Set dictionary of available values.
/// </summary>
void SetDictionary(IQueryable<SelectListItem> dictionary);
Expression<Func<TModel, bool>> BuildExpression<TModel>();

/// <summary>
/// hide/show filter.
Expand Down
10 changes: 4 additions & 6 deletions Filter/IFilterView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System.Collections.Generic;
using System.Web.Mvc;
using System;

public interface IFilterView
{
Expand All @@ -15,10 +16,7 @@ public interface IFilterView
/// </summary>
string TemplateName { get; set; }

/// <summary>
/// Dictionary of available values
/// </summary>
IEnumerable<SelectListItem> Dictionary { get; }
Func<IEnumerable<SelectListItem>> ValueResolver { get; }

/// <summary>
/// list of conditions
Expand Down Expand Up @@ -70,6 +68,6 @@ public interface IFilterView
/// <summary>
/// Display value format
/// </summary>
string ValueFormat { get; set; }
string ValueFormat { get; set; }
}
}
}
1 change: 0 additions & 1 deletion Filter/ReferenceTypeFilter/IReferenceTypeCondition.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// ReSharper disable CheckNamespace
namespace Sprint.Filter
// ReSharper restore CheckNamespace
{
using System;
using System.Linq.Expressions;
Expand Down
10 changes: 5 additions & 5 deletions Filter/ReferenceTypeFilter/IReferenceTypeFilter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using Sprint.Helpers;
// ReSharper disable CheckNamespace


// ReSharper disable CheckNamespace
namespace Sprint.Filter
// ReSharper restore CheckNamespace
{
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using Helpers;

public interface IReferenceTypeFilter<TModel, TProperty> : IFilter where TProperty : class, IComparable, IComparable<TProperty>, IEquatable<TProperty>
{
Expand All @@ -28,5 +26,7 @@ void ConditionBuilder<T>(Func<
void SetDefaultValue(IFilterValue<TProperty> filterValue);

void Init(IFilterValue<TProperty> filterValue);

void SetValueResolver(Func<IFilterValue<TProperty>, IEnumerable<SelectListItem>> valueResolver);
}
}
22 changes: 4 additions & 18 deletions Filter/ReferenceTypeFilter/IReferenceTypeFilterConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using Sprint.Helpers;
// ReSharper disable CheckNamespace


// ReSharper disable CheckNamespace
namespace Sprint.Filter
// ReSharper restore CheckNamespace
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using Helpers;
using System.Web.Mvc;
using System.Linq.Expressions;

Expand Down Expand Up @@ -44,17 +40,7 @@ public interface IReferenceTypeFilterConfiguration<TModel, TProperty> where TPro
/// <param name="tempaleName">Template name.</param>
IReferenceTypeFilterConfiguration<TModel, TProperty> SetTemplate(string tempaleName);

/// <summary>
/// Set dictionary of available values.
/// </summary>
/// <param name="dictionary">Available filter values.</param>
IReferenceTypeFilterConfiguration<TModel, TProperty> SetDictionary(Func<IEnumerable<SelectListItem>> dictionary);

/// <summary>
/// Set dictionary of available values.
/// </summary>
/// <param name="dictionary">Available filter values.</param>
IReferenceTypeFilterConfiguration<TModel, TProperty> SetDictionary(IQueryable<SelectListItem> dictionary);
IReferenceTypeFilterConfiguration<TModel, TProperty> SetValueResolver(Func<IFilterValue<TProperty>, IEnumerable<SelectListItem>> valueResolver);

/// <summary>
/// Set filter display name.
Expand Down
46 changes: 23 additions & 23 deletions Filter/ReferenceTypeFilter/ReferenceTypeFilter.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
using Sprint.Helpers;
// ReSharper disable CheckNamespace


// ReSharper disable CheckNamespace
namespace Sprint.Filter
// ReSharper restore CheckNamespace
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc;
using Helpers;

public sealed class ReferenceTypeFilter<TModel,TProperty>:IReferenceTypeFilter<TModel, TProperty> where TProperty : class, IComparable, IComparable<TProperty>, IEquatable<TProperty>
{
private readonly IDictionary<string, IReferenceTypeCondition<TProperty>> _conditions;
private bool _isVisible;
private Type _returnModelType;
private readonly string _typeName;
private Lazy<IEnumerable<SelectListItem>> _lazyDictionary;
private IQueryable<SelectListItem> _dictionary;
private Func<IFilterValue<TProperty>,
Func<IFilterValue<TProperty>, LambdaExpressionDecorator<Func<TModel, bool>>>,
string, object> _expressionBuilder;

private IFilterValue<TProperty> _defaultFilterValue;
private IFilterValue<TProperty> _initFilterValue;
private Lazy<IFilterValue<TProperty>> _defaultLazyFilterValue;

private Func<IFilterValue<TProperty>, IEnumerable<SelectListItem>> _valueResolver;
private Func<IReferenceTypeCondition<TProperty>, IFilterValue<TProperty>, Expression<Func<TModel, bool>>> _conditionInvoker;

private IFilterValue<TProperty> _filterValue;
Expand Down Expand Up @@ -92,9 +87,9 @@ string IFilterView.TypeName

string IFilterView.TemplateName { get; set; }

IEnumerable<SelectListItem> IFilterView.Dictionary
Func<IEnumerable<SelectListItem>> IFilterView.ValueResolver
{
get { return _dictionary ?? (_lazyDictionary != null ? _lazyDictionary.Value : null); }
get { return ValueResolver; }
}

IEnumerable<SelectListItem> IFilterView.GetConditions()
Expand All @@ -120,6 +115,11 @@ public void Init(IFilterValue<TProperty> value)
_initFilterValue = value;
}

public void SetValueResolver(Func<IFilterValue<TProperty>, IEnumerable<SelectListItem>> valueResolver)
{
_valueResolver = valueResolver;
}

void IFilter.Init(IFilterValue value)
{
Init(value as IFilterValue<TProperty>);
Expand Down Expand Up @@ -185,17 +185,7 @@ public void SetDefaultValue(IFilterValue<TProperty> filterValue)
_filterValue = null;

_defaultFilterValue = filterValue;
}

public void SetDictionary(Func<IEnumerable<SelectListItem>> dictionary)
{
_lazyDictionary = new Lazy<IEnumerable<SelectListItem>>(dictionary);
}

public void SetDictionary(IQueryable<SelectListItem> dictionary)
{
_dictionary = dictionary;
}
}

public void Visible(bool isVisible)
{
Expand Down Expand Up @@ -267,6 +257,16 @@ private IFilterValue<TProperty> GetFilterValue()
private KeyValuePair<string, IReferenceTypeCondition<TProperty>> GetCondition(IFilterValue filterValue)
{
return _conditions.FirstOrDefault(x => x.Key == filterValue.ConditionKey);
}
}

private IEnumerable<SelectListItem> ValueResolver()
{
if (_valueResolver == null)
return null;

var value = GetFilterValue();

return _valueResolver(value);
}
}
}
23 changes: 6 additions & 17 deletions Filter/ReferenceTypeFilter/ReferenceTypeFilterConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using Sprint.Helpers;
// ReSharper disable CheckNamespace


// ReSharper disable CheckNamespace
namespace Sprint.Filter
// ReSharper restore CheckNamespace
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using Helpers;
using System.Linq.Expressions;
using System.Web.Mvc;

Expand Down Expand Up @@ -56,19 +52,12 @@ public IReferenceTypeFilterConfiguration<TModel, TProperty> SetTemplate(string t
return this;
}

public IReferenceTypeFilterConfiguration<TModel, TProperty> SetDictionary(Func<IEnumerable<SelectListItem>> dictionary)
{
_filter.SetDictionary(dictionary);

return this;
}

public IReferenceTypeFilterConfiguration<TModel, TProperty> SetDictionary(IQueryable<SelectListItem> dictionary)
public IReferenceTypeFilterConfiguration<TModel, TProperty> SetValueResolver(Func<IFilterValue<TProperty>, IEnumerable<SelectListItem>> valueResolver)
{
_filter.SetDictionary(dictionary);
_filter.SetValueResolver(valueResolver);

return this;
}
}

public IReferenceTypeFilterConfiguration<TModel, TProperty> SetTitle(string title)
{
Expand Down
7 changes: 5 additions & 2 deletions Filter/ValueTypeFilter/IValueTypeFilter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sprint.Helpers;
using System.Web.Mvc;
using Sprint.Helpers;
// ReSharper disable CheckNamespace


Expand All @@ -25,8 +26,10 @@ void ConditionBuilder<T>(Func<

void SetDefaultValue(Func<IFilterValue<TProperty?>> filterValue);

void SetDefaultValue(IFilterValue<TProperty?> filterValue);
void SetDefaultValue(IFilterValue<TProperty?> filterValue);

void Init(IFilterValue<TProperty?> filterValue);

void SetValueResolver(Func<IFilterValue<TProperty?>, IEnumerable<SelectListItem>> dictionary);
}
}
14 changes: 2 additions & 12 deletions Filter/ValueTypeFilter/IValueTypeFilterConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,9 @@ public interface IValueTypeFilterConfiguration<TModel, TProperty> where TPropert
/// Set filter template.
/// </summary>
/// <param name="tempaleName">Template name.</param>
IValueTypeFilterConfiguration<TModel, TProperty> SetTemplate(string tempaleName);
IValueTypeFilterConfiguration<TModel, TProperty> SetTemplate(string tempaleName);

/// <summary>
/// Set dictionary of available values.
/// </summary>
/// <param name="dictionary">Available filter values.</param>
IValueTypeFilterConfiguration<TModel, TProperty> SetDictionary(Func<IEnumerable<SelectListItem>> dictionary);

/// <summary>
/// Set dictionary of available values.
/// </summary>
/// <param name="dictionary">Available filter values.</param>
IValueTypeFilterConfiguration<TModel, TProperty> SetDictionary(IQueryable<SelectListItem> dictionary);
IValueTypeFilterConfiguration<TModel, TProperty> SetValueResolver(Func<IFilterValue<TProperty?>, IEnumerable<SelectListItem>> valueResolver);

/// <summary>
/// Set filter display name.
Expand Down
38 changes: 20 additions & 18 deletions Filter/ValueTypeFilter/ValueTypeFilter.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
using Sprint.Helpers;
// ReSharper disable CheckNamespace


// ReSharper disable CheckNamespace
namespace Sprint.Filter
// ReSharper restore CheckNamespace
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc;
using Helpers;

public sealed class ValueTypeFilter<TModel,TProperty>:IValueTypeFilter<TModel, TProperty> where TProperty : struct, IComparable, IComparable<TProperty>, IEquatable<TProperty>
{
private readonly IDictionary<string, IValueTypeCondition<TProperty>> _conditions;
private bool _isVisible;
private Type _returnModelType;
private readonly string _typeName;
private Lazy<IEnumerable<SelectListItem>> _lazyDictionary;
private IQueryable<SelectListItem> _dictionary;
private Func<IFilterValue<TProperty?>, IEnumerable<SelectListItem>> _valueResolver;
private Func<
IFilterValue<TProperty?>,
Func<IFilterValue<TProperty?>, LambdaExpressionDecorator<Func<TModel, bool>>>,
Expand Down Expand Up @@ -85,15 +81,15 @@ string IFilterView.TypeName
get { return _typeName; }
}

string IFilterView.ValueFormat { get; set; }
string IFilterView.ValueFormat { get; set; }

string IFilterView.Title { get; set; }

string IFilterView.TemplateName { get; set; }

IEnumerable<SelectListItem> IFilterView.Dictionary
Func<IEnumerable<SelectListItem>> IFilterView.ValueResolver
{
get { return _dictionary ?? (_lazyDictionary != null ? _lazyDictionary.Value : null); }
get { return ValueResolver; }
}

IEnumerable<SelectListItem> IFilterView.GetConditions()
Expand Down Expand Up @@ -186,15 +182,11 @@ public void SetDefaultValue(IFilterValue<TProperty?> filterValue)
_defaultFilterValue = filterValue;
}

public void SetDictionary(Func<IEnumerable<SelectListItem>> dictionary)
{
_lazyDictionary = new Lazy<IEnumerable<SelectListItem>>(dictionary);
}

public void SetDictionary(IQueryable<SelectListItem> dictionary)
public void SetValueResolver(Func<IFilterValue<TProperty?>, IEnumerable<SelectListItem>> valueResolver)
{
_dictionary = dictionary;
}
_valueResolver = valueResolver;
}

public void Visible(bool isVisible)
{
Expand Down Expand Up @@ -265,5 +257,15 @@ private KeyValuePair<string, IValueTypeCondition<TProperty>> GetCondition(IFilte
{
return _conditions.FirstOrDefault(x => x.Key == filterValue.ConditionKey);
}

private IEnumerable<SelectListItem> ValueResolver()
{
if (_valueResolver == null)
return null;

var value = GetFilterValue();

return _valueResolver(value);
}
}
}
Loading

0 comments on commit 0d68369

Please sign in to comment.