1

My model contains an entity with a numeric Landcode property. The value 0 is a valid value for this property:

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

The generated code for the LoadByLandcode method looks like this:

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

As you can see CodeFluent returns null if the provided landCode parameter is 0. In order to avoid this check I have indicated that the landCode parameter is nullable:

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

or

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

Now in the BOM the check for 0 has been removed, but in the stored procedure a check for null on the landCode parameter has been added:

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

I neither want a check for 0 in the BOM nor a check for NULL in the stored procedure. How can I achieve this?

BremHi
  • 21
  • 1

1 Answers1

0

To remove the default value check in the generated BOM, set 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>

If you use the graphical interface:

This generates:

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
meziantou
  • 20,589
  • 7
  • 64
  • 83