codefluent 自定义存储过程

codefluent custom stored procedure

我有一个带有参数的自定义存储过程,return 不同表的字段如何将这个自定义存储映射到一个实体?我只想为我不想保存的报告使用只读值或类似的东西我尝试将额外的字段添加到最相似的实体但是当我在代码中执行该方法时额外的字段为空

解决方案 1:使用视图

视图允许聚合来自不同实体的数据。

<Article>
    <Id />
    <Name />
    <Lines typeName="LineCollection" />

    <cf:method name="LoadArticlesByCommand" body="load(string commandName) from ArticleByCommand where CommandName = @commandName" />

    <cf:view name="ArticleByCommand" autoLightweight="true">   
        <ArticleName expression="Name"/>
        <ArticleQty expression="Lines.Quantity" />
        <CommandName expression="Lines.Command.Name" />
    </cf:view>
</Article>

<Command>
    <Id />
    <Name />
    <Lines typeName="LineCollection" />
</Command>

<Line setType="List">
    <Article typeName="Article" key="true" />
    <Command typeName="Command" key="true" />
    <Quantity typeName="int" />
</Line> 

解决方案 2:使用轻量级实体

您可以创建仅包含存储过程使用的属性的轻量级实体,而不是创建视图。

<cf:entity name="Person" lightweight="true">
  <cf:property name="FirstName" typeName="string" />
  <cf:property name="lastName" typeName="string" />

  <cf:method name="ComputeBalance" 
             body="load () raw" 
             rawBody="SELECT 'John' AS FirstName, 'Doe' AS LastName" />
</cf:entity>

解决方案 3:自定义映射

对于更具体的值或类型,可以提供自定义方法以将数据库值映射到 .NET 类型。此自定义方法将以 DataReader 作为参数调用,这意味着开发人员可以做任何他想做的事情。

<cf:entity name="Sample">
  <cf:method name="LoadPair" body="raw" rawBody="SELECT 1234,5678" 
             returnTypeName="CodeFluent.Runtime.Utilities.Pair&lt;System.Int32,System.Int32&gt;" 
             cfom:methodName="On{0}" />
  <cf:snippet>
    private static CodeFluent.Runtime.Utilities.Pair&lt;int,int&gt; OnLoadPair(System.Data.IDataReader reader)
    {
        return new Pair&lt;int, int&gt;(reader.GetInt32(0), reader.GetInt32(1));
    }
   </cf:snippet>
</cf:entity>

您也可以使用 OnAfterReadRecordOnBeforeReadRecord 规则

如果您不必将自定义存储过程的结果映射到实体,那么另一个选择是使用对数据集的内置支持。

http://blog.codefluententities.com/2011/06/22/dataset-support-in-codefluent-entities/

<cf:method name="LoadAllCities" body="raw" returnTypeName="System.Data.DataSet">
SELECT $Address::City$ FROM $Address$
</cf:method>

.

DataSet ds = Address.LoadAllCities();
foreach (DataTable table in ds.Tables)
{
    foreach (DataRow row in table.Rows)
    {
        Console.WriteLine("City: " + row[0]);
    }
}

在重新阅读你的问题后,我提供了另一个答案。

回应您所说的部分"I try to add the extra fields to the most similar entity but when I execute the method in code the extra fields are null"。下面的步骤应该可以解决这个问题。

  1. 执行在 SQL Management Studio 中自动创建的存储过程之一。
  2. 执行您手动创建的存储过程。
  3. 验证两个存储过程返回的字段名是否匹配。

我认为上面的方法可以解决你眼前的问题,但我仍然不喜欢这个解决方案。原因是你说你选择了最相似的实体。我认为这将在未来引起问题,特别是如果存储过程未映射到所有实体属性。

我会推荐轻量级实体、视图或数据集。