Search code examples
codefluent

codefluent custom stored procedure


I have a custom stored procedure with in parameters that return fields of different tables how I can map this custom stored to an entity? I only want to use like a read only values for a report I don't want to save or something like that 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


Solution

  • Solution 1: Using a view

    A view allows to aggregate data from different entities.

    <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> 
    

    Solution 2: Using a lightweight entity

    Instead of creating a view, you can can create a lightweight entity that contains only the properties used by the stored procedure.

    <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>
    

    Solution 3: Custom mapping

    For more specific values or types, a custom method can be provided to map the database values to .NET types. This custom method will be called with a DataReader as parameter, meaning that a developer could do whatever he wants.

    <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>
    

    You can also use OnAfterReadRecord or OnBeforeReadRecord rules