【问题标题】:CodeFluent produces either a check for 0 in the method or a check for NULL in the stored procedureCodeFluent 在方法中生成 0 检查或在存储过程中生成 NULL 检查
【发布时间】:2016-06-23 12:21:39
【问题描述】:

我的模型包含一个具有数字 Landcode 属性的实体。值 0 是此属性的有效值:

<cf:entity name="Land">
  <cf:property name="Id" key="true" />
  <cf:property name="Landcode" typeName="ushort" nullable="false" usePersistenceDefaultValue="false" />

  <cf:method name="LoadByLandcode"
      body="LOADONE(ushort landCode) WHERE Landcode = @landcode">
  </cf:method>
</cf:entity>

LoadByLandcode 方法生成的代码如下所示:

public static Land LoadByLandcode(ushort landCode)
{
    if ((landCode == CodeFluentPersistence.DefaultUInt16Value))
    {
        return null;
    }
    Land land = new Land();
    CodeFluent.Runtime.CodeFluentPersistence persistence = CodeFluentContext.Get(BusinessLayerStoreName).Persistence;
    persistence.CreateStoredProcedureCommand(null, "Land", "LoadByLandcode");
    persistence.AddParameter("@landCode", landCode);
    System.Data.IDataReader reader = null;
    try
    {
        reader = persistence.ExecuteReader();
        if ((reader.Read() == true))
        {
            land.ReadRecord(reader, CodeFluent.Runtime.CodeFluentReloadOptions.Default);
            land.EntityState = CodeFluent.Runtime.CodeFluentEntityState.Unchanged;
            return land;
        }
    }
    finally
    {
        if ((reader != null))
        {
            reader.Dispose();
        }
        persistence.CompleteCommand();
    }
    return null;
}

如您所见,如果提供的 landCode 参数为 0,CodeFluent 将返回 null。为了避免这种检查,我指出 landCode 参数可以为空:

  <cf:method name="LoadByLandcode" body="LOADONE(ushort landCode?) WHERE Landcode = @landcode">
  </cf:method>

<cf:method name="LoadByLandcode" body="LOADONE(ushort landCode) WHERE Landcode = @landcode">
  <cf:parameter name="landCode" nullable="true" />
</cf:method>

现在在 BOM 中已删除对 0 的检查,但在存储过程中添加了对 landCode 参数的 null 检查:

CREATE PROCEDURE [dbo].[Land_LoadByLandcode]
(
  @landCode [smallint] = NULL
)
AS
SET NOCOUNT ON
IF (@landCode IS NULL)
BEGIN
  SELECT DISTINCT [Land].[Land_Id], [Land].[Landcode], ...FROM [Land] 
END
ELSE
BEGIN
  SELECT DISTINCT [Land].[Land_Id], [Land].[Landcode], ... FROM [Land] 
    WHERE ([Land].[Landcode] = @landCode)
END
RETURN

我既不想在 BOM 中检查 0,也不想在存储过程中检查 NULL。我怎样才能做到这一点?

【问题讨论】:

    标签: codefluent


    【解决方案1】:

    要删除生成的 BOM 中的默认值检查,请设置 cfom:checkDefaultValue="false"

    <cf:method name="LoadByLandcode" body="LOADONE(ushort landCode) WHERE Landcode = @landcode">
       <cf:parameter typeName="ushort" name="landCode" nullable="False" cfom:checkDefaultValue="false" modelNullable="False"  usePersistenceDefaultValue="false" />
    </cf:method>
    

    如果使用图形界面:

    这会生成:

    public static Land LoadByLandcode(ushort landCode)
    {
        Land land = new Land();
        CodeFluent.Runtime.CodeFluentPersistence persistence = CodeFluentContext.Get(Constants.StoreName).Persistence;
        persistence.CreateStoredProcedureCommand(null, "Land", "LoadByLandcode");
        persistence.AddRawParameter("@landCode", landCode);
        System.Data.IDataReader reader = null;
        try
        {
            reader = persistence.ExecuteReader();
            if ((reader.Read() == true))
            {
                land.ReadRecord(reader, CodeFluent.Runtime.CodeFluentReloadOptions.Default);
                land.EntityState = CodeFluent.Runtime.CodeFluentEntityState.Unchanged;
                return land;
            }
        }
        finally
        {
            if ((reader != null))
            {
                reader.Dispose();
            }
            persistence.CompleteCommand();
        }
        return null;
    }
    
    
    
    CREATE PROCEDURE [dbo].[Land_LoadByLandcode]
    (
     @landCode [smallint]
    )
    AS
    SET NOCOUNT ON
    SELECT DISTINCT [Land].[Land_Id], [Land].[Land_Landcode] 
        FROM [Land]
        WHERE ([Land].[Land_Landcode] = @landCode)
    
    RETURN
    GO
    

    【讨论】:

      猜你喜欢
      • 2019-05-06
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      相关资源
      最近更新 更多