Skip to content

HowTo Contracts

IFYates edited this page Nov 9, 2022 · 5 revisions

The main design goals of Pho/rm are around the use of strong "contracts".
As with any other part of software design, this is a codified agreed data shape between two domains. In this case, the data layer and the business layer.

Types

There are 2 main types of contract used by Pho/rm:

  1. Data contracts, for retrieving entities from the datasource
  2. Action contracts, for sending data or performing actions on the datasource

The referenced pages go in to detailed examples of how to use each type of contract.

Naming

Pho/rm will automatically attempt to resolve the default schema (namespace) for a datasource on connection.
This value can be set specifically on each contract by using the Namespace property on a DataMemberAttribute or PhormContractAttribute.

Unless specified using the Name property of a DataContractAttr or PhormContractAttribute, the name of the given type will be used, with the leading I of an interface omitted.

class MyType1 { } // Will use [default-schema].[prefix_MyType1]
interface IMyType2 { } // Will use [default-schema].[prefix_MyType2]

[DataContract(Namespace = "schema", Name = "DbObject")]
class MyType3 { } // Will use [schema].[prefix_DbObject]

Prefix

The contract prefix used is based on the "object type" (assumed by use unless specified).
For example, all Call and From uses are assumed to be StoredProcedure types, while Get calls on the session will assume View.

IPhormSession session = diContainer.GetInstance<IPhormSession>();
session.ProcedurePrefix = "sp_";

int result = session.Call<IMyType2>();
// SQL: EXEC [default-schema].[sp_MyType2]