Skip to content

HowTo Events

IFYates edited this page Nov 9, 2022 · 2 revisions

Contents


Events

Pho/rm invokes various events to expose internal activity.

Each of those can be subscribed to at the session or global level.

Connected

// Subscribe
session.Connected += onConnected;
IFY.Phorm.Events.Connected += onConnected;

// Fires when a connection has been created (before CommandExecuting)
int result = session.Call<IAction>(); // Will only fire if the session was not already in use

CommandExecuting

// Subscribe
session.CommandExecuting += onCommandExecuting;
IFY.Phorm.Events.CommandExecuting += onCommandExecuting;

// Fires when command is about to execute
int result = session.Call<IAction>();

CommandExecuted

// Subscribe
session.CommandExecuted += onCommandExecuted;
IFY.Phorm.Events.CommandExecuted += onCommandExecuted;

// Fires when command has finished executing
int result = session.Call<IAction>();

ConsoleMessage

// Subscribe
session.ConsoleMessage += onConsoleMessage;
IFY.Phorm.Events.ConsoleMessage += onConsoleMessage;

// Fires when contract raises logs or errors
int result = session.Call<IAction>();

See logs or errors for more detail.

UnexpectedRecordColumn

// Subscribe
session.UnexpectedRecordColumn += onUnexpectedRecordColumn;
IFY.Phorm.Events.UnexpectedRecordColumn += onUnexpectedRecordColumn;

// Fires when contract returns columns in records not matched to Entity members
Entity[] data = session.Get<Entity[]>()!;

UnresolvedContractMember

// Subscribe
session.UnresolvedContractMember += onUnresolvedContractMember;
IFY.Phorm.Events.UnresolvedContractMember += onUnresolvedContractMember;

// Fires when some Entity members are not returned in the result columns
Entity[] data = session.Get<Entity[]>()!;

Logs

SQL contracts can raise logs in different ways:

CREATE PROCEDURE [dbo].[usp_PrintLogs]
AS
	SET NOCOUNT ON
	RAISERROR ('One', 0, 1) WITH NOWAIT;
	RAISERROR ('Two', 2, 3) WITH NOWAIT;
	RAISERROR ('Three', 4, 5) WITH NOWAIT;
    PRINT 'Finish'
RETURN 1

Calling this contract will fire the following console events:

Source Message Level IsError
dbo.usp_PrintLogs @ 4 One 0 false
dbo.usp_PrintLogs @ 5 Two 2 false
dbo.usp_PrintLogs @ 6 Three 4 false
dbo.usp_PrintLogs @ 7 Finish 0 false

Errors

Unhandled errors thrown in SQL will fail the execution with an appropriate Exception:

CREATE PROCEDURE [dbo].[usp_ThrowError]
AS
	SET NOCOUNT ON
	RAISERROR ('Before', 0, 1) WITH NOWAIT;
	SELECT 1 / 0 -- Error!
    PRINT 'After' -- Not reached
RETURN 1

Calling this contract using SqlPhormSession can be caught as such:

try {
	res = session.Call("ThrowError");
} catch (SqlException ex) {
	// Handle exception
	// ex.Message == "Divide by zero error encountered."
}

To reduce the need for exception handling in multiple places, the ExceptionsAsConsoleMessage setting can be set to true, causing errors thrown by SQL contracts to treated the same as logs, except with the IsError property set to true:

session.ExceptionsAsConsoleMessage = true;
IFY.Phorm.GlobalSettings.ExceptionsAsConsoleMessage = true;

Calling the contract again will now fire the following console events:

Source Message Level IsError
dbo.usp_ThrowError @ 4 Before 0 false
dbo.usp_ThrowError @ 5 Divide by zero error encountered 16 true