For those who have not yet seen Stackoverflow or Twitter’s Notification Panels. You are missing out.
It’s a neat way to display messages to your users without being too intrusive.
There has been quite a lot of noise regarding ASP.NET MVC with the release of version 2.0. Not to mention the amount of noise the JQuery community has been making. This usually follows by the line, “JQuery is the best thing since sliced bread :)”
Since ASP.NET MVC is not a replacement for ASP.NET Webforms, let’s explore how we can incorporate a notification bar in ASP.NET Webforms.
You can use the following method with any JQuery type notification panel/bar but for this sample grab the Notify Bar from Dmitri Smirnov.
1. Include the jquery.notifyBar.css and jquery.notifyBar.js in your master page
2. Add a static class to your project e.g. (Note the use of extension methods here)
/// <summary>
/// Notification Helper
/// </summary>
public static class NotificationHelper
{
/// <summary>
/// Show Message
/// </summary>
/// <param name="message"></param>
public static void ShowNotification(this System.Web.UI.Page page, string message)
{
page.ClientScript.RegisterStartupScript(typeof(Page),
"notificationScript",
"<script>$(function () {$.notifyBar({html: '"+message+@"',delay: 2000,animationSpeed: 'normal'});});</script>");
}
}
Now in your Pages you could just call the
Page.ShowNotification(“Hello World!”);
c3b90612-d552-4373-b120-db4890ca89de|0|.0
First we will need to setup the JsonResult controller action.
///
/// Get Models
///
///
///
public JsonResult GetModels(string id)
{
JsonResult result = new JsonResult();
var filteredModels = from model in homeViewModel.Models
where model.MakeID.ToString() == id
select model;
result.Data = filteredModels.ToList();
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return result;
}
This method now gives us the ability to use the nifty $.getJSON jquery call.
The signature for the call is as follows
jQuery.getJSON( url, [ data ], [ callback(data, textStatus) ] )
Given that we have setup 2 Drop Down Lists, one for Makes and the other for Models, like so.
we can include the following bit of jquery
//Hook onto the MakeID list's onchange event
$("#MakeID").change(function() {
//build the request url
var url = '' + "Home/GetModels";
//fire off the request, passing it the id which is the MakeID's selected item value
$.getJSON(url, { id: $("#MakeID").val() }, function(data) {
//Clear the Model list
$("#ModelID").empty();
//Foreach Model in the list, add a model option from the data returned
$.each(data, function(index, optionData) {
$("#ModelID").append("" + optionData.Description + "");
});
});
}).change();
CascadingDropDownList.zip (365.72 kb)
8e9ecd79-e81d-45a1-bcfc-0f862fa90a2b|0|.0
It’s difficult to remember the principles of the paradigm when you are bombarded with deadline after deadline and you just need to take a step back and realize that.
class, instance, methods and inheritance, abstraction and encapsulation they ring a bell don’t they? They are the principles of object oriented programming, the paradigm most of the programmers use nowadays.
Digging through my own code, I see names like “Entity” and “RenderableEntity” and you know, I kept thinking, they aren’t truly descriptive and looking at their name, you cannot determine what their functions or properties are.
When developing an API, I like to keep in mind that the consumer of the API, should not need to have a look at any documentation to understand how the API works, it should just come naturally.
Let’s have a look at an example from the .NET Framework
DirectoryInfo directoryInfo = Directory.CreateDirectory("NewDirectory");
directoryInfo.MoveTo("c:\\NewDirectory");
I’m not too fond of Directory.CreateDirectory, I would’ve had Directory.Create(…), but you can determine that Directory would have a Create method inside of it, as well as returning some kind of information related to the directory that just go created.
I hardly ever have to open the MSDN documentation, the reason for this is clean design of the BCL (Base Class Libraries).
So, next time you decide to write a library of some sort, make sure that it’s intuitive and that you aren’t the next victim of the Daily WTF.
e4181b41-6726-44d5-95c8-72a24ad637fd|0|.0
It’s extremely important as technologists that we keep up to speed with some basic algorithms and principles of computer science. It’s difficult when you are bombarded with all kinds of new technologies to go back to the basics now and then.
Igor has a new take on solving problems and it makes use of some computer science concepts such as recursion and loops. It’s called RoboZZle and it’s a puzzle game coded in Silverlight.
RoboZZle is an online puzzle game that challenges you to program a robot to pick up all stars on a game board. The game mechanics are simple, yet allow for a wide variety of challenges that call for very different solution approaches.
The game is well put together and the controls are simple enough. Although, I thought that in the beginning the buttons were supposed to be dragged and not clicked and dropped onto the empty method call.
The reason for highlighting this game is the amount of developers that just seem to skip the basic concepts and when it comes to solving a basic problem.
This highlights an entry made by Jeff Atwood of Coding Horror and now more recently the amount of boom of Stack Overflow.
dedc0fa6-40cc-47d7-af67-88c2b27798a3|0|.0
Never trust user input!
This week at MIX10, Phill Haack had a presentation on what is new with ASP.NET MVC 2.0. The following is a list of the new features in ASP.NET MVC 2.0 from Scott Guthrie’s blog
I have recently been tinkering with an amazing validation engine in jquery from Cedric Dugas. I thought to myself, what an awesome idea it would be to try and minimize the effort of adding the validation engine to the Model Validation inside ASP.NET MVC 2.0.
The first step was to determine where the validation was taking place.
Inside of jquery.validate.js, I found the formatAndAdd function on line number 574. After the error message is constructed, I added the validationEngine.buildPrompt function which is a way for developers to utilize the nice looking prompt that is packaged as part of the plugin.
formatAndAdd: function(element, rule) {
var message = this.defaultMessage(element, rule.method),
theregex = /\$?\{(\d+)\}/g;
if (typeof message == "function") {
message = message.call(this, rule.parameters, element);
} else if (theregex.test(message)) {
message = jQuery.format(message.replace(theregex, '{$1}'), rule.parameters);
}
$.validationEngine.buildPrompt(element, message, "error");
this.errorList.push({
message: "",
element: element
});
this.errorMap[element.name] = message;
this.submitted[element.name] = message;
},
Step two to determine where the errors are removed, when they have passed the validation rule.
I found an unhighlight function on line 259 of the jquery.validate script. This code doesn’t remove the error, but it does remove the highlighting from the control. I decided to tie into this piece of code by adding the closePrompt method from the validation engine.
unhighlight: function(element, errorClass, validClass) {
$.validationEngine.closePrompt(element);
$(element).removeClass(errorClass).addClass(validClass);
}
The final step is to initialize the validation engine.
on line 50, after the submit button was clicked, we will initialize our validation engine.
if (validator.settings.onsubmit) {
$(validator.currentForm).validationEngine();
Here is the sample code. To utilize this code, all you will need to do is get the latest version of the Validation Engine and replace the jquery.validation.js with this one.
jquery.validate.js (48.89 kb)
ValidationDemo.zip (391.38 kb)
Update : An Update from the Validation Engine guru over at position-absolute now removes the box when you click on it, if it’s in the way.
bef5dfa6-87ae-403f-8410-056152b856d8|2|4.5
It’s difficult to break away from old traditions, but sometimes it pays to think a differently. NHibernate is an awesome example of what I am talking about. It gives a developer the flexibility of concentrating on writing code instead of having to synchronize the database with the data layer/code.
Currently, what I see happening quite often is the following.
- Specification document gets drawn up.
- Database is slapped together by the developer or DB “dude”.
- Developer starts working on the code, only to realize that a column is missing due to either poor planning or requirements changing, the developer spends time having to change/add a column/s and tracking down all the changes in the stored procedure and hopefully he/she has tracked all of them down, otherwise….
I have 2 words, Fluent NHibernate…
How to setup Fluent NHibernate
- Download Fluent NHibernate from http://fluentnhibernate.org/downloads
- Add references to the DLLs
- Create your first Entity
public class Person
{
public virtual int ID { get; private set; }
public virtual string Name { get; set; }
public virtual string Lastname { get; set; }
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Schema("dbo");
Table("Person");
Id(x => x.ID);
Map(x => x.Name);
Map(x => x.Lastname);
}
}
- Setup Fluent NHibernate and this is where fluent really shines. No more having to deal with the pain of the XML configuration.
public static class SessionFactory
{
public static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005
.ConnectionString(
c => c.FromConnectionStringWithKey("AthenaConnectionString")))
.Mappings(
m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
private static void BuildSchema(Configuration config)
{
new SchemaUpdate(config).Execute(true, true);
}
}
- How to use Fluent NHibernate in a simple example
var sessionFactory = SessionFactory.CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
Person person = new Person { Name = "Pieter", Lastname = "Germishuys" };
session.SaveOrUpdate(person);
transaction.Commit();
}
}
3JDQDM2DSFR8
15a366ee-39e4-46da-877b-2f43c74fde1b|0|.0
Isn’t symmetry nice? I always have some OCD when it comes to diagrams. So, it’s not surprise when you see this screenshot of the Linq2Sql Context
Along with this, now we can go ahead and create Athena.DAL which contains the Linq2Sql context.
The solution isn’t that far long, so I won’t put up any code yet.
From this fantastic post on implementing the Repository pattern using Linq2SQL, we quickly put together some code that resembles the following
The Repository Interface
namespace Athena.DAL
{
/// <summary>
/// Repository Contract
/// </summary>
public interface IRepository<TRow, TEntity>
where TRow : class
{
#region Find
IList<TEntity> Find(Expression<Func<TRow, bool>> exp);
TEntity FindSingle(Expression<Func<TRow, bool>> exp);
#endregion
#region Save
void Save(TEntity entity);
#endregion
#region Delete
void Delete(TEntity entity);
#endregion
}
}
Our Entry Model, which will describe an Entry fully
namespace Athena.DAL
{
/// <summary>
/// Entry Model
/// </summary>
public class EntryModel
{
public Entry Entry { get; set; }
public List<Tag> Tags { get; set; }
public List<Category> Categories { get; set; }
}
}
and now for our not yet implemented EntryRepository
namespace Athena.DAL
{
/// <summary>
/// Entry Repository
/// </summary>
public class EntryRepository : IRepository<Entry, EntryModel>
{
#region Find
/// <summary>
/// Find
/// </summary>
/// <param name="exp"></param>
/// <returns></returns>
public IList<EntryModel> Find(System.Linq.Expressions.Expression<Func<Entry, bool>> exp)
{
throw new NotImplementedException();
}
/// <summary>
/// Find Single
/// </summary>
/// <param name="exp"></param>
/// <returns></returns>
public EntryModel FindSingle(System.Linq.Expressions.Expression<Func<Entry, bool>> exp)
{
throw new NotImplementedException();
}
#endregion
#region Save
/// <summary>
/// Save
/// </summary>
/// <param name="entity"></param>
public void Save(EntryModel entityModel)
{
throw new NotImplementedException();
}
#endregion
#region Delete
/// <summary>
/// Delete
/// </summary>
/// <param name="entity"></param>
public void Delete(EntryModel entityModel)
{
throw new NotImplementedException();
}
#endregion
}
}
88900210-1026-48dc-8bb3-13181c890c4a|0|.0
So, here is a quick database design of Athena, the blogging engine that I am building as a pet project. Also, my girlfriend recently asked me if I wouldn’t code something for her, so there is some motivation behind it, other than the geek inside of me.
Next, we need to create a new ASP.NET MVC 2.0 Project and start working on the model.
b8e1ac28-ae5f-4c11-beaa-2719657b6ee1|0|.0
This framework is not at all supposed to challenge or replace a blog engine but to merely be an effort from myself to showcase some of the fantastic and cool technologies out there.
I chose Athena because she is the Greek god of wisdom!

I have already blogged about some of technologies that we are going to use, so tonight, we are just going to quickly put the framework together and then start playing.
06e507f6-b20f-491c-9279-4ba4836df3a4|0|.0
Caching considerations
Donut hole caching from Phill Haack!
The Repository Pattern
This thread on StackOverflow
The Output cache is a clear winner for fast and effective caching, but like Mr. Haack has mentioned, only use it when it’s the right tool for the job. The Output Cache can give a great bit of flexibility with regards to where the output is stored.
The current locations for the Output Cache are the following
from http://support.microsoft.com/kb/323290
- Any - This stores the output cache in the client's browser, on the proxy server (or any other server) that participates in the request, or on the server where the request is processed. By default, Any is selected.
- Client - This stores output cache in the client's browser.
- Downstream - This stores the output cache in any cache-capable devices (other than the origin server) that participate in the request.
- Server - This stores the output cache on the Web server.
- None - This turns off the output cache.
6dffd84f-aec0-4146-b574-b47ad3941735|1|4.0