Why does blogger.com screw up my code? Does anyone out there know a good way to post code?
I've yet to find a good way to post an attachment.
Why does blogger.com screw up my code? Does anyone out there know a good way to post code?
I've yet to find a good way to post an attachment.
They say it's the only safe way... use a barrier method every time.
Seriously, though. I was pleased to see that .Net 2.0 contained an implementation of Semaphore. It contains a lot of cool stuff. Something it doesn't have, though, is a barrier. Barriers are a synchronization tool that can be used to ensure no thread starts working until the desired number of threads are ready.
A good real life analogy for a barrier is the starter in a race. The starter makes sure that all of the runners (threads) are at their marks. When all of the contestants are ready, the starter fires his fake pistol and they're off. That's what the barrier does. It's not the most commonly used synchronization tool - I use it mostly in tests - but it can come in handy.
Below, I've implemented a barrier as a wait handle. I commented it a little but, frankly, I'm tired. It should be pretty clear how it works.
Google blogging screws up one's code, so I've moved the example to a seperate text file.
Today, we're developing a web part. This web part has the requirement that administrators be able to edit the user interface through the web UI. Since more than instance may be deployed to a single web site and one web site can span more than one host, we cannot simply provide an editor for a file.
What we really need is the ability to treat one of our web part's properties as a user control. Of course, user controls are useless if they can't have scripts or databinding in them, so we cannot simply use TemplateControl.ParseControl; we have to use LoadControl. Now we see our problem: LoadControl requires a file but we want to persist our control as a property.
The solution ends up being pretty simple: We store the body of our skin as a property of our web part with freshness data. In the solution provided here, our freshness data is a Guid and the body of our skin is the text portion of a user control (we are foregoing code-behind, for now). Every time an instance of our web part is rendered, it tries to load its skin from a cache directory (which is also specified via property) using the Guid as its name. If the file does not exist, we create it.
The result is, effectively, every time an administrator saves a change to a web part, a new use control will be created on every host. Since the user control is loaded from a file, it will get the advantage of ASP.NET's caching & compilation infrastructure; meaning it can have custom code and data-bindings in it.
Unfortunately, Google Blogs screws up source code. You can download an example here.
A nightly job can clean up old items by simply deleting all the files in the cache folder: the ones worth saving will get regenerated the next time they are needed anyway.
"Why?" you ask, or maybe, "Why not just use files?" I cannot answer that. I mean: I know the answer; I just can't give it to you right now. There are some out there who know why already. To anyone who asks "why" the only legal answer I can give you is "you'll see."
Messages from the Controller should be passed to the skin via method calls. Messages from the skin should be posted via an event. Shared data should be referenced by properties. Once you have defined the contract between your controller and its skin, you may then implement the skin and controller in whatever order and manner you choose. The end result is that you have a modular system in which new user interfaces can be deployed independantly of the application itself.
A code example of how this might be implemented will be posted in the near future.