Last active
August 8, 2016 13:43
-
-
Save olivier-spinelli/363bd28e312f98515b66dfe704dab12a to your computer and use it in GitHub Desktop.
Command/Result pattern for Sql Server with CK-Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class CommandDemoTests | |
{ | |
[Test] | |
public async Task command_pattern_just_work_with_poco_result() | |
{ | |
var cmd = new CmdDemo() | |
{ | |
ActorId = 878, | |
CompanyName = "Invenietis", | |
LaunchnDate = new DateTime( 2016, 6, 30 ) | |
}; | |
var p = TestHelper.StObjMap.Default.Obtain<CmdDemoPackage>(); | |
using( var ctx = new SqlStandardCallContext() ) | |
{ | |
CmdDemo.ResultPOCO r = await p.RunCommandAsync( ctx, cmd ); | |
Assert.That( r.Delay, Is.LessThan( 0 ) ); | |
Assert.That( r.ActualCompanyName, Is.EqualTo( "INVENIETIS HOP!" ) ); | |
} | |
} | |
[Test] | |
public async Task command_pattern_just_work_with_a_clean_reaonly_poco() | |
{ | |
var cmd = new CmdDemo() | |
{ | |
ActorId = 878, | |
CompanyName = "Invenietis", | |
LaunchnDate = new DateTime( 2016, 6, 30 ) | |
}; | |
var p = TestHelper.StObjMap.Default.Obtain<CmdDemoPackage>(); | |
using( var ctx = new SqlStandardCallContext() ) | |
{ | |
CmdDemo.ResultReadOnly r = await p.RunCommandROAsync( ctx, cmd ); | |
Assert.That( r.Delay, Is.LessThan( 0 ) ); | |
Assert.That( r.ActualCompanyName, Is.EqualTo( "INVENIETIS HOP!" ) ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Demo command POCO (do not look for anything that makes sense). | |
/// Two different results are nested here: a mutable POCO and | |
/// an immutable object, CmdDemo.ResultReadOnly. | |
/// </summary> | |
public class CmdDemo | |
{ | |
public int ActorId { get; set; } | |
public string CompanyName { get; set; } | |
public DateTime LaunchnDate { get; set; } | |
/// <summary> | |
/// Result POCO as a mutable object. | |
/// </summary> | |
public class ResultPOCO | |
{ | |
public int Delay { get; set; } | |
public string ActualCompanyName { get; set; } | |
} | |
/// <summary> | |
/// Immutable result POCO (often better). | |
/// </summary> | |
public class ResultReadOnly | |
{ | |
public ResultReadOnly( int delay, string actualCompanyName ) | |
{ | |
Delay = delay; | |
ActualCompanyName = actualCompanyName; | |
} | |
public int Delay { get; } | |
public string ActualCompanyName { get; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Demo package that exposes the CmdDemo command with its | |
/// two returned POCO (mutable and immutable). | |
/// This is for demo only: obviously only one of them is enough. | |
/// </summary> | |
[SqlPackage( Schema = "Command", ResourcePath = "Res" ), Versions( "1.0.0" )] | |
public abstract partial class CmdDemoPackage : SqlPackage | |
{ | |
/// <summary> | |
/// Executes the CmdDemo command and returns a mutable result object. | |
/// </summary> | |
/// <param name="ctx">The call context to use.</param> | |
/// <param name="cmd">The command to execute.</param> | |
/// <returns>A mutable result object.</returns> | |
[SqlProcedure( "sCommandRun" )] | |
public abstract Task<CmdDemo.ResultPOCO> RunCommandAsync( ISqlCallContext ctx, [ParameterSource]CmdDemo cmd ); | |
/// <summary> | |
/// Executes the CmdDemo command and returns an immutable result object. | |
/// </summary> | |
/// <param name="ctx">The call context to use.</param> | |
/// <param name="cmd">The command to execute.</param> | |
/// <returns>An immutable result object.</returns> | |
[SqlProcedure( "sCommandRun" )] | |
public abstract Task<CmdDemo.ResultReadOnly> RunCommandROAsync( ISqlCallContext ctx, [ParameterSource]CmdDemo cmd ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create procedure Command.sCommandRun | |
( | |
@ActorId int, | |
@CompanyName nvarchar(128), | |
@LaunchnDate datetime2(3), | |
@Delay int output, | |
@ActualCompanyName nvarchar(128) output | |
) | |
as | |
begin | |
--[beginsp] | |
set @Delay = datediff( second, sysutcdatetime(), @LaunchnDate ); | |
set @ActualCompanyName = upper(@CompanyName + N' HOP!'); | |
--[endsp] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment