Active Query Builder  »  Blog

February 21, 2018

Using Redis to store server-side objects instead of the ASP.NET session-based storage

The previous version was strictly tied to the ASP.NET session mechanism, so it was difficult to use it in web farms with automatic load balancing between servers. The client was able to work only with the server on which its session was started since it contained an instance of the QueryBuilder object for this user. Now, this restriction is in the past.

The default behaviour of saving the component state within the ASP.NET session remains the same as it's fast and convenient for simple apps, but now you can change it easily. Let's look how an instance of the QueryBuilder and QueryTransformer object is obtained in the demo projects. The QueryBuilderStore factory uses the SessionStore object under the hood:

public ActionResult Index()
    // Get an instance of the QueryBuilder object
    var qb = QueryBuilderStore.Get();

    if (qb == null) {
        qb = new QueryBuilder();
        // initialize new instance here


What you can do now is to replace the storage provider with your own implementation of IQueryBuilderStorageProvider interface which is defined as follows:

public interface IQueryBuilderStorageProvider
    QueryBuilder Get(string id);
    void Put(QueryBuilder qb);
    void Delete(string id);

Then you can replace the default SessionQueryBuilderProvider with your own during the application startup:

public class MvcRazor : HttpApplication
    protected void Application_Start()
            QueryBuilderStore.Provider = new QueryBuilderRedisStoreProvider();

and then use it in almost the same way as you did that before!

The state of the QueryBuilder object can be serialized and deserialized using just one LayoutSQL property since the version 3.1. All you have to do is to redefine the Get, Put and Delete methods and make one additional step: after deserializing an instance of the QueryBuilder object, you may need to assign a database connection or pass the preloaded content of MetadataContainer to it.

The following implementation lets store the component's state in Redis:

    using StackExchange.Redis;

    public class QueryBuilderRedisStoreProvider : IQueryBuilderProvider
        private readonly IDatabase _db;

        public RedisQueryBuilderProvider()
            var redis = ConnectionMultiplexer.Connect();
            _db = redis.GetDatabase();

        public QueryBuilder Get(string id)
            var layout = _db.StringGet(id);

            var qb = new QueryBuilder(id);

            if (layout.HasValue)
                qb.LayoutSQL = layout;

            return qb;

        public void Put(QueryBuilder qb)
            _db.StringSetAsync(qb.Tag, qb.LayoutSQL);

        public void Delete(string id)
            _db.StringSetAsync(id, "");

The new CustomStorage demo projects illustrates this functionality by saving the component's state in a SqLite database.

at ActiveDBSoft.
» All Active Query Builder news

Comments by Disqus:




Join Types

Automatic Joins Creation
Active Query Builder automatically determines relationships between tables and creates appropriate joins for them.

Different Join Types
Active Query Builder allows to define different join types and various server-specific query options visually.

Unions and Sub-queries

Unions and Sub-Queries
Unions, sub queries and derived tables can be accessed and built visually in Active Query Builder as easy as the main query.

Grouping, Sorting and Criteria
Active Query Builder allows to define grouping, sorting, and constructing criteria in a simple and direct way.

Active Query Builder Screenshots

Social networks