In my previous post, I talked about rendering partial views asynchronously. In this post, I will talk about the benefits of this approach by adding more performance improvement measures.
As you might recall from my previous post, I showed an example of a controller action that renders a partial view. This action then was called by some jQuery code, which injected the HTML that was returned from the action. This approach also allows for granular output caching. Instead of caching an entire page, what you really want is to only cache certains sections of your page. This article explains all you need to know about output caching. In my example, we have the following situation:
The Index action just renders some HTML without any business logic. However, the GenerateSlideDeck method does contain business logic (i.e. data from the database). In my case, the data that is returned is fairly static data, which means caching is appropriate. Implementing Output Caching is very easy: just add an attribute above the action and you’re all set. In this example, I decided to set the expiration date to 4 hours:
For business logic that is not so static, output caching is probably not the best method. Don’t worry though, there are plenty of other tips and tricks.
Run in Release mode
This is a simple trick that should not be forgotten: deploy your application in release mode, not debug mode.
Bundling, minification and CDN
This is a relatively new feature that was released in .NET 4.5, but there is a lot of documentation available. When I have to create an application that requires data storage (in other words, every time), I leverage the repository pattern. I believe it is the best implementation of a data access layer. This approach makes it easy to implement asynchronous operations. In my synchronous repository class for Entity Framework, I have the following method:
This is the asynchronous way:
The conversion from synchronous to asynchronous comes down to the following steps:
- Wrap the return type type with the Action type
- Add ‘async’ to the method header
- Call the ToListAsync() method instead of ToList()
- Add the await keyword before calling the ToListAsync() method.
These steps also must be applied for all other methods that call this asynchronous method. An example is already visible above.
Many performance improving measure are also good for the architecture of your application. Some of the tips and tricks are easy to implement while others require more attention and coding. However, performance is a very important but underestimated part in software development. For more tips and tricks, please refer to this site