Service Logic
Service Logic executes on create, read, update or delete of an entity.
Service classes can be added to the Project.Services/Logic folder.
Logic Stage
Service Logic can execute at the listed stages below.
- PreOperation: After security validation and before the save to the database
- PostOperation: After the save to the database
Create Service Logic
To create Service Logic, create a new file in the Project.Services project in the ServiceLogic folder.
Project.Services / Logic / MyEntityService.cs
The ServiceLogic attribute takes the following parameters.
- TableName: The name of the entity the service logic will execute on. '*' for all entities.
- DataOperations: Flags that determine what data operations this should execute on, create, read, update, and delete.
- LogicStages: Flags that determine which stages the service logic should exeucte on, PreOperation, or PostOperation.
- Order: Service Logic classes executing on the same table, data operation, and logic stage, will execute following the order.
ServiceContext
ServiceContext has a number of properties that can be used during execution.
Project.Services / Logic / MyEntityService.cs
Modify Records
Use the Create, Update, and Delete methods of the ServiceContext to modify records. While it is possible to modify records directly using the Entity Framework DbContext, using the ServiceContext ensures that the service logic is executed.
Project.Services / Logic / MyEntityService.cs
Performance
For optimal performance, prioritize using PreOperation
logic whenever possible. This allows the database save to be delayed until all records are processed, or until an entity with PostOperation
logic is triggered.