Business Layer Deployment
One of the things that I’ve been thinking about quite a bit lately is what type of deployment model best fits into an n-tier architecture.
There are obviously several ways we can deploy a business layer for an application.
We could just go 1 tier and intersperse our business logic with our data layer. This is *clearly* the wrong solution for 99% of projects. The only time that I could even begin to see a justification for something like this is a quick prototype. If this prototype makes it to production (as most do), then this needs to be refactored into one of the other designs.
We could develop our business objects and related data access code in the same assembly as our main project. Ideally, this would probably be in a separate namespace, such as MyProject.Data.
Lastly, we could develop an assembly that contains all of our business objects and data access code that our main project needs and then reference that assembly from the main project.
Typically, I have developed business layers using the third model that I’ve discussed. The argument for this is that the business objects are reusable by another project. Fair enough… in theory. In practice, I’ve usually worked on fairly standalone products, so reuse of business objects and data access classes doesnt happen that much.
So, here’s my conundrum…
When creating an application from the ground up, is there any advantage to externalizing your business layer? Is there a disadvantage to factoring it out of your project at a later point in time if you need it?
What are your thoughts?
GUI Design Patterrns
Earlier today, I stumbled across some links to GUI design patterns. I so wish that developers would spend more time implementing a clean UI rather than just slapping any old shit together.
Jenifer Tidwell’s UI Patterns and Techniques
Welie’s GUI Design Patterns
Fantastic information available there. Enjoy!
[via Jeff Atwood]
Using the Command pattern for undo functionality
Command is a very powerful design pattern, whose intent is to encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
One of the biggest advantages to this pattern is that it decouples the object that invokes the operation from the one that actually knows how to perform it.
Today, I want to show you how to implement undo functionality using this command design pattern. For our example, we will be developing a very simple Notepad clone. Nothing too fancy, but enough to show the power of the pattern.
[ Source Code (.zip) ]
The first thing we want to do is to create an abstraction around our textbox control. In the Command pattern, this abstraction is called the Receiver. The reciever in our example is an object called Document.
class Document { private TextBox textbox; public Document(TextBox textbox) { this.textbox = textbox; } public void BoldSelection() { Text = String.Format("<b>{0}</b>", Text); } public void UnderlineSelection() { Text = String.Format("<u>{0}</u>", Text); } public void ItalicizeSelection() { Text = String.Format("<i>{0}</i>", Text); } public void Cut() { textbox.Cut(); } public void Copy() { textbox.Copy(); } public void Paste() { textbox.Paste(); } public string Text { get { return textbox.Text; } set { textbox.Text = value; } } }
What we have defined with the Document object are the operations that could be performed against this document, completely decoupling this functionality from our main application. If we want to change what happens when we bold a selection, we go to this object, rather than to the presentation code.
Secondly, we will need to design our Command interfaces. Since it is possible that we have commands that will not require undo functionality (for example, Copy), we have created two base classes (Command and UndoableCommand). We’ll see how UndoableCommand ties in a little later in the article. For now, just keep in mind that this is the class to derive from if you need your command to be able to undo itself.
public abstract class Command { public abstract void Execute(); } public abstract class UndoableCommand : Command { public abstract void Undo(); }
As we work through our application and start adding menu items to it, we will start to see that we need a Command object to handle each of this actions. So, to handle bold, let’s create the following:
class BoldCommand : UndoableCommand { private Document document; private string previousText; public BoldCommand(Document doc) { this.document = doc; previousText = this.document.Text; } public override void Execute() { document.BoldSelection(); } public override void Undo() { document.Text = previousText; } }
By creating a document object that wraps the textbox, we were able to completely decouple the object that will invoke the operation (a menu item) from the one that knows how to perform it (the document object).
The remaining command objects are very similar to the above. Because of this, I wont present the entire code here in print, although it is available via the download.
The remaining piece that we will need to bring everything together is a CommandManager. The CommandManager is a very simple class that has an internal stack that keeps track of our commands for our undo functionality.
class CommandManager { private Stack commandStack = new Stack(); public void ExecuteCommand(Command cmd) { cmd.Execute(); if (cmd is UndoableCommand) { commandStack.Push(cmd); } } public void Undo() { if (commandStack.Count > 0) { UndoableCommand cmd = (UndoableCommand)commandStack.Pop(); cmd.Undo(); } } }
We see in the above code, that we only add the command to the undo stack if it is an UndoableCommand. Remember, when I said that we would see how it ties in. Here it is. We dont want commands that dont have any undo functionality to be added to the stack. If the user tries to undo something that doesnt support undo, it would appear to be unresponsive.
The remaining thing that we now have to do is wire up the event handlers for the menu items (and the toolbars if you’re so inclined).
public class MainForm : System.Windows.Forms.Form { private System.Windows.Forms.TextBox documentTextbox; private CommandManager commandManager = new CommandManager(); private Document document; public MainForm() { // // Required for Windows Form Designer support // InitializeComponent(); document = new Document(this.documentTextbox); } // a bunch of snipped code private void cutMenuItem_Click(object sender, System.EventArgs e) { commandManager.ExecuteCommand(new CutCommand(document)); } private void pasteMenuItem_Click(object sender, System.EventArgs e) { commandManager.ExecuteCommand(new PasteCommand(document)); } // etc... }
I’ve made the entire sample application available for download. Keep in mind, at this point, this is a very rudimentary text editor. Your mission, should you choose to accept it, is to add redo functionality to this application.
I hope that I’ve been able to illustrate the power of this pattern and how it could be easily used to add complex functionality. It’s really easy to add new Commands, because you dont have to change any existing objects.
Comments and feedback are welcomed and appreciated…
Session State and Internet Explorer
I think we’ve finally uncovered the root cause of an issue that has been bugging us (no pun intended) for a long time.
In a nutshell, the problem was that sometimes, but not always, the session state from one running copy of Internet Explorer would bleed over into another.
What was finally determined was that as long as you’re sharing the same process (instance on the processes list in your Task Manager), you’ll also be sharing sessions.
So, if you’re in IE and you press CTRL-N, you get a new window within the same process (no extra iexplore.exe in your Task Manager) and thus will share session state.
However, when you double click a shortcut (or the .exe itself) or create an instance via ShellExecute or CreateProcess, you’ll get a new window with a separate process (a new iexplore.exe in your Task Manager) and thus you will not share session state.
Now, the question comes up… What happens when you do window.open via javascript? The answer is that the new window gets launched in the same process space as the original window, and therefore, you *will* end up sharing session information.
Configuration in the .NET framework
A lot of people (me included) use the configuration API in the .NET framework (System.Configuration.ConfigurationSettings.AppSettings) to store configuration settings for their applications.
One thing that I’ve been doing a lot lately is to create an object that provides strongly typed accessors to the data that I store in the AppSettings.
For example, take the following configuration snippet:
<configuration>
<appSettings>
<add key="connectionString" value="server=.;database=myDatabase;ui=user;pwd=password"/>
</appSettings>
</configuration>
Rather than referencing this throughout my entire code base with ConfigurationSettings.AppSettings["connectionString"], I create an object that provides an accessor to this.
class Configuration { public static string ConnectionString { get { return ConfigurationSettings.AppSettings["connectionString"]; } } }
The benefits of this are three-fold:
- Intellisense support for your configuration options.
- “Magic Words” are removed from the code base. This allows you to catch errors at compile time, instead of runtime. For example: lets say that you called
ConfigurationSettings.AppSettings["connectString"]in one spot in your code. Using the object above would have made that a compile error. - Decoupling of your configuration mechanism from the rest of the code base. The rest of your code simply references this Configuration object; you are free to change the implementation of the Configuration object without affecting the rest of the code.
The same idea can also be applied to custom configuration sections. Just add a method or property to your configuration object that calls ConfigurationSettings.GetConfig("yourSection").
This is a very neat technique, I think. I hope that you find it useful as well.
Big news from Mozilla
Mozilla.org today released new versions of three of their applications: Firefox 1.0PR, Thunderbird 0.8 and Mozilla Suite 1.7.3.
Some new things that I’ve noticed: email integration into Firefox and RSS support for Thunderbird (finally).
Firefox has also introduced a new feature called Live Bookmarks, which works with RSS feeds directly in the bookmark panel.
Enjoy!
September 11, 2004
Three years ago today, the United States of America experienced the single worst act of terrorism against itself. I remember that day as if it were yesterday.
I woke up that morning, and just like any other at that period in my life, I got it in the car and drove to the gym. While in the car, I was listening to the radio announcer talking about a plane hitting the WTC.
A few years before that, I remember watching television one evening, without really paying attention to what was on. All of the sudden, something really caught my attention. It was Tom Brokaw’s voice telling me that aliens had landed in New York. Ultimately, this ended up being a 30 minute promotional spot for the movie Independence Day.
Because of this experience a few years ago, the first thoughts that went through my mind were quickly trying to process which new movie this could be promoting. Of course, there was no way that this could have really happened.
The gym was just a mile away, and after I got there, I realized that this was in fact really happening. The girls behind the counter were crying hysterically. I asked what was wrong and they had mentioned that they were watching what was happening on the television. It now registered to me that this was for real.
We were a nation under attack. I stayed there at the gym, watching with everyone else while the second tower fell. I was there when the media announced that another plane had struck the Pentagon. I was there when we heard that a plane went down in a Pennsylvania field.
I went home, and sat in front of the television, yearning for any new information that could shed any light on who and what might have caused this.
I remember the flags put up in everyone’s front yard. People seemed to be a little more friendly. Against what these savages tried to accomplish, the only thing they succeeded in was uniting us as a nation.
I remember President Bush’s words when he addressed the nation. “For the last nine days, the entire world has seen the state of our union, and it is strong”, he declared to thundering applause.
Fast forward three years. Most of the confusion and outrage is still there. Three years of processing this event has not made it any easier. I still get misty eyed every time I see the footage of the days events.
Someone asked the question the other day, “When do you think we wont associate the words ‘nine-eleven’ to that awful day?” I think that it’s very important to never forget that horrendous day.
Let’s never forget the people who died that day. Let’s never forget the soldiers who have died since then protecting the freedoms that we so cherish. Let’s not forget the soldiers that are *still* out there fighting for us and protecting us from these savages. I promise you that they havent forgotten what they’re fighting for.
God bless America…
Announcing Omea Reader
A new aggregator is about to hit the market…
JetBrains, the folks who bring us IntelliJ IDEA and Resharper, have taken a step into the information management arena.
Omea Reader is a tool which combines an RSS/ATOM feed reader, a newsgroup reader and a Web bookmark manager in a single easy to use interface. Omea Reader includes the full range of information management features of Omea Pro, including fast searching, views and categories, flags, annotations and much more. Omea Reader can be extended with new features or even new resource types through its powerful plugin development Open API.
As a limited time offer, you can get a permanent license key for Omea Reader for free, by filling out a simple registration form. After that time, Reader may remain free, or may become a commercial product. Either way, the license is yours to keep.
Omea Reader is currently being offered as a release candidate, but should reach its 1.0 release in a few days. I’ve been using EAPs of Omea Pro as my main information store since the very first EAP release back in February of this year. The difference between Omea Reader and Omea Pro is that the pro version offers Outlook integration.
This is absolutely an incredible tool, and I urge everyone to give it a try.
Who broke the RSS?
I’ve noticed that the RSS feeds both blogs.msdn.com and weblogs.asp.net have deterioriated into displaying only an excerpt of the content. On top of that, a lot of links arent present and formatting is lost.
The whole idea behind RSS is to get the entire content feed, and not have to open my web browser all the time. I’ve gotten down to where I’m only subscribed to two feeds that are not full content.
Honestly, I’ve started to grow tired of the noise on weblogs.asp.net, and have considered dropping it for a while now. This may be the thing that pushes me over the edge. However, I’m not in a huge hurry to drop blogs.msdn.com, so I’m begging whoever did this to fix it. GIVE US BACK THE FULL FEEDS!
*Update:* It appears that Im not the only one thats disgruntled about the new feed formats. Steve Maine has a pretty intense rant about it as well.
*Update:* Scoble has provided some explanation into why this happened, and some ways that we can help fix this.
*Update:* The people have spoken… Scott Watermasysk mentions that full text feeds are back on! Thanks Scott!
GMail Invites
You want GMail invites? I’ll give you GMail invites.
First four people to comment get one each.
*Update*: All the invites are gone. As I get more, I’ll continue to shell them out. Thanks for playing… :=)


