There is a pattern in ASP.NET - whenever there is a piece of markup that generates code (like the .aspx
/.ascx
files in WebForms or .cshtml
files in MVC3), these files are dynamically compiled at runtime. aspnet_compiler will produce another assembly for them, which references your code-behind assembly.
This approach seems awkward to me and I don't understand why it hasn't been discontinued already. A much better approach (in my opinion) is like in Winforms or resource files - you have your .whatever
file, and then there is .whatever.desginer.cs
file. This designer file is created at runtime as you type. When you compile, the compiler doesn't care about your .whatever
file, it just takes the .whatever.designer.cs
file and produces a single solid assembly.
This provides several benefits:
- You can inherit your forms from each other, similar to windows forms;
- You can always see the code that is being generated, possibly adjusting your markup to generate better code;
- You can easily instantiate strongly typed instances of your forms;
The only benefit I can see from dynamic compilation is that
- You can change the markup file anytime and don't need to recompile the app.
To be honest, I've often wanted for one of the first three benefits, but never for the last one. Recompiling the app to see your changes isn't that big of a deal. Especially since you have to do it anyway when working with code-behind, where the most of your time will be. And when you deliver your app to the client, you deliver it as a monolithic, precompiled block. When you update, you update everything, not just individual .aspx
files - there's no point to.
So... why is it like this? What am I missing?