moved conversion into a different project.
This commit is contained in:
parent
118f7c30f8
commit
01e7652c06
58
PoweredSoft.Conversion/Convert.cs
Normal file
58
PoweredSoft.Conversion/Convert.cs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
using PoweredSoft.Types.Converters;
|
||||||
|
using PoweredSoft.Types.Interface;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace PoweredSoft.Types
|
||||||
|
{
|
||||||
|
public static class Converter
|
||||||
|
{
|
||||||
|
public static List<ITypeConverter> Converters { get; internal set; } = new List<ITypeConverter>
|
||||||
|
{
|
||||||
|
new StringToDateConverter(),
|
||||||
|
new StringToGuidConverter()
|
||||||
|
};
|
||||||
|
|
||||||
|
public static void RegisterConverter(ITypeConverter converter)
|
||||||
|
{
|
||||||
|
lock (Converters)
|
||||||
|
{
|
||||||
|
Converters.Add(converter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ReplaceConverter(ITypeConverter converter, Type source, Type destination)
|
||||||
|
{
|
||||||
|
lock (Converters)
|
||||||
|
{
|
||||||
|
Converters.RemoveAll(t => t.CanConvert(source, destination));
|
||||||
|
Converters.Add(converter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static object To(this object source, Type type)
|
||||||
|
{
|
||||||
|
object ret = null;
|
||||||
|
|
||||||
|
// safe if null.
|
||||||
|
if (source == null)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
// establish final type.
|
||||||
|
var notNullType = Nullable.GetUnderlyingType(type);
|
||||||
|
var finalType = notNullType ?? type;
|
||||||
|
var converter = Converters.FirstOrDefault(t => t.CanConvert(source.GetType(), finalType));
|
||||||
|
if (converter != null)
|
||||||
|
ret = converter.Convert(source, finalType);
|
||||||
|
else
|
||||||
|
ret = Convert.ChangeType(source, finalType);
|
||||||
|
|
||||||
|
if (notNullType != null)
|
||||||
|
ret = Activator.CreateInstance(type, new object[] { ret });
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
PoweredSoft.Conversion/Converters/StringToDateConverter.cs
Normal file
13
PoweredSoft.Conversion/Converters/StringToDateConverter.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using PoweredSoft.Types.Interface;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace PoweredSoft.Types.Converters
|
||||||
|
{
|
||||||
|
public class StringToDateConverter : ITypeConverter
|
||||||
|
{
|
||||||
|
public bool CanConvert(Type source, Type destination) => source == typeof(string) && destination == typeof(DateTime);
|
||||||
|
public object Convert(object source, Type destination) => DateTime.Parse((string)source);
|
||||||
|
}
|
||||||
|
}
|
13
PoweredSoft.Conversion/Converters/StringToGuidConverter.cs
Normal file
13
PoweredSoft.Conversion/Converters/StringToGuidConverter.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using PoweredSoft.Types.Interface;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace PoweredSoft.Types.Converters
|
||||||
|
{
|
||||||
|
public class StringToGuidConverter : ITypeConverter
|
||||||
|
{
|
||||||
|
public bool CanConvert(Type source, Type destination) => source == typeof(string) && destination == typeof(Guid);
|
||||||
|
public object Convert(object source, Type destination) => Guid.Parse((string)source);
|
||||||
|
}
|
||||||
|
}
|
12
PoweredSoft.Conversion/Interface/ITypeConverter.cs
Normal file
12
PoweredSoft.Conversion/Interface/ITypeConverter.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace PoweredSoft.Types.Interface
|
||||||
|
{
|
||||||
|
public interface ITypeConverter
|
||||||
|
{
|
||||||
|
bool CanConvert(Type source, Type destination);
|
||||||
|
object Convert(object source, Type destination);
|
||||||
|
}
|
||||||
|
}
|
7
PoweredSoft.Conversion/PoweredSoft.Types.csproj
Normal file
7
PoweredSoft.Conversion/PoweredSoft.Types.csproj
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -37,6 +37,8 @@ namespace PoweredSoft.DynamicLinq.Dal
|
|||||||
modelBuilder.Configurations.Add(new CommentConfiguration());
|
modelBuilder.Configurations.Add(new CommentConfiguration());
|
||||||
modelBuilder.Configurations.Add(new PostConfiguration());
|
modelBuilder.Configurations.Add(new PostConfiguration());
|
||||||
modelBuilder.Configurations.Add(new WebsiteConfiguration());
|
modelBuilder.Configurations.Add(new WebsiteConfiguration());
|
||||||
|
modelBuilder.Configurations.Add(new CommentLikeConfiguration());
|
||||||
|
modelBuilder.Configurations.Add(new UniqueConfiguration());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,23 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace PoweredSoft.DynamicLinq.Dal.Configurations
|
namespace PoweredSoft.DynamicLinq.Dal.Configurations
|
||||||
{
|
{
|
||||||
|
public class UniqueConfiguration : EntityTypeConfiguration<Unique>
|
||||||
|
{
|
||||||
|
public UniqueConfiguration() : this("dbo")
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniqueConfiguration(string schema)
|
||||||
|
{
|
||||||
|
ToTable("Unique", schema);
|
||||||
|
HasKey(t => t.Id);
|
||||||
|
Property(t => t.Id).HasColumnName("Id").HasColumnType("bigint").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
|
||||||
|
Property(t => t.RowNumber).HasColumnType("uniqueidentifier").IsRequired();
|
||||||
|
Property(t => t.OtherNullableGuid).HasColumnType("uniqueidentifier");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class AuthorConfiguration : EntityTypeConfiguration<Author>
|
public class AuthorConfiguration : EntityTypeConfiguration<Author>
|
||||||
{
|
{
|
||||||
public AuthorConfiguration() : this("dbo")
|
public AuthorConfiguration() : this("dbo")
|
||||||
|
15
PoweredSoft.DynamicLinq.Dal/Pocos/Uniqe.cs
Normal file
15
PoweredSoft.DynamicLinq.Dal/Pocos/Uniqe.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PoweredSoft.DynamicLinq.Dal.Pocos
|
||||||
|
{
|
||||||
|
public class Unique
|
||||||
|
{
|
||||||
|
public long Id { get; set; }
|
||||||
|
public Guid RowNumber { get; set; }
|
||||||
|
public Guid? OtherNullableGuid { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -50,6 +50,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="BlogContext.cs" />
|
<Compile Include="BlogContext.cs" />
|
||||||
<Compile Include="Configurations\Configurations.cs" />
|
<Compile Include="Configurations\Configurations.cs" />
|
||||||
|
<Compile Include="Pocos\Uniqe.cs" />
|
||||||
<Compile Include="Pocos\Website.cs" />
|
<Compile Include="Pocos\Website.cs" />
|
||||||
<Compile Include="Pocos\CommentLike.cs" />
|
<Compile Include="Pocos\CommentLike.cs" />
|
||||||
<Compile Include="Pocos\Post.cs" />
|
<Compile Include="Pocos\Post.cs" />
|
||||||
|
@ -42,6 +42,14 @@ namespace PoweredSoft.DynamicLinq.Test
|
|||||||
Assert.IsTrue(Posts.AsQueryable().Query(t => t.Equal("Id", 1, QueryConvertStrategy.LeaveAsIs)).Any());
|
Assert.IsTrue(Posts.AsQueryable().Query(t => t.Equal("Id", 1, QueryConvertStrategy.LeaveAsIs)).Any());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void TestGuid()
|
||||||
|
{
|
||||||
|
var randomGuidStr = Guid.NewGuid().ToString();
|
||||||
|
TestData.Uniques.AsQueryable().Query(t => t.Equal("RowNumber", randomGuidStr));
|
||||||
|
TestData.Uniques.AsQueryable().Query(t => t.Equal("OtherNullableGuid", randomGuidStr));
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void SpecifyType()
|
public void SpecifyType()
|
||||||
{
|
{
|
||||||
|
@ -54,6 +54,9 @@
|
|||||||
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
<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>
|
<HintPath>..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="PoweredSoft.Types, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\PoweredSoft.Types.1.0.0\lib\netstandard2.0\PoweredSoft.Types.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
|
@ -34,6 +34,12 @@ namespace PoweredSoft.DynamicLinq.Test
|
|||||||
|
|
||||||
internal static class TestData
|
internal static class TestData
|
||||||
{
|
{
|
||||||
|
static readonly internal List<Unique> Uniques = new List<Unique>
|
||||||
|
{
|
||||||
|
new Unique { Id = 1, RowNumber = Guid.NewGuid(), OtherNullableGuid = null } ,
|
||||||
|
new Unique { Id = 2, RowNumber = Guid.NewGuid(), OtherNullableGuid = Guid.NewGuid() }
|
||||||
|
};
|
||||||
|
|
||||||
static readonly internal List<MockPersonObject> Persons = new List<MockPersonObject>
|
static readonly internal List<MockPersonObject> Persons = new List<MockPersonObject>
|
||||||
{
|
{
|
||||||
new MockPersonObject { FirstName = "David", LastName = "Lebee", Age = 28 },
|
new MockPersonObject { FirstName = "David", LastName = "Lebee", Age = 28 },
|
||||||
|
@ -4,4 +4,5 @@
|
|||||||
<package id="Faker.Data" version="1.0.7" targetFramework="net461" />
|
<package id="Faker.Data" version="1.0.7" targetFramework="net461" />
|
||||||
<package id="MSTest.TestAdapter" version="1.2.0" targetFramework="net461" />
|
<package id="MSTest.TestAdapter" version="1.2.0" targetFramework="net461" />
|
||||||
<package id="MSTest.TestFramework" version="1.2.0" targetFramework="net461" />
|
<package id="MSTest.TestFramework" version="1.2.0" targetFramework="net461" />
|
||||||
|
<package id="PoweredSoft.Types" version="1.0.0" targetFramework="net462" />
|
||||||
</packages>
|
</packages>
|
@ -13,6 +13,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PoweredSoft.DynamicLinq.Ent
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoweredSoft.DynamicLinq.ConsoleApp", "PoweredSoft.DynamicLinq.ConsoleApp\PoweredSoft.DynamicLinq.ConsoleApp.csproj", "{A166BD89-7BF3-475B-871F-294B8A420D9E}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PoweredSoft.DynamicLinq.ConsoleApp", "PoweredSoft.DynamicLinq.ConsoleApp\PoweredSoft.DynamicLinq.ConsoleApp.csproj", "{A166BD89-7BF3-475B-871F-294B8A420D9E}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{77B4027B-ECB0-4ED1-8646-025AC4146CE2}"
|
||||||
|
ProjectSection(SolutionItems) = preProject
|
||||||
|
README.md = README.md
|
||||||
|
EndProjectSection
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
@ -327,17 +327,8 @@ namespace PoweredSoft.DynamicLinq.Helpers
|
|||||||
if (value == null)
|
if (value == null)
|
||||||
return Expression.Constant(null);
|
return Expression.Constant(null);
|
||||||
|
|
||||||
// the types.
|
var convertedValue = PoweredSoft.Types.Converter.To(value, member.Type);
|
||||||
var valueType = value.GetType();
|
return Expression.Constant(convertedValue, member.Type);
|
||||||
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, memberType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ConstantExpression ResolveConstant(Expression member, object value, QueryConvertStrategy convertStrategy)
|
public static ConstantExpression ResolveConstant(Expression member, object value, QueryConvertStrategy convertStrategy)
|
||||||
@ -474,7 +465,7 @@ namespace PoweredSoft.DynamicLinq.Helpers
|
|||||||
foreach (var o in enumerableValue)
|
foreach (var o in enumerableValue)
|
||||||
{
|
{
|
||||||
if (convertStrategy == QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)
|
if (convertStrategy == QueryConvertStrategy.ConvertConstantToComparedPropertyOrField)
|
||||||
list.Add(TypeHelpers.ConvertFrom(memberExpression.Type, o));
|
list.Add(PoweredSoft.Types.Converter.To(o, memberExpression.Type));
|
||||||
else
|
else
|
||||||
list.Add(o);
|
list.Add(o);
|
||||||
}
|
}
|
||||||
|
@ -24,26 +24,7 @@ namespace PoweredSoft.DynamicLinq.Helpers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
|
||||||
<Company>Powered Softwares Inc.</Company>
|
<Company>Powered Softwares Inc.</Company>
|
||||||
<Authors>David Lebée</Authors>
|
<Authors>David Lebée</Authors>
|
||||||
<Copyright></Copyright>
|
<Copyright></Copyright>
|
||||||
@ -22,6 +22,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="PoweredSoft.Types" Version="1.0.0" />
|
||||||
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
|
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user