M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

M# Data Caching

Effective usage of caching helps increasing scalability and performance of an application, by storing related data as close to the data consumer as possible and by eliminating repetitive data processing, such as fetching from a data source and transporting it to the UI. In this tutorial we will examine how M# manages data caching. It is very important to understand data caching mechanism in order to write effective and better data queries e.g. get, save, delete etc. More information on data access methods of M# is available in chapter 4 "Working with Data".

Fetching Data

Fetching an instance by "ID" from the database is performed using "Database.Get()" and because the "ID" of the record is a primary key, it doesn’t change, so M# caches every single instance returned by "Database.Get()" method, if the method call is not under any transaction scope. "Database.Get()" always tries to get the data record from the data cash before querying database and which point it is added to the data cache.

"Database.GetList()" method is provided in M# data access framework to fetch a collection of records from the database. M# only caches the result set if the criteria provided is not dynamic in terms of its value, if the criteria provided cannot be translated into direct SQL query e.g. if you query database with a criteria of type "DateTime" with a value of "DateTime.Now" then the result set will not be cached.

The underlined part in the above shown code contains criteria, where the value is always changing. In order to make this code tuned for better performance you must write the 2nd criteria in "Where" Linq extension method as shown below:

Persisting Data Change

Because data is cached when it is fetched form the database, it makes the next data queries much faster but it also raises a question mark on data integrity, that what happens to data when a user updates the data coming from cache?

M# framework provides a method Database.Update(), which must be used to update an existing record. This method internally clones the given instance and calls the Database.Save() on the cloned instance, which after saving the changes, removes it from data cache based on "ID". So, the next time the record is fetched from database it is again cached maintaining the data record state and performance of application.

Important: Calling "Database.Save()" method on an existing record could result in data corruption because you change data that is on a cache, if the save action fails then the instance on the cache would different than in the database as memory cache is not run in transaction. It is always advised to call "Database.Update()" method whenever persisting changes to the data instance.

Direct Database Change

Changing data directly in the database by not using M# framework will not be reflected to the application. Applications using M# data access framework always query data from the data cache first and therefore the changes submitted without updating cache will not able available to the system. You must refresh the data cache in order to reflect changes made directly. It is highly advisable to always commit data using M# framework apart from very exceptional cases.