Implementing Insert-builder with fluent syntax

This commit is contained in:
Morten Christensen
2012-12-07 12:23:03 -01:00
parent 97509316ba
commit 9935abd1a2
7 changed files with 98 additions and 3 deletions

View File

@@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Umbraco.Core.Persistence.Migrations.Model
{
public class InsertionDataDefinition : List<KeyValuePair<string, object>>
{
}
}

View File

@@ -0,0 +1,24 @@
using System.Collections.Generic;
using Umbraco.Core.Persistence.Migrations.Model;
namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert.Expressions
{
public class InsertDataExpression : IMigrationExpression
{
private readonly List<InsertionDataDefinition> _rows = new List<InsertionDataDefinition>();
public string SchemaName { get; set; }
public string TableName { get; set; }
public List<InsertionDataDefinition> Rows
{
get { return _rows; }
}
public override string ToString()
{
//TODO implement the use of sql syntax provider?
return string.Empty;
}
}
}

View File

@@ -1,7 +1,7 @@
namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert
{
public interface IInsertBuilder
public interface IInsertBuilder : IFluentSyntax
{
IInsertDataSyntax IntoTable(string tableName);
}
}

View File

@@ -0,0 +1,7 @@
namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert
{
public interface IInsertDataSyntax : IFluentSyntax
{
IInsertDataSyntax Row(object dataAsAnonymousType);
}
}

View File

@@ -1,4 +1,6 @@
namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert
using Umbraco.Core.Persistence.Migrations.Syntax.Insert.Expressions;
namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert
{
public class InsertBuilder : IInsertBuilder
{
@@ -8,5 +10,12 @@
{
_context = context;
}
public IInsertDataSyntax IntoTable(string tableName)
{
var expression = new InsertDataExpression { TableName = tableName };
_context.Expressions.Add(expression);
return new InsertDataBuilder(expression);
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.ComponentModel;
using Umbraco.Core.Persistence.Migrations.Model;
using Umbraco.Core.Persistence.Migrations.Syntax.Insert.Expressions;
namespace Umbraco.Core.Persistence.Migrations.Syntax.Insert
{
public class InsertDataBuilder : IInsertDataSyntax
{
private readonly InsertDataExpression _expression;
public InsertDataBuilder(InsertDataExpression expression)
{
_expression = expression;
}
public IInsertDataSyntax Row(object dataAsAnonymousType)
{
_expression.Rows.Add(GetData(dataAsAnonymousType));
return this;
}
private static InsertionDataDefinition GetData(object dataAsAnonymousType)
{
var data = new InsertionDataDefinition();
var properties = TypeDescriptor.GetProperties(dataAsAnonymousType);
foreach (PropertyDescriptor property in properties)
{
data.Add(new KeyValuePair<string, object>(property.Name, property.GetValue(dataAsAnonymousType)));
}
return data;
}
}
}

View File

@@ -229,6 +229,7 @@
<Compile Include="Persistence\Migrations\Model\ConstraintDefinition.cs" />
<Compile Include="Persistence\Migrations\Model\ConstraintType.cs" />
<Compile Include="Persistence\Migrations\Model\Direction.cs" />
<Compile Include="Persistence\Migrations\Model\InsertionDataDefinition.cs" />
<Compile Include="Persistence\Migrations\Model\ModificationType.cs" />
<Compile Include="Persistence\Migrations\Model\ForeignKeyDefinition.cs" />
<Compile Include="Persistence\Migrations\Model\IndexColumnDefinition.cs" />
@@ -290,8 +291,11 @@
<Compile Include="Persistence\Migrations\Syntax\IColumnTypeSyntax.cs" />
<Compile Include="Persistence\Migrations\Syntax\IFluentSyntax.cs" />
<Compile Include="Persistence\Migrations\Syntax\IForeignKeyCascadeSyntax.cs" />
<Compile Include="Persistence\Migrations\Syntax\Insert\Expressions\InsertDataExpression.cs" />
<Compile Include="Persistence\Migrations\Syntax\Insert\IInsertBuilder.cs" />
<Compile Include="Persistence\Migrations\Syntax\Insert\IInsertDataSyntax.cs" />
<Compile Include="Persistence\Migrations\Syntax\Insert\InsertBuilder.cs" />
<Compile Include="Persistence\Migrations\Syntax\Insert\InsertDataBuilder.cs" />
<Compile Include="Persistence\Migrations\Syntax\Rename\IRenameBuilder.cs" />
<Compile Include="Persistence\Migrations\Syntax\Rename\RenameBuilder.cs" />
<Compile Include="Persistence\Migrations\Syntax\Schema\ISchemaBuilder.cs" />
@@ -595,6 +599,12 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Persistence\Migrations\Syntax\Delete\Column\" />
<Folder Include="Persistence\Migrations\Syntax\Delete\Constraint\" />
<Folder Include="Persistence\Migrations\Syntax\Delete\DefaultConstraint\" />
<Folder Include="Persistence\Migrations\Syntax\Delete\ForeignKey\" />
<Folder Include="Persistence\Migrations\Syntax\Delete\Index\" />
<Folder Include="Persistence\Migrations\Syntax\Delete\Table\" />
<Folder Include="Persistence\Migrations\Syntax\Schema\Index\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />