创建自定义linq方法以创建sql表达式

时间:2015-12-14 09:38:40

标签: vb.net linq fluent-migrator

我尝试创建类似于FluentMigrator中可以找到的自定义linq方法(和类)。我想执行这样的调用:

Create.Table("Users")
    .WithColumn("UserId").AsInt32().Identity().PrimaryKey()
    .WithColumn("GroupId").AsInt32().NotNullable()
    .WithColumn("UserName").AsString(32).NotNullable()
    .WithColumn("Password").AsString(32).NotNullable();

我已经实现了一个处理Create.TableWithColumn的类。两种方法都返回表示要创建的表的相同类类型。

我不明白的是如何创建其他方法(AsInt32,Identity,...)。这些方法不对我的表类进行操作,而是对WithColumn方法创建的列类进行操作。

如何定义我的类,以便将表类链接到列类,反之亦然?

这是我到目前为止提出的代码:

Friend Class PdmCreateExpression
    Public Property Schema As String
    Public Property TableName As String
    Public Property Columns As List(Of PdmColumn)

    Public Sub New()
        Columns = New List(Of PdmColumn)
    End Sub

    Public Function Table(tableName As String) As PdmCreateExpression
        Me.TableName = tableName
        Return Me
    End Function

    Public Function WithColumn(columnName As String) As PdmCreateExpression
        Dim newColumn = New PdmColumn(columnName)
        Columns.Add(newColumn)
        Return Me
    End Function

    Public Overrides Function ToString() As String
        Dim sqlString = "CREATE TABLE "

        If Not String.IsNullOrEmpty(Schema) Then sqlString += "[" + Schema + "]."
        sqlString += "[" + TableName + "] ("
        sqlString += String.Join(",", Columns.Select(Function(c) "[" + c.Name + "]"))
        sqlString += ")"

        Return sqlString
    End Function

End Class

Friend Class PdmColumn
    Public Property Name As String
    Public Property SqlType As String

    Public Sub New(name As String)
        Me.Name = name
    End Sub

    Public Function AsSqlType(sqlType As String)
        Me.SqlType = sqlType
        Return Me
    End Function
End Class

Friend MustInherit Class MigratorBase
    Protected Property _DbConnection As SqlConnection
    Protected Property Expressions As List(Of PdmCreateExpression)

    Sub New(dbConnection As SqlConnection)
        _DbConnection = dbConnection
        Expressions = New List(Of PdmCreateExpression)()
    End Sub

    Public MustOverride Sub Install()

    Public Function Create() As PdmCreateExpression
        Dim newExpression = New PdmCreateExpression()
        Expressions.Add(newExpression)
        Return newExpression
    End Function
End Class

使用上面的代码我可以像这样创建我的查询:

Dim migrator = New MigratorBase(_DbConnection)
Dim sql = migrator.Create.Table("VersionInfo") _
                .WithColumn("Version") _
                .WithColumn("TimestampCreated") _
                .ToString()

1 个答案:

答案 0 :(得分:0)

这些不是LINQ方法;它们被称为extension methods

您可以使用所谓的"标记接口完成后续操作":

interface ITableConvertibleExtensionTarget
{    
}

interface ITableExtensionTarget : ITableConvertibleExtensionTarget
{
}

interface IColumnExtensionTarget
{
}

class TableMap : ITableExtensionTarget
{    
}

class ColumnMap : IColumnExtensionTarget, ITableConvertibleExtensionTarget
{    
}

static class ColumnMapExtensions
{
    public static ColumnMap AsInt32(this IColumnExtensionTarget column)
    {
        // Mapping voodoo
        return column;
    }

    public static ColumnMap NotNullable(this IColumnExtensionTarget column)
    {
        // More voodoo
        return column;
    }
}

public static TableConvertibleExtensionTargetExtensions
{
    public static TableMap WithColumn(this ITableConvertibleExtensionTarget table)
    {
        return new TableMap(...);
    }
}