Skip to content

Commit 6829f8f

Browse files
Tyler CarlsonTyler Carlson
authored andcommitted
Add LinqToSQL for 4.0
1 parent c29df8c commit 6829f8f

11 files changed

Lines changed: 292 additions & 4 deletions
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{FEB6F117-EE1A-439C-A085-FD994216C41B}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>AutoMapper.Collection.LinqToSQL</RootNamespace>
11+
<AssemblyName>AutoMapper.Collection.LinqToSQL</AssemblyName>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="AutoMapper, Version=4.0.4.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
34+
<HintPath>..\packages\AutoMapper.4.0.4\lib\net40\AutoMapper.dll</HintPath>
35+
<Private>True</Private>
36+
</Reference>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Data.Linq" />
40+
<Reference Include="System.IO, Version=2.6.9.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
41+
<HintPath>..\packages\Microsoft.Bcl.1.1.9\lib\net40\System.IO.dll</HintPath>
42+
<Private>True</Private>
43+
</Reference>
44+
<Reference Include="System.Runtime, Version=2.6.9.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
45+
<HintPath>..\packages\Microsoft.Bcl.1.1.9\lib\net40\System.Runtime.dll</HintPath>
46+
<Private>True</Private>
47+
</Reference>
48+
<Reference Include="System.Threading.Tasks, Version=2.6.9.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
49+
<HintPath>..\packages\Microsoft.Bcl.1.1.9\lib\net40\System.Threading.Tasks.dll</HintPath>
50+
<Private>True</Private>
51+
</Reference>
52+
<Reference Include="System.Xml.Linq" />
53+
<Reference Include="System.Data.DataSetExtensions" />
54+
<Reference Include="Microsoft.CSharp" />
55+
<Reference Include="System.Data" />
56+
<Reference Include="System.Xml" />
57+
</ItemGroup>
58+
<ItemGroup>
59+
<Compile Include="GetLinqToSQLPrimaryKeyExpression.cs" />
60+
<Compile Include="GetLinqToSQLPrimaryKeyProperties.cs" />
61+
<Compile Include="IPersistance.cs" />
62+
<Compile Include="Persistance.cs" />
63+
<Compile Include="PersistanceExtensions.cs" />
64+
<Compile Include="Properties\AssemblyInfo.cs" />
65+
</ItemGroup>
66+
<ItemGroup>
67+
<None Include="packages.config" />
68+
</ItemGroup>
69+
<ItemGroup>
70+
<ProjectReference Include="..\AutoMapper.Collection\AutoMapper.Collection.csproj">
71+
<Project>{37ad667a-8080-476c-88fd-20310ac7caf3}</Project>
72+
<Name>AutoMapper.Collection</Name>
73+
</ProjectReference>
74+
</ItemGroup>
75+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
76+
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
77+
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
78+
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
79+
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
80+
</Target>
81+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
82+
Other similar extension points exist, see Microsoft.Common.targets.
83+
<Target Name="BeforeBuild">
84+
</Target>
85+
<Target Name="AfterBuild">
86+
</Target>
87+
-->
88+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using AutoMapper.EquivilencyExpression;
2+
3+
namespace AutoMapper.Collection.LinqToSQL
4+
{
5+
public class GetLinqToSQLPrimaryKeyExpression : GenerateEquivilentExpressionsBasedOnGeneratePropertyMaps
6+
{
7+
public GetLinqToSQLPrimaryKeyExpression()
8+
: base(new GetLinqToSQLPrimaryKeyProperties())
9+
{
10+
}
11+
}
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.Linq.Mapping;
4+
using System.Linq;
5+
using System.Reflection;
6+
using AutoMapper.EquivilencyExpression;
7+
8+
namespace AutoMapper.Collection.LinqToSQL
9+
{
10+
public class GetLinqToSQLPrimaryKeyProperties : IGeneratePropertyMaps
11+
{
12+
public IEnumerable<PropertyMap> GeneratePropertyMaps(Type srcType, Type destType)
13+
{
14+
var mapper = (Mapper.Configuration as IConfigurationProvider).ResolveTypeMap(srcType, destType);
15+
var propertyMaps = mapper.GetPropertyMaps();
16+
17+
var primaryKeyPropertyMatches = destType.GetProperties().Where(IsPrimaryKey).Select(m => propertyMaps.FirstOrDefault(p => p.DestinationProperty.Name == m.Name)).ToList();
18+
19+
return primaryKeyPropertyMatches;
20+
}
21+
22+
private static bool IsPrimaryKey(PropertyInfo propertyInfo)
23+
{
24+
return propertyInfo.GetCustomAttributes(typeof(ColumnAttribute), false)
25+
.OfType<ColumnAttribute>().Any(ca => ca.IsPrimaryKey);
26+
}
27+
}
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace AutoMapper.Collection.LinqToSQL
4+
{
5+
public interface IPersistance
6+
{
7+
/// <summary>
8+
/// Insert Or Update the <see cref="T:System.Data.Entity.DbSet`1"/> with <paramref name="from"/>
9+
/// </summary>
10+
/// <remarks>Uses <see cref="AutoMapper.EquivilencyExpression.EquivilentExpressions.GenerateEquality"/>> to find equality between Source and From Types to determine if insert or update</remarks>
11+
/// <typeparam name="TFrom">Source Type mapping from</typeparam>
12+
/// <param name="from">Object to update to <see cref="T:System.Data.Entity.DbSet`1"/></param>
13+
void InsertOrUpdate<TFrom>(TFrom from) where TFrom : class;
14+
15+
void InsertOrUpdate(Type type, object from);
16+
17+
/// <summary>
18+
/// Remove from <see cref="T:System.Data.Entity.DbSet`1"/> with <paramref name="from"/>
19+
/// </summary>
20+
/// <remarks>Uses <see cref="AutoMapper.EquivilencyExpression.EquivilentExpressions.GenerateEquality"/>> to find equality between Source and From Types to determine if insert or update</remarks>
21+
/// <typeparam name="TFrom">Source Type mapping from</typeparam>
22+
/// <param name="from">Object to remove that is equivilent in <see cref="T:System.Data.Entity.DbSet`1"/></param>
23+
void Remove<TFrom>(TFrom from) where TFrom : class;
24+
}
25+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Data.Linq;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
6+
namespace AutoMapper.Collection.LinqToSQL
7+
{
8+
public class Persistance<TTo> : IPersistance
9+
where TTo : class
10+
{
11+
private readonly Table<TTo> _sourceSet;
12+
private readonly IMappingEngine _mappingEngine;
13+
14+
public Persistance(Table<TTo> sourceSet, IMappingEngine mappingEngine)
15+
{
16+
_mappingEngine = mappingEngine;
17+
_sourceSet = sourceSet;
18+
}
19+
20+
public void InsertOrUpdate<TFrom>(TFrom from)
21+
where TFrom : class
22+
{
23+
InsertOrUpdate(typeof(TFrom), from);
24+
}
25+
26+
public void InsertOrUpdate(Type type, object from)
27+
{
28+
var equivExpr = Mapper.Map(from, type, typeof(Expression<Func<TTo, bool>>)) as Expression<Func<TTo, bool>>;
29+
if (equivExpr == null)
30+
return;
31+
32+
var to = _sourceSet.FirstOrDefault(equivExpr);
33+
34+
if (to == null)
35+
{
36+
to = Activator.CreateInstance<TTo>();
37+
_sourceSet.InsertOnSubmit(to);
38+
}
39+
_mappingEngine.Map(from, to);
40+
}
41+
42+
public void Remove<TFrom>(TFrom from)
43+
where TFrom : class
44+
{
45+
var equivExpr = Mapper.Map<TFrom, Expression<Func<TTo, bool>>>(from);
46+
if (equivExpr == null)
47+
return;
48+
var to = _sourceSet.FirstOrDefault(equivExpr);
49+
50+
if (to != null)
51+
_sourceSet.DeleteOnSubmit(to);
52+
}
53+
}
54+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Data.Linq;
5+
using System.Reflection;
6+
using AutoMapper.QueryableExtensions.Impl;
7+
8+
namespace AutoMapper.Collection.LinqToSQL
9+
{
10+
public static class PersistanceExtensions
11+
{
12+
public static IPersistance Persist<TSource>(this Table<TSource> source)
13+
where TSource : class
14+
{
15+
return new Persistance<TSource>(source, Mapper.Engine);
16+
}
17+
18+
private static readonly MethodInfo ForMethodInfo = typeof(IQueryDataSourceInjection<>).GetMethod("For");
19+
20+
public static IEnumerable For<TSource>(this IQueryDataSourceInjection<TSource> source, Type destType)
21+
{
22+
var forMethod = source.GetType().GetMethod("For").MakeGenericMethod(destType);
23+
var listType = typeof(List<>).MakeGenericType(destType);
24+
var forResult = forMethod.Invoke(source, new object[] { null });
25+
var enumeratedResult = Activator.CreateInstance(listType, forResult);
26+
return enumeratedResult as IEnumerable;
27+
}
28+
}
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("AutoMapper.Collection.LinqToSQL")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("AutoMapper.Collection.LinqToSQL")]
13+
[assembly: AssemblyCopyright("Copyright © 2015")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("feb6f117-ee1a-439c-a085-fd994216c41b")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="AutoMapper" version="4.0.4" targetFramework="net4" />
4+
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net4" />
5+
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net4" />
6+
</packages>

src/AutoMapper.Collection/AutoMapper.Collection.sln

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2012
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.23107.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
46
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMapper.Collection", "AutoMapper.Collection\AutoMapper.Collection.csproj", "{37AD667A-8080-476C-88FD-20310AC7CAF3}"
57
EndProject
68
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{19AAEE83-5EEC-4EAA-9CF7-16F8ED58B50E}"
@@ -17,6 +19,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1719
update.bat = update.bat
1820
EndProjectSection
1921
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMapper.Collection.LinqToSQL", "AutoMapper.Collection.LinqToSQL\AutoMapper.Collection.LinqToSQL.csproj", "{FEB6F117-EE1A-439C-A085-FD994216C41B}"
23+
EndProject
2024
Global
2125
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2226
Debug|Any CPU = Debug|Any CPU
@@ -31,6 +35,10 @@ Global
3135
{A7F99BDD-5251-46FA-90F8-184741AE9D02}.Debug|Any CPU.Build.0 = Debug|Any CPU
3236
{A7F99BDD-5251-46FA-90F8-184741AE9D02}.Release|Any CPU.ActiveCfg = Release|Any CPU
3337
{A7F99BDD-5251-46FA-90F8-184741AE9D02}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{FEB6F117-EE1A-439C-A085-FD994216C41B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{FEB6F117-EE1A-439C-A085-FD994216C41B}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{FEB6F117-EE1A-439C-A085-FD994216C41B}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{FEB6F117-EE1A-439C-A085-FD994216C41B}.Release|Any CPU.Build.0 = Release|Any CPU
3442
EndGlobalSection
3543
GlobalSection(SolutionProperties) = preSolution
3644
HideSolutionNode = FALSE

src/AutoMapper.Collection/AutoMapper.Collection/AutoMapper.Collection.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>AutoMapper.Collection</RootNamespace>
1111
<AssemblyName>AutoMapper.Collection</AssemblyName>
12-
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
1515
<RestorePackages>true</RestorePackages>
@@ -63,7 +63,9 @@
6363
</ItemGroup>
6464
<ItemGroup>
6565
<None Include="Equivilency Expression\packages.config" />
66-
<None Include="packages.config" />
66+
<None Include="packages.config">
67+
<SubType>Designer</SubType>
68+
</None>
6769
</ItemGroup>
6870
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6971
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />

0 commit comments

Comments
 (0)