The new XPO tutorials for ASP.NET Core MVC and Razor Pages with DevExtreme are out so of course I could not wait to test them out. I followed their instructions here and had them running in no time. And it was a beauty but that is not what this article is about. Going trough the source code I stumbled upon this:
unitOfWork.GetObjectByKeyAsync and it reminded me of a mini series I have been wanting to do for a long time. I had a lot of support tickets bookmarked it for this so I guess it is finally time. The article ORM Hate from Martin Fowler which I just recently re-read was also a clear trigger to finally get it done (or at least start it).
The idea is simple, just a collection of Tips and Tricks that will help us to use XPO more efficiently. For example:
- When to use GetObjectByKey instead of FindObject.
- XPCollection vs XPQuery
- XPView and XPDataView
- Prefetch data for calculated fields
- Why it is generally a bad idea to use references in a primary key
- Identity Map
- Direct SQL Queries
- Mapping Database Views
- All methods in the Create, Read, Update and Delete Data topic
- And more…
I know we have the articles:
And a lot of others support tickets going over this (my bookmarks are living proof) but I feel they sometimes get a lit bit overwhelming for the new user, I know they were for me when I started it.
Ok no more rambling around, let’s start with the first one.
The GetObjectByKey method searches in memory for the object with the specified value of the key property. If such an object is found, it is not reloaded. To get the up-to-date object from the data store, use the overloaded GetObjectByKey method with the alwaysGetFromDb parameter set to true.
The FindObject method searches for the first object of the type designated by the specified generic type parameter which matches the specified criteria. Criteria are evaluated on the data store side. Objects created and modified within a transaction (Cache), are not processed by the criteria. Instead, their images, stored in the data store, are processed by the criteria.
That info above it is exactly by the documentation, so let’s summarize it:
Find Object always query the database.
GetObjectByKey check the cache and if it finds the object it will not query the database.
Except GetObjectByKey and XPCollection.Filter all other methods (FindObject, GetObjects, XPCollection.Reload, etc.) execute criteria by sending a query to the database.
And that is all for now. Quick, simple, but it gets the momentum going. Until next time. XPO out!