For the past three years, I have been working on a line of business application targeted to manage and run cities and towns. As you can imagine this has a very large data model and it continues to grow. One of the challenges of using Silverlight and Entity Framework over WCF RIA Services is that as the model grows the application seems to get slower and slower for the initial data request of a Domain Context. There are some very cool tricks that you can do to pre-compile your views so that your initial data request is no slower than any other request.
I am going to show you a couple approaches that are available to you right now and review each of them with pros and cons to each.
The first approach comes from a MSDN article How to: Pre-Generate Views to Improve Query Performance and does a great job at walking you through the steps required to get this accomplished. I am not going to go into the steps required for this approach but I believe that this approach involves too much ceremony and configuration in order for it to be the best choice. Here are some of my thoughts as to why I don’t like this approach:
- I don’t like the requirement of needing to have a pre-build event. As you have more .edmx files this entry area becomes very cumbersome.
- I don’t like that I need to change the default behavior of the .edmx model Metadata Artifact Processing to Copy to Output Directory. As more and more .edmx files are needed it is very easy to forget this step.
- Because you have your model being copied to the output directory, you need to bring in the .csdl, .ssdl, and .msl files from the output directory and add each of them as an Embedded Resource. Once you do this you will also need to change your connection string as indicated in the article accordingly.
Even though this isn’t my favorite approach, it works and does a great job of pre-compiling your views.
If you have already read the first article, you will notice that it points to another option, How to use T4 template for View Generation. This approach uses T4 templates to provide the pre-compiled views. The T4 templates are available for download from the link provided. This method has none of the negative points that I mentioned in the first article. You have to be sure that when you bring in the T4 template that you name it the same as your model, e.g. .Views.tt. You must also be sure that the file that is generated is included as part of your project. I like this approach many times over the previous approach since it doesn’t involve as much ceremony and configuration to get it working. NOTE: If you have updated your Entity Framework model and DomainService, you need to be sure to remember to right-click your .Views.tt file and choose “Run Custom Tool” to make sure that your pre-compiled views are up to date.
You are now armed with enough information to go and pre-compile your views and make your application run quickly again. I spent quite a bit of time researching and looking for good blog posts on this and want to list a couple of them for you here so that you can gain some understanding when and where you will actually see some dividends from all of your efforts based on the size of your model.
The following is a table of links that have the name of the post and a description of what it is about:
|Isolating Performance with Precompiled/Pre-generated Views in the Entity Framework 4||This post goes into great detail analyzing sample data and providing benchmarks on performance.|
|Solving the “No logical space left to create more user strings” Error and Improving performance of Pre-generated Views in Visual Studio .NET 4 Entity Framework||Discusses potential issues with the pre-compilation process and shows a way to improve on performance along with providing a tool (MakeRes.exe) to get this accomplished.|
|Connection Strings||MSDN article explaining Entity Framework connection strings. I used this when I was working on the first implementation. I also dynamically create my connection strings instead of using the web.config file so this was useful.|
|EDM Generator (EdmGen.exe)||MSDN article explaining the use of the EdmGen.exe tool.|
|How to: Use EdmGen.exe to Generate the Model and Mapping Files||MSDN article walking you through an actual generation of the .csdl, .ssdl, and .msl files.|
I recommend that if you are planning on pre-compiling your views, you should also consider using the MakeRes.exe tool and use the dictionary based solution instead of the default views that are generated for you.
That is all there is to it. You will be happy to know that you will not have to make any changes from your client code as all this happens on the server.
I hope this helps out and thanks for reading…