Create, Read, Update and Delete Data
- 2 minutes to read
You can create a business logic in the following places:
- In Controllers
- You can declare new and customize existing Actions and handle other Controller events. Once you have obtained or created an Object Space instance (as described in the Ways to Access an Object Space (the Database Context for CRUD Operations) topic), you can use it for data manipulation (create, read, update or delete data).
- In Model (business classes)
You can put logic into property getters and setters, implement methods that are triggered automatically when the object is created, loaded, saved, and deleted (see the IXafEntityObject topic), and declare action methods.
This topic lists articles that describe how to implement common data-aware operations in Controllers with the corresponding Object Space methods, events, and business classes.
Tasks
- Create a New Object
- Load Objects
- Save Objects to Database
- Delete Objects from Database
- Evaluate Scalar Values and Fetch a Portion of Data
- Execute Business Logic When a Property is Changed and Track Modifications in Objects
- Refresh Objects and Rollback Changes
CRUD in XPO
When an XAF application uses XPO, an XPObjectSpace is created. Internally, it uses a UnitOfWork to create, update, and delete business objects. From an XPObjectSpace
, use the XPObjectSpace.Session property to access a UnitOfWork
instance.
When you create a new XPO business object, the XPObjectSpace
‘s UnitOfWork
is passed to the business class constructor. You can use the Session property to access the passed UnitOfWork
instance in a business class. Use this instance to load and create business objects in business classes: Create-Read-Update-Delete (CRUD).
When a new XPObjectSpace
is created, a new UnitOfWork
is also created. An XPObjectSpace
can create a new UnitOfWork
, for example, when you call the IObjectSpace.Refresh method. This UnitOfWork
connects to a Data Layer or Object Layer that corresponds to the current ObjectSpaceProvider.
CRUD in EF Core
Inside an EF Core business class, implement the IObjectSpaceLink interface in the class. XAF automatically assigns an Object Space instance to the IObjectSpaceLink.ObjectSpace
property. For data manipulation in EF Core business classes, use the assigned Object Space APIs listed in the topics above.