One of the common activities that I have found myself doing lately whenever I create a new project is to identify a way of loading configuration information and customizing the behavior of the Master Page. The way I initially went about solving this problem was to create a base class and have all of my Page ViewModel’s inherit from it. This caused two different things that I had to constantly maintain, making sure all of my ViewModel’s inherited from this MasterViewModel and then remembering to set the data every time a ViewResult is returned in an Action.
public class MasterViewModel : IMasterViewModel
{
public string SiteName { get; set; }
}
I was able to take care of the second concern by simply creating a base controller that all of my controllers would inherit from and then override View(string viewName, string masterName, object model) { … } so that the settings are automatically injected into the model.
protected override ViewResult View(string viewName, string masterName, object model)
{
((MasterViewModel)model).SiteName = "Example";
return base.View(viewName, masterName, model);
}
In order to get around the smell of forcing my ViewModel’s to inherit from a base class I put the MasterViewModel into the ViewData dictionary that is returned in the ViewResult.
protected override ViewResult View(string viewName, string masterName, object model)
{
ViewResult result = base.View(viewName, masterName, model);
result.ViewData[Data.Site] = _blogConfiguration.Configuration;
return result;
}
Once I had this in place, I created a new ViewMasterPage with a property that exposed the data that was put into ViewData. This then allowed me to have a strongly typed referenced from the Master Page.
<%@ Import Namespace="Azure.Domain.Models"%>
<%@ Master Language="C#" Inherits="Azure.Web.Views.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title><%= SiteConfiguration.Name %><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.1.min.js" type="text/javascript" language="javascript"></script>
<script src="http://ajax.microsoft.com/ajax/jQuery.Validate/1.6/jQuery.Validate.min.js" type="text/javascript" language="javascript"></script>
<asp:ContentPlaceHolder ID="HeaderContent" runat="server" />
</head>
<body>
<div id="header">
<h1><%= SiteConfiguration.Name %></h1>
</div>
<div id="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
<div id="footer">
©<%= DateTime.UtcNow.Year %> <%= SiteConfiguration.Owner %>
</div>
</body>
</html>
All examples are taken from the work I have been doing as a part of creating my own blog engine on Azure. You can track my progress and grab source code on my AzureBlog project site on CodePlex.
Filed under: ASP.NET MVC | 4 Comments
Tags: ASP.NET MVC
jQuery drop down menu
The other day at work I found myself making a custom drop down list that did filtering on a table when an item was selected. I’ve since enhanced this plugin so that instead of doing the filtering itself, it will perform a callback with the selected item.
To see this in action check it out here.
You can see the source code for the plugin here.
To use the plugin it is as simple as making a table and putting this into one of the header cells.
<th>
<ul class="menu">
<li class="">All</li>
<li class="jquery">jQuery Functions</li>
<li class="object">Object Accessors</li>
<li class="data">Data</li>
<li class="plugin">Plugins</li>
<li class="interop">Interopability</li>
</ul>
</th>
Then to initialize the plugin just execute the following script.
<script type="text/javascript" language="javascript">
$(".menu").menu();
</script>
Filed under: Uncategorized | Leave a Comment
My world has not involved doing very much with SQL Server for a while now. This has been changing a lot since I took my new job working on MSDN Forums where I have jumped head first into the area of Service Broker, exhibit 1. In addition to starting a new job, I have been digging into Domain Driven Design and other ways of architecting a high performant web application. This has led me to think a lot about how message passing and asynchronous notification via events using a pub/sub pattern. In my new job we are using SQL Server Service Broker to queue notifications and other actions that are performed in the system in an asynchronous fashion. While I traditionally see MSMQ being used in this type of scenario, I found it a very interesting approach to using Service Broker which is different from the traditional data orientation used with Service Broker. I would like to do some experiments to see how this could compare with and compliment something like Mass Transit and nServiceBus.
Filed under: Data, Domain Driven Design | Leave a Comment
Tags: service broker, sql, DDD
Source Code Test
Trying out the new source code feature by posting a chop kata I did a while back.
public class Chop1
{
public static int Chop(int value, int[] values)
{
if (values == null || values.Length == 0)
{
return -1;
}
if (values.Length == 1)
{
if (values[0] == value)
{
return 0;
}
return -1;
}
var middle = values.Length / 2;
if (values[middle] == value)
{
return middle;
}
if (value < values[middle])
{
var newArray = new int[middle];
Array.ConstrainedCopy(values, 0, newArray, 0, middle);
return Chop(value, newArray);
}
if (value > values[middle])
{
var size = values.Length - middle;
var newArray = new int[size];
Array.ConstrainedCopy(values, middle, newArray, 0, size);
var result = Chop(value, newArray);
if (result != -1)
{
return result + middle;
}
}
return -1;
}
}
And here are my tests.
[Test]
public void FourItems()
{
Assert.AreEqual(0, Chop1.Chop(1, new[] { 1, 3, 5, 7 }));
Assert.AreEqual(1, Chop1.Chop(3, new[] { 1, 3, 5, 7 }));
Assert.AreEqual(2, Chop1.Chop(5, new[] { 1, 3, 5, 7 }));
Assert.AreEqual(3, Chop1.Chop(7, new[] { 1, 3, 5, 7 }));
Assert.AreEqual(-1, Chop1.Chop(0, new[] { 1, 3, 5, 7 }));
Assert.AreEqual(-1, Chop1.Chop(2, new[] { 1, 3, 5, 7 }));
Assert.AreEqual(-1, Chop1.Chop(4, new[] { 1, 3, 5, 7 }));
Assert.AreEqual(-1, Chop1.Chop(6, new[] { 1, 3, 5, 7 }));
Assert.AreEqual(-1, Chop1.Chop(8, new[] { 1, 3, 5, 7 }));
}
[Test]
public void EmptyArray()
{
Assert.AreEqual(-1, Chop1.Chop(3, new int[] { }));
}
[Test]
public void SingleItem()
{
Assert.AreEqual(-1, Chop1.Chop(3, new[] { 1 }));
Assert.AreEqual(0, Chop1.Chop(1, new[] { 1 }));
}
[Test]
public void ThreeItemsNoMatch()
{
Assert.AreEqual(-1, Chop1.Chop(0, new[] { 1, 3, 5 }));
Assert.AreEqual(-1, Chop1.Chop(2, new[] { 1, 3, 5 }));
Assert.AreEqual(-1, Chop1.Chop(4, new[] { 1, 3, 5 }));
Assert.AreEqual(-1, Chop1.Chop(6, new[] { 1, 3, 5 }));
}
[Test]
public void ThreeItemsMatchFirst()
{
Assert.AreEqual(0, Chop1.Chop(1, new[] { 1, 3, 5 }));
}
[Test]
public void ThreeItemsMatchMiddle()
{
Assert.AreEqual(1, Chop1.Chop(3, new[] { 1, 3, 5 }));
}
[Test]
public void ThreeItemsMatchLast()
{
Assert.AreEqual(2, Chop1.Chop(5, new[] { 1, 3, 5 }));
}
Filed under: Uncategorized | Leave a Comment
Tags: kata
What’s a Value Object?
I have been reading Eric Evan’s Domain Driven Design along with trying out ideas on little projects that I am doing on my own. I have recently been trying to figure out how to create and model Entity objects with their associated Value objects. In this case I started with a simple Profile entity. For the purpose of this example just imagine Profile as an online user profile for some sort of public online community website, this could be an extension of the ASP.NET Membership Profile Provider.
In this example it is easy to say that Affiliation, Avatar, and Identity are good candidates as having their own data type. There are still two questions I have about the values associated to the Profile entity.
- Should the value objects associated to the entity be immutable?
- Should the simple properties use simple data types or a more complex data type that is customized?
Immutability
For the case of this blog post I am going to assume that all of the properties are going to be immutable. I plan on following up on this question on a later post.
Data Types
In my previous experiments with Domain Driven Design I used the approach of using a simple data type for the value type properties where appropriate. In these experiments I also tried to abide to a purely immutable property approach. In this experiment I went with the following model for creating the value objects:
In addition the value objects also implement ToString as well as implicit overloads for their simple data type. For example here is the code for StringValueObject.
This still isn’t really how I tried my experiment, in reality I actually added another layer such that it looked similar to this inheritance structure.
Some of the initial advantages I thought I would get from this are:
- Easier to control Null values.
- Values will always resolve using dotted notation.
- Values can be implicitly casted to their simple value types.
- Values can be extended without affecting the entity interface. For example the display name can be changed to be composed of First and Last name.
- Focus is kept on mapping to the domain instead of mapping to simple data types.
- Data validation is the responsibility of the value object class.
To finish it off, this is how I currently have my Profile entity object looking.
Since this is still an experiment to learn how to better model my domain, I do not yet know if this is one horrible idea or will prove to have far more benefits than I listed above. If you have any ideas please leave a comment as I am eager to learn more.
Filed under: Domain Driven Design | 3 Comments
Tags: Domain Driven Design, Value Object
Microsoft has been making DirectAccess available to its employees which was released in Windows 7 and Windows Server 2008 R2. The great thing about DirectAcess is that I no longer have to VPN into work, Microsoft still makes me authenticate with my Smart Card. This allows me to access internal resources whenever I want.
Problem
I have no idea how DirectAccess works, but I do know that SubVersion doesn’t work. After giving up on trying to get it to work I took another stab at it today. The error I was getting was the following: Could not resolve hostname ‘my.svn.server.at.microsoft’: The requested name is valid, but no data of the requested type was found. I could ping the svn server and it resolved without any problems. I could also access the SubVersion server through my browser. It wasn’t until I looked carefully at my ping results that I noticed that the IP address being used was IPv6.
I did some searching and found that SubVersion has special code that is used for doing IPv6 requests but that it is only enabled through compile time options. I took a try at downloading the source code to compile it myself, but soon realized that I’m way too lazy to try to get this to work. So I did some more Binging and found that even though there are four sources listed for Windows they are all based on the CollabNet binaries and they have not enabled IPv6 in their build. SlikSVN is different, they have enabled IPv6 by default in their build. In fact TortoiseSVN even turned in on when they were building version 1.5.0 as version 1.5.0-alpha1 had it enabled but was then shut off when 1.5.0 was shipped.
Version 1.5.0- CHG: Support for IPv6 disabled due to problems with merging and terrible slowdown in some situations. (Stefan)
Version 1.5.0-alpha1- NEW: Support for IPV6. See issue #352 for details. (Stefan)
Solution
After uninstalling the CollabNet version of SubVersion and installing SlikSVN I have now been able to connect to the SVN server through DirectAccess. This forces me to use the command line for checkout, pull, and commit, but I can still use TortoiseSVN and VisualSVN features for all local actions without any problems.
Filed under: Uncategorized | 2 Comments
Tags: DirectAccess, svn, Windows 7, Windows Server 2008 R2
Are you using BootCamp to run Windows on your MacBook? Have you tried installing applications and they failed saying that they don’t have permissions? And do you have an OS X partition? If so you have been a victim of a bad installer. To get around this you have to make the OS X partition not have a drive letter, which happens to be the default behavior after you install the Boot Camp utilities.
To remove the drive letter from the OS X partition, perform the following steps.
1) Launch Computer Management.

