-
Notifications
You must be signed in to change notification settings - Fork 0
Getting started
IFYates edited this page Nov 9, 2022
·
2 revisions
A basic Pho/rm CRUD solution will require 4 components.
We'll assume that an entity table already exists:
CREATE TABLE [dbo].[Data] (
[Id] BIGINT NOT NULL PRIMARY KEY,
[Key] NVARCHAR(50) NOT NULL UNIQUE,
[Value] NVARCHAR(256) NULL
)
Ideally, we will use a "get" contract, but we could also pull from the table directly:
CREATE TABLE [dbo].[usp_GetAllData]
AS
SET NOCOUNT ON -- See "Best practices"
SELECT [Id], [Key], [Value] -- See "Best practices"
FROM [dbo].[Data]
RETURN 1 -- Success; See "Retrieving entities"
We can also define as many action contracts as needed:
CREATE TABLE [dbo].[usp_CreateDataItem] (
@Key NVARCHAR(50),
@Value NVARCHAR(256),
@Id BIGINT = NULL OUTPUT
) AS
SET NOCOUNT ON -- See "Best practices"
INSERT INTO [dbo].[Data] ([Key], [Value])
SELECT @Key, @Value
SET @Id = SCOPE_IDENTITY()
RETURN 1 -- Success; See "Retrieving entities"
We must set up the connection somewhere in bootstrap of the application (assuming a DI framework):
IPhormSession phormSession = new SqlPhormSession(mySqlConnectionString);
diContainer.Register<IPhormSession>(phormSession);
Well-defined entity DTOs for mapping:
public class Data : ICreateDataItem
{
public long Id { get; set; }
public string Key { get; set; }
public string Value { get; set; }
}
Ideally, one contract per database action:
public interface ICreateDataItem : IPhormContract
{
long Id { set; } // From db
string Key { get; } // To db
string Value { get; } // To db
}
Probably in a repository:
IPhormSession session = diContainer.GetInstance<IPhormSession>();
Data newItem = new Data
{
Key = "Data1",
Value = "Value"
};
bool success = session.Call<ICreateDataItem>(newItem) == 1;
Data[] allData = session.GetAll<Data[]>()!;