Add project files.

This commit is contained in:
David 2018-02-11 19:55:29 -06:00
parent f51b9350e4
commit 50fc50225d
23 changed files with 988 additions and 0 deletions

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>

View File

@ -0,0 +1,41 @@
using PoweredSoft.DynamicLinq.Dal.Configurations;
using PoweredSoft.DynamicLinq.Dal.Pocos;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Dal
{
public class BlogContext : DbContext
{
public DbSet<Author> Authors { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Post> Posts { get; set; }
static BlogContext()
{
Database.SetInitializer<BlogContext>(new DropCreateDatabaseAlways<BlogContext>());
}
public BlogContext()
{
}
public BlogContext(string connectionString) : base(connectionString)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new AuthorConfiguration());
modelBuilder.Configurations.Add(new CommentConfiguration());
modelBuilder.Configurations.Add(new PostConfiguration());
}
}
}

View File

@ -0,0 +1,73 @@
using PoweredSoft.DynamicLinq.Dal.Pocos;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Dal.Configurations
{
public class AuthorConfiguration : EntityTypeConfiguration<Author>
{
public AuthorConfiguration() : this("dbo")
{
}
public AuthorConfiguration(string schema)
{
ToTable("Author", schema);
HasKey(t => t.Id);
Property(t => t.Id).HasColumnName("Id").HasColumnType("bigint").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(t => t.FirstName).HasColumnType("nvarchar").HasMaxLength(50).IsRequired();
Property(t => t.LastName).HasColumnType("nvarchar").HasMaxLength(50).IsRequired();
}
}
public class PostConfiguration : EntityTypeConfiguration<Post>
{
public PostConfiguration() : this("dbo")
{
}
public PostConfiguration(string schema)
{
ToTable("Post", schema);
HasKey(t => t.Id);
Property(t => t.Id).HasColumnName("Id").HasColumnType("bigint").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(t => t.AuthorId).HasColumnName("AuthorId").HasColumnType("bigint").IsRequired();
Property(t => t.Title).HasColumnName("Title").HasColumnType("nvarchar").HasMaxLength(100).IsRequired();
Property(t => t.Content).HasColumnName("Content").HasColumnType("nvarchar(max)").IsRequired();
Property(t => t.CreateTime).HasColumnName("CreateTime").HasColumnType("datetimeoffset").IsRequired();
Property(t => t.PublishTime).HasColumnName("PublishTime").HasColumnType("datetimeoffset").IsOptional();
HasRequired(t => t.Author).WithMany(t => t.Posts).HasForeignKey(t => t.AuthorId).WillCascadeOnDelete(false);
}
}
public class CommentConfiguration : EntityTypeConfiguration<Comment>
{
public CommentConfiguration() : this("dbo")
{
}
public CommentConfiguration(string schema)
{
ToTable("Comment", schema);
HasKey(t => t.Id);
Property(t => t.Id).HasColumnName("Id").HasColumnType("bigint").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(t => t.ParentCommentId).HasColumnName("ParentCommentId").HasColumnType("bigint").IsOptional();
Property(t => t.PostId).HasColumnName("PostId").HasColumnType("bigint").IsRequired();
Property(t => t.DisplayName).HasColumnName("DisplayName").HasColumnType("nvarchar").HasMaxLength(100).IsRequired();
Property(t => t.Email).HasColumnName("Email").HasColumnType("nvarchar").IsOptional();
Property(t => t.CommentText).HasColumnName("CommentText").HasColumnType("nvarchar").HasMaxLength(255).IsOptional();
HasRequired(t => t.Post).WithMany(t => t.Comments).HasForeignKey(t => t.PostId).WillCascadeOnDelete(false);
HasOptional(t => t.ParentComment).WithMany(t => t.Comments).HasForeignKey(t => t.ParentCommentId).WillCascadeOnDelete(false);
}
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Dal.Pocos
{
public class Author
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Post> Posts { get; set; }
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Dal.Pocos
{
public class Comment
{
public long Id { get; set; }
public long? ParentCommentId { get; set; }
public long PostId { get; set; }
public string DisplayName { get; set; }
public string Email { get; set; }
public string CommentText { get; set; }
public Comment ParentComment { get; set; }
public ICollection<Comment> Comments { get; set; }
public Post Post { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Dal.Pocos
{
public class Post
{
public long Id { get; set; }
public long AuthorId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTimeOffset CreateTime { get; set; }
public DateTimeOffset? PublishTime { get; set; }
public Author Author { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
}

View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C16927E7-1358-4B9D-BDD7-149E505DE6CC}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PoweredSoft.DynamicLinq.Dal</RootNamespace>
<AssemblyName>PoweredSoft.DynamicLinq.Dal</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll</HintPath>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BlogContext.cs" />
<Compile Include="Configurations\Configurations.cs" />
<Compile Include="Pocos\Post.cs" />
<Compile Include="Pocos\Author.cs" />
<Compile Include="Pocos\Comment.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("PoweredSoft.DynamicLinq.Dal")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PoweredSoft.DynamicLinq.Dal")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c16927e7-1358-4b9d-bdd7-149e505de6cc")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.2.0" targetFramework="net461" />
</packages>

View File

@ -0,0 +1,35 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PoweredSoft.DynamicLinq.Dal.Pocos;
using PoweredSoft.DynamicLinq.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Test
{
[TestClass]
public class ComplexQueryTest
{
[TestMethod]
public void ComplexQueryBuilder()
{
// subject.
var authors = new List<Post>()
{
new Post { Id = 1, AuthorId = 1, Title = "Hello 1", Content = "World" },
new Post { Id = 2, AuthorId = 1, Title = "Hello 2", Content = "World" },
new Post { Id = 3, AuthorId = 2, Title = "Hello 3", Content = "World" },
};
// the query.
var query = authors.AsQueryable();
query = query.Query(q => q
.Compare("AuthorId", ConditionOperators.Equal, 1)
.Or("AuthorId", ConditionOperators.Equal, 2)
);
}
}
}

View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{6F5C80F0-9045-4098-913F-7BDAD135E6DD}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PoweredSoft.DynamicLinq.Test</RootNamespace>
<AssemblyName>PoweredSoft.DynamicLinq.Test</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Faker, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Faker.Data.1.0.7\lib\net45\Faker.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="ComplexQueryTest.cs" />
<Compile Include="QueryTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PoweredSoft.DynamicLinq.Dal\PoweredSoft.DynamicLinq.Dal.csproj">
<Project>{C16927E7-1358-4B9D-BDD7-149E505DE6CC}</Project>
<Name>PoweredSoft.DynamicLinq.Dal</Name>
</ProjectReference>
<ProjectReference Include="..\PoweredSoft.DynamicLinq\PoweredSoft.DynamicLinq.csproj">
<Project>{2abc5a60-b549-4ecd-bef4-31ca7ba4ef06}</Project>
<Name>PoweredSoft.DynamicLinq</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets')" />
</Project>

View File

@ -0,0 +1,20 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("PoweredSoft.DynamicLinq.Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PoweredSoft.DynamicLinq.Test")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("6f5c80f0-9045-4098-913f-7bdad135e6dd")]
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PoweredSoft.DynamicLinq.Dal.Pocos;
using PoweredSoft.DynamicLinq.Extensions;
namespace PoweredSoft.DynamicLinq.Test
{
[TestClass]
public class QueryTests
{
[TestMethod]
public void Equal()
{
// subject.
var authors = new List<Author>()
{
new Author { Id = long.MaxValue, FirstName = "David", LastName = "Lebee" }
};
// the query.
var query = authors.AsQueryable();
// simple where.
var newQuery = query.Where("FirstName", ConditionOperators.Equal, "David");
// must match.
Assert.IsTrue(newQuery.Any(), "Must have at least one author that matches");
}
[TestMethod]
public void Contains()
{
// subject.
var authors = new List<Author>()
{
new Author { Id = long.MaxValue, FirstName = "David", LastName = "Lebee" }
};
// the query.
var query = authors.AsQueryable();
// simple where.
var newQuery = query.Where("FirstName", ConditionOperators.Contains, "Da");
// must match.
Assert.IsTrue(newQuery.Any(), "Must have at least one author that matches");
}
[TestMethod]
public void StartsWith()
{
// subject.
var authors = new List<Author>()
{
new Author { Id = long.MaxValue, FirstName = "David", LastName = "Lebee" }
};
// the query.
var query = authors.AsQueryable();
// simple where.
var newQuery = query.Where("FirstName", ConditionOperators.StartsWith, "Da");
// must match.
Assert.IsTrue(newQuery.Any(), "Must have at least one author that matches");
}
[TestMethod]
public void EndsWith()
{
// subject.
var authors = new List<Author>()
{
new Author { Id = long.MaxValue, FirstName = "David", LastName = "Lebee" }
};
// the query.
var query = authors.AsQueryable();
// simple where.
var newQuery = query.Where("FirstName", ConditionOperators.EndsWith, "Da");
// must match.
Assert.IsFalse(newQuery.Any(), "Not suppose to find any matches");
}
[TestMethod]
public void LessThen()
{
// subject.
var authors = new List<Post>()
{
new Post { Id = 1, AuthorId = 1, Title = "Hello 1", Content = "World" },
new Post { Id = 2, AuthorId = 1, Title = "Hello 2", Content = "World" },
new Post { Id = 3, AuthorId = 2, Title = "Hello 3", Content = "World" },
};
// the query.
var query = authors.AsQueryable();
// simple where.
var newQuery = query.Where("AuthorId", ConditionOperators.LessThan, 2);
// must match.
Assert.AreEqual(2, newQuery.Count());
}
[TestMethod]
public void GreaterThanOrEqual()
{
// subject.
var authors = new List<Post>()
{
new Post { Id = 1, AuthorId = 1, Title = "Hello 1", Content = "World" },
new Post { Id = 2, AuthorId = 1, Title = "Hello 2", Content = "World" },
new Post { Id = 3, AuthorId = 2, Title = "Hello 3", Content = "World" },
};
// the query.
var query = authors.AsQueryable();
// simple where.
var newQuery = query.Where("AuthorId", ConditionOperators.GreaterThanOrEqual, 2);
// must match.
Assert.AreEqual(1, newQuery.Count());
}
}
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Faker.Data" version="1.0.7" targetFramework="net461" />
<package id="MSTest.TestAdapter" version="1.2.0" targetFramework="net461" />
<package id="MSTest.TestFramework" version="1.2.0" targetFramework="net461" />
</packages>

View File

@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoweredSoft.DynamicLinq", "PoweredSoft.DynamicLinq\PoweredSoft.DynamicLinq.csproj", "{2ABC5A60-B549-4ECD-BEF4-31CA7BA4EF06}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoweredSoft.DynamicLinq.Dal", "PoweredSoft.DynamicLinq.Dal\PoweredSoft.DynamicLinq.Dal.csproj", "{C16927E7-1358-4B9D-BDD7-149E505DE6CC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoweredSoft.DynamicLinq.Test", "PoweredSoft.DynamicLinq.Test\PoweredSoft.DynamicLinq.Test.csproj", "{6F5C80F0-9045-4098-913F-7BDAD135E6DD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2ABC5A60-B549-4ECD-BEF4-31CA7BA4EF06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2ABC5A60-B549-4ECD-BEF4-31CA7BA4EF06}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2ABC5A60-B549-4ECD-BEF4-31CA7BA4EF06}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2ABC5A60-B549-4ECD-BEF4-31CA7BA4EF06}.Release|Any CPU.Build.0 = Release|Any CPU
{C16927E7-1358-4B9D-BDD7-149E505DE6CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C16927E7-1358-4B9D-BDD7-149E505DE6CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C16927E7-1358-4B9D-BDD7-149E505DE6CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C16927E7-1358-4B9D-BDD7-149E505DE6CC}.Release|Any CPU.Build.0 = Release|Any CPU
{6F5C80F0-9045-4098-913F-7BDAD135E6DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F5C80F0-9045-4098-913F-7BDAD135E6DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F5C80F0-9045-4098-913F-7BDAD135E6DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F5C80F0-9045-4098-913F-7BDAD135E6DD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DC29EF19-8131-41DC-943F-813CF103F7E9}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq
{
public enum ConditionOperators
{
Equal,
GreaterThan,
GreaterThanOrEqual,
LessThan,
LessThanOrEqual,
Contains,
StartsWith,
EndsWith
}
internal static class Constants
{
internal static readonly MethodInfo ContainsMethod = typeof(string).GetMethod("Contains");
internal static readonly MethodInfo StartsWithMethod = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
internal static readonly MethodInfo EndsWithMethod = typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) });
}
}

View File

@ -0,0 +1,28 @@
using PoweredSoft.DynamicLinq.Fluent;
using PoweredSoft.DynamicLinq.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Extensions
{
public static class QueryableExtensions
{
public static IQueryable<T> Where<T>(this IQueryable<T> query, string path, ConditionOperators conditionOperator, object value, bool convertConstantToLeftOperator = true)
{
query = query.Query(qb => qb.Compare(path, conditionOperator, value, convertConstantToLeftOperator: convertConstantToLeftOperator));
return query;
}
public static IQueryable<T> Query<T> (this IQueryable<T> query, Action<QueryBuilder<T>> callback)
{
var queryBuilder = new QueryBuilder<T>(query);
callback(queryBuilder);
var ret = queryBuilder.Build();
return ret;
}
}
}

View File

@ -0,0 +1,107 @@
using PoweredSoft.DynamicLinq.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Fluent
{
public class QueryBuilder<T>
{
public IQueryable<T> Query { get; set; }
public Type QueryableType { get; set; }
public List<QueryFilterPart> Parts { get; protected set; } = new List<QueryFilterPart>();
public QueryBuilder(IQueryable<T> query)
{
Query = query;
}
public QueryBuilder<T> Compare(string path, ConditionOperators conditionOperators, object value,
bool convertConstantToLeftOperator = true, bool and = true)
{
Parts.Add(new QueryFilterPart
{
And = and,
ConditionOperator = conditionOperators,
Path = path,
Value = value,
ConvertConstantToLeftOperator = convertConstantToLeftOperator
});
return this;
}
public QueryBuilder<T> SubQuery(Action<QueryBuilder<T>> subQuery, bool and = true)
{
// create query builder for same type.
var qb = new QueryBuilder<T>(Query);
// callback.
subQuery(qb);
// create a query part.
var part = new QueryFilterPart();
part.And = and;
part.Parts = qb.Parts;
Parts.Add(part);
//return self.
return this;
}
public QueryBuilder<T> And(string path, ConditionOperators conditionOperator, object value, bool convertConstantToLeftOperator = true)
=> Compare(path, conditionOperator, value, convertConstantToLeftOperator: convertConstantToLeftOperator, and: true);
public QueryBuilder<T> Or(string path, ConditionOperators conditionOperator, object value, bool convertConstantToLeftOperator = true)
=> Compare(path, conditionOperator, value, convertConstantToLeftOperator: convertConstantToLeftOperator, and: false);
public QueryBuilder<T> And(Action<QueryBuilder<T>> subQuery)
=> SubQuery(subQuery, true);
public QueryBuilder<T> Or(Action<QueryBuilder<T>> subQuery)
=> SubQuery(subQuery, false);
public IQueryable<T> Build()
{
var parameter = Expression.Parameter(typeof(T), "t");
var expression = BuildExpression(parameter, Parts);
var lambda = Expression.Lambda<Func<T, bool>>(expression, parameter);
var query = Query.Where(lambda);
return query;
}
protected Expression BuildExpression(ParameterExpression parameter, List<QueryFilterPart> parts)
{
Expression ret = null;
parts.ForEach(part =>
{
Expression innerExpression;
if (part.Parts?.Any() == true)
innerExpression = BuildExpression(parameter, part.Parts);
else
innerExpression = BuildExpression(parameter, part);
if (ret != null)
ret = part.And ? Expression.And(ret, innerExpression) : Expression.Or(ret, innerExpression);
else
ret = innerExpression;
});
return ret;
}
private Expression BuildExpression(ParameterExpression parameter, QueryFilterPart part)
{
var member = QueryableHelpers.ResolvePathForExpression(parameter, part.Path);
var constant = part.ConvertConstantToLeftOperator ? QueryableHelpers.GetConstantSameAsLeftOperator(member, part.Value) : Expression.Constant(part.Value);
var expression = QueryableHelpers.GetConditionExpressionForMember(parameter, member, part.ConditionOperator, constant);
return expression;
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Fluent
{
public class QueryFilterPart
{
public string Path { get; set; }
public ConditionOperators ConditionOperator { get; set; }
public object Value { get; set; }
public bool And { get; set; }
public bool ConvertConstantToLeftOperator { get; set; }
public List<QueryFilterPart> Parts { get; set; } = new List<QueryFilterPart>();
}
}

View File

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Helpers
{
public static class QueryableHelpers
{
public static Expression GetConditionExpressionForMember(ParameterExpression parameter, Expression member, ConditionOperators conditionOperator, ConstantExpression constant)
{
if (parameter == null)
throw new ArgumentNullException("parameter");
if (member == null)
throw new ArgumentNullException("member");
if (constant == null)
throw new ArgumentNullException("constant");
Expression ret = null;
if (conditionOperator == ConditionOperators.Equal)
ret = Expression.Equal(member, constant);
else if (conditionOperator == ConditionOperators.GreaterThan)
ret = Expression.GreaterThan(member, constant);
else if (conditionOperator == ConditionOperators.GreaterThanOrEqual)
ret = Expression.GreaterThanOrEqual(member, constant);
else if (conditionOperator == ConditionOperators.LessThan)
ret = Expression.LessThan(member, constant);
else if (conditionOperator == ConditionOperators.LessThanOrEqual)
ret = Expression.LessThanOrEqual(member, constant);
else if (conditionOperator == ConditionOperators.Contains)
ret = Expression.Call(member, Constants.ContainsMethod, constant);
else if (conditionOperator == ConditionOperators.StartsWith)
ret = Expression.Call(member, Constants.StartsWithMethod, constant);
else if (conditionOperator == ConditionOperators.EndsWith)
ret = Expression.Call(member, Constants.EndsWithMethod, constant);
else
throw new ArgumentException("conditionOperator", "Must supply a known condition operator");
return ret;
}
/// <summary>
/// Returns the right expression for a path supplied.
/// </summary>
/// <param name="param">Expression.Parameter(typeOfClassOrInterface)</param>
/// <param name="path">the path you wish to resolve example Contact.Profile.FirstName</param>
/// <returns></returns>
public static Expression ResolvePathForExpression(ParameterExpression param, string path)
{
Expression body = param;
foreach (var member in path.Split('.'))
{
body = Expression.PropertyOrField(body, member);
}
return body;
}
public static ConstantExpression GetConstantSameAsLeftOperator(Expression member, object value)
{
if (member == null)
throw new ArgumentNullException("member");
if (value == null)
return Expression.Constant(null);
// the types.
var valueType = value.GetType();
var memberType = member.Type;
// if match.
if (valueType == memberType)
return Expression.Constant(value);
// attempt a conversion.
object convertedValue = TypeHelpers.ConvertFrom(memberType, value);
return Expression.Constant(convertedValue);
}
}
}

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PoweredSoft.DynamicLinq.Helpers
{
public static class TypeHelpers
{
public static object ConvertFrom(Type type, object source)
{
object ret = null;
// safe if null.
if (source == null)
return ret;
// not nullable type.
var notNullableType = Nullable.GetUnderlyingType(type);
if (notNullableType == null)
{
ret = Convert.ChangeType(source, type);
return ret;
}
// the ret.
ret = Convert.ChangeType(source, notNullableType);
return ret;
}
}
}

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2ABC5A60-B549-4ECD-BEF4-31CA7BA4EF06}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PoweredSoft.DynamicLinq</RootNamespace>
<AssemblyName>PoweredSoft.DynamicLinq</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Constants.cs" />
<Compile Include="Extensions\QueryableExtensions.cs" />
<Compile Include="Helpers\QueryableHelpers.cs" />
<Compile Include="Helpers\TypeHelpers.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Fluent\QueryBuilder.cs" />
<Compile Include="Fluent\QueryFilterPart.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("PoweredSoft.DynamicLinq")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PoweredSoft.DynamicLinq")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("2abc5a60-b549-4ecd-bef4-31ca7ba4ef06")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]