Skip to content

Best practices

Ian Yates edited this page Mar 25, 2022 · 3 revisions

Lists of best practices in regards to performance, correctness, and quality, as learned through use of the framework.

General

  • Use the "gateway" or "repository" patterns to remove direct Pho/rm requests from your business logic.

Get

  • If "nullable reference types" is enabled for your project, you can safely assume that Get<T[]> (get array) will never return null.
    The suggested syntax is therefore Get<T[]>()!.

Contracts

Provider-specific suggestions will be tagged, where known.

  • One contract per action.
  • Use a prefix to distinguish stored procedures and views from tables. Pho/rm suggests usp_ and vw_ by default.
  • Name stored procedures clearly. i.e., GetDataByKey, GetAllData.
  • Do not use SELECT * to access data; this removes protection against underlying structural changes (see our ethos).
  • [T-SQL] Start procedures with SET NOCOUNT ON to remove unnecessary performance overhead and network traffic.
    If you need the affected rows count for a particular query, you can print/raiserror the @@ROWCOUNT value as needed.
  • [T-SQL] If you are using transactions within a procedure, start the procedure with SET XACT_ABORT ON to further protect data integrity.
  • All contracts have an accessible int return value. Given that the default will be 0 if omitted, the same as non-execution, it is recommended to use a standard non-0 value for success (e.g., 1) for actions that are not returning a meaningful result value, so that it can still be tested for success.
    All examples here use RETURN 1 to indicate success.
  • Use negative results to return unique errors/validation failures per stored procedure.