Skip to content

Commit

Permalink
Adding a factory for creating filters in a non-generic way (issue #25)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbelmont committed Jan 16, 2019
1 parent 1b1eae0 commit a16677e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
8 changes: 8 additions & 0 deletions ExpressionBuilder.Test/Unit/FilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ExpressionBuilder.Generics;
using ExpressionBuilder.Operations;
using ExpressionBuilder.Test.Models;
using FluentAssertions;
using NUnit.Framework;
using System;
using System.Linq;
Expand Down Expand Up @@ -77,5 +78,12 @@ public void ShouldNotStartGroupIfPreviousOneIsEmpty()
filter.StartGroup();
Assert.That(filter.Statements.Count(), Is.EqualTo(1));
}

[TestCase(TestName = "Should create a filter by passing the type as an argument")]
public void ShouldCreateAFilterByPassingTheTypeAsAnArgument()
{
var filter = FilterFactory.Create(typeof(Person));
filter.Should().BeOfType(typeof(Filter<Person>));
}
}
}
3 changes: 2 additions & 1 deletion ExpressionBuilder/ExpressionBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@
<Compile Include="Exceptions\PropertyValueTypeMismatchException.cs" />
<Compile Include="Exceptions\WrongNumberOfValuesException.cs" />
<Compile Include="Exceptions\UnsupportedOperationException.cs" />
<Compile Include="Generics\FilterStatement.cs" />
<Compile Include="FilterFactory.cs" />
<Compile Include="Generics\Filter.cs" />
<Compile Include="Generics\FilterStatement.cs" />
<Compile Include="Generics\FilterStatementConnection.cs" />
<Compile Include="Helpers\OperationEqualityComparer.cs" />
<Compile Include="Helpers\OperationHelper.cs" />
Expand Down
24 changes: 24 additions & 0 deletions ExpressionBuilder/FilterFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using ExpressionBuilder.Generics;
using ExpressionBuilder.Interfaces;
using System;

namespace ExpressionBuilder
{
/// <summary>
/// </summary>
public static class FilterFactory
{
/// <summary>
/// Creates a Filter&lt;TClass&gt; by passing the 'TClass' as a parameter.
/// </summary>
/// <param name="type"></param>
/// <typeparam name="TClass"></typeparam>
/// <returns></returns>
public static IFilter Create(Type type)
{
Type[] typeArgs = { type };
var filterType = typeof(Filter<>).MakeGenericType(typeArgs);
return (IFilter)Activator.CreateInstance(filterType);
}
}
}

0 comments on commit a16677e

Please sign in to comment.