2) When it shows up, expand Storage on the left and then select Disk Management.

3) To remove this, right click on the HFS drive and select Change Drive Letter and Paths... This is usually the E: if you did the default installation of OS X and Windows.
4) Now select the drive letter and click on Remove.

5) Click on Ok. When the drive letter is removed it will look similar to the following:

Filed under: Uncategorized | 1 Comment
Tags: .NET, Boot Camp
Who needs a Relational Database?
Ever since working on IMM I have enjoyed the fact that I haven’t needed to design a database or write any data access code. Having started working on a small side project I was in a situation where I needed to persist data. My first reaction was to use a simple relational database so I started modeling my domain objects in SQL. I soon realized that I was violating DRY. I thought I could try using nHibernate but that would still require building a database model. I instead decided to go with Windows Azure Table Storage but ended up having trouble getting it to work. Instead I started looking for something with a nice REST interface. I came across Amazon SimpleDB but decided against it, for now. Instead I decided to go with CouchDB. The reason I did this is because I don’t need to worry about defining a schema and access is very fast.
Now being completely honest here, I didn’t stay on CouchDB as I was hoping to hand off the project t some other people at Microsoft so I ported it Entity Framework 1.0. After doing this port I found that I had to destroy my domain model and I had to constantly fight for any level of purity. After a while of fighting this I decided to try out nHibernate after watching Ayende teach Rob Conery how to use it. Midway through the first episode I had it working.
Filed under: Data | Leave a Comment
Tags: couchdb, nhibernate
1: private static string RetrievePassword()
2: {
3: string password;
4: StringBuilder passwordBuilder = new StringBuilder();
5: ConsoleKeyInfo keyInfo;
6: while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter)
7: {
8: if (keyInfo.Key == ConsoleKey.Backspace)
9: {
10: if (passwordBuilder.Length > 0)
11: {
12: passwordBuilder.Remove(passwordBuilder.Length - 1, 1);
13: }
14: }
15: else
16: {
17: passwordBuilder.Append(keyInfo.KeyChar);
18: }
19: }
20: password = passwordBuilder.ToString();
21: return password;
22: }
Filed under: Uncategorized | Leave a Comment
Tags: .NET, CLI, Password
Book List
Need to do a post to remember where I found these lists of books that I should read.
- Top 100 Best Software Engineering Books, Ever (June 10, 2008)
- Top 50 New Software Development Books (March 4, 2009)
Filed under: Links | Leave a Comment
Tags: Books
Search
-
Links
Recent Entries
- Passing Configuration Data to the Master Page in ASP.NET MVC
- jQuery drop down menu
- Thinking out loud about Service Broker Queues
- Source Code Test
- What’s a Value Object?
- DirectAccess, SubVersion, and IPv6
- Installing .NET Framework 4 Beta 2 on MacBook Pro
- Who needs a Relational Database?
- Retrieve Password from Command Line
- Book List
- Manifesto for Software Craftsmanship
Categories
- ASP.NET MVC (1)
- Data (2)
- Domain Driven Design (2)
- Links (1)
- Software Craftsmanship (1)
- Uncategorized (5)

