Skip to main content
.NET 6.0+

XafApplication.CreateCustomModelCacheManager Event

Occurs when the object used to manage saving and loading the Application Model cache is created.

Namespace: DevExpress.ExpressApp

Assembly: DevExpress.ExpressApp.v24.1.dll

NuGet Package: DevExpress.ExpressApp

Declaration

public event EventHandler<CreateCustomModelCacheManagerEventArgs> CreateCustomModelCacheManager

Event Data

The CreateCustomModelCacheManager event's data class is CreateCustomModelCacheManagerEventArgs. The following properties provide information specific to this event:

Property Description
ModelCacheManager Specifies the object that manages the Application Model cache.

Remarks

By default, the Application Model content is cached to the Model.Cache.xafml file when the XafApplication.EnableModelCache property is set to true. To use a custom storage for the cache, handle the CreateCustomModelCacheManager event and pass a custom ModelCacheManager object to the CreateCustomModelCacheManagerEventArgs.ModelCacheManager parameter.

Tip

To change the cache file location, override the XafApplication.GetModelCacheFileLocationPath method instead of using this event.

The following example demonstrates the custom ModelCacheManager implementation.

using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
// ...
public class MyModelCacheManager : ModelCacheManager {
    string fileName = "MyCacheFile.bin";
    public MyModelCacheManager() : base(null, AppDomain.CurrentDomain.SetupInformation.ApplicationBase) { }
    public MyModelCacheManager(Stream stream, string modelCacheFileLocationPath)
        : base(stream, modelCacheFileLocationPath) {
    }
    protected override bool CanLoadModelCache() {
        return File.Exists(fileName);
    }
    protected override IDictionary<string, string> LoadCore() {
        Dictionary<string, string> serializedModel = null;
        using (FileStream loadStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) {
            BinaryFormatter bf = new BinaryFormatter();
            serializedModel = bf.Deserialize(loadStream) as Dictionary<string, string>;
        }
        return serializedModel;
    }
    protected override void SaveCore(Dictionary<string, string> serializedModel) {
        using (FileStream saveStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(saveStream, serializedModel);
        }
    }
}

To use this custom implementation, handle the CreateCustomModelCacheManager event in the Program.cs (Program.vb) file:

winApplication.EnableModelCache = true;
winApplication.CreateCustomModelCacheManager += delegate(object sender, CreateCustomModelCacheManagerEventArgs e) {
    e.ModelCacheManager = new MyModelCacheManager();
};
winApplication.Setup();
winApplication.Start();

The Application Model cache is not used when the debugger is attached (see Debugger.IsAttached). So, the CreateCustomModelCacheManager is not triggered when you debug your application in Visual Studio. If you need to temporarily enable caching to debug your custom code, set the ModelCacheManager.UseCacheWhenDebuggerIsAttached field to true in the WinApplication.cs (WinApplication.vb) file.

ModelCacheManager.UseCacheWhenDebuggerIsAttached = true;

When you finished with debugging, remove this line. The model cache does not speed up your application startup in Visual Studio because the modules versions are constantly updated, and consequently, the cache is always recreated.

See Also