我的模型包含一个具有数字 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。我怎样才能做到这一点?