Wednesday, April 29, 2009

Fluent Interface Updated

This is a update on Fluent Interface patter based on my previous blog. The update is based on a much simplified interface for generic domain object.

First, I defined the following interface:

public interface IDomainObjReaderFluent<T> where T: class
{
IDomainObjFluent ReadData(int byIndex);
IDomainObjFluent ReadData(string byName);
T GetObject();
}


The interface defines two ways to read data, by index or by name. For the example of Employee class, the implementation class is:

class EmployeeReaderFluent : IDomainObjReaderFluent
{
private IDbReader _reader;
private int _id;
private string _name;
private DateTime _dob;
//...
public EmployeeReaderFluent<Employee>(IDbReader reader)
{
_id = 0;
_name = null;
_bod = DateTime.Now;
_reader = reader;
}
IDomainObjReaderFluent ReadData(int byIndex)
{
if ( byIndex == EmployeeConst.IdIndex )
{ _id = _reader.GetInt32(byIndex); }
else if ( byIndex == EmployeeConst.NameIndex )
{ _name = _reader.GetString(byIndex); }
else if ( byIndex == EmployeeConst.BODIndex )
{ _bod = _reader.GetDateTime(byIndex); }
//...
}
// ReadData(string byName) skipped ...
Employee GetObject() {
return new Employee(_id, _name, _dob, ...);
}
}


In the implementation of the interface for a domain object, the class has complete knowledge of how each field is retrieved from IDbReader or a data reader. In other words, it will be up to the implementation class to decide how to get data from the data reader. The data reader instance can be passed in through its CTOR.

Here is an example of its usage:

Employee empl = new
EmployeeReaderFluent<Employee>(myDbReader).
ReadData(EmployeeConst.IdIndex).
ReadData(EmployeeConst.NameIndex).
ReadData(EmployeeConst.BodIndex).
...
GetObject();


where myDbReader is an instance of IDbReader, and EmployeeConst is a class with only constants of index values for each field. I prefer to use const class to embedded literal constants.

Read More...

Tuesday, April 21, 2009

StructureMap and Fluent Interface

Recently I have been working a project to process data. The case is very simple. It reads data from database, process data and finally out put data to either database or text file. I have worked on the similar scenario for many cases. This time, I decided to design a generic pattern for developing similar type applications.

Based on past experience, I think I need a dependency framework for decoupling components and integrating various implementations of interfaces into a specific application. StructureMap got my attention. After about one investigation, I really like this framework. It is just for DI but very powerful. One interesting feature of this tool is its code-based registration to map DIs, which is based on Fluent Interface.

After reading the definition and examples of Fluent Interface, I really like its readability feature. Just coming up to my mind, I envision its implementation of mapping database element to domain object.

For example, the following is a domain class as Employee:

public class Employee {
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public datetime BOD { get; set; }
public float Salary { get; set; }
}


My employee reader interface is based on Fluent Interface like this:

interface IEmployeeReaderFluent {
IEmployeeReaderFluent ReadID(IDataReader);
IEmployeeReaderFluent ReadFirstName(IDataReader);
IEmployeeReaderFluent ReadLastName(IDataReader);
IEmployeeReaderFluent ReadBOD(IDataReader);
IEmployeeReaderFluent ReadSalary(IDataReader);
Employee GetObject();
}


And the implementation class is defined as:

class EmployeeReaderFluent : IEmployeeReaderFluent {
private Employee = new Employee();
IEmployeeReaderFluent ReadID(IDataReader reader) {
Employee.ID = IDataReader.GetInt32(
IDataReader.GetOrdinal("ID"));
}
IEmployeeReaderFluent ReadFirstName(IDataReader reader) {
Employee.FirstName = reader.GetString(
reader.GetOrdinal("FirstName"));
}
IEmployeeReaderFluent ReadLastName(IDataReader reader) {
Employee.LastName = reader.GetString(
reader.GetOrdinal("LastName"));
}
IEmployeeReaderFluent ReadBOD(IDataReader) {
Employee.BOD = reader.GetDateTime(
reader.GetOrdinal("BOD"));
}
IEmployeeReaderFluent ReadSalary(IDataReader) {
Employee.Salary = reader.GetFloat(
reader.GetOrdinal("Salary"));
}
Employee GetObject() { return Employee; }
}


Example of the usages:

IEmployeeReaderFluent employeeReader = 
new EmployeeReaderFluent()
.ReadID(reader)
.ReadFirstName(reader)
.ReadLastName(reader)
.ReadBOD(reader)
.ReadSalary(reader);
Employee employeeObj = employeeReader.GetObject();

Read More...

Monday, April 13, 2009

Yahoo YUI Reading Blinds

Yahoo YUI Reading Blinds is a very nice browser tool based on JavaScript and YUI. Recently I tried to dig out more about this tool by looking its codes. Here are some of my findings.

First, the tool is based on a group of JavaScript codes. It is very long one like this:

javascript:var%20x=%20function(){var%20h=document.createElement('script');h.src='http://yuiblog.com/assets/readingblinds.js';h.type='text/javascript';document.getElementsByTagName('head')[0].appendChild(h)}();


This long line of codes does not have spaces. The spaces are actually specified by "%20" in codes. Then I break the codes in a nice format like this:

javascript:var x= function(){
var h=document.createElement('script');
h.src='http://yuiblog.com/assets/readingblinds.js';
h.type='text/javascript';
document.getElementsByTagName('head')[0].appendChild(h)
}();


In stead of putting this codes to toolbar, I added this tool as a new bookmark to my bookmark (I am using Vimperator and there is no toolbar visible). From the bookmark, I click on this. It works fine with reading blinds visible on top and bottom, very nice! I like this reading blinds more than others (you may find other reading blinds on web). It works well with with mouse movement and vertical scroll bar.



The only problem I had was that my Vimperator caches all the keystrokes. I could not use s, l, and b keys for smaller, larger and toggle. Vimperator blockes them to the tool. Fortunately, Vimperator has a "PASS THOUGH" mode to let all the keys passed: Control-Z.

This tool actually injects YUI JavaScript codes into the page: loading YUI scripts into <head> area. The scripts then adds top and bottom <div>s as blinds. In addition to that, it adds key and mouse event to move blinds.

I further hacker this tool to change the URL to the script src to my local Mac's Sites folder like this:

...
h.src='http://localhost/~[username]/YahooYUIReadingBlinds/readingblinds.js';
...


I added this to another bookmark as "YUI Reading Blinds Local Scripts". It works great! Since I download the readingblinds.js to my local site, I then made some changes to figure out how it works and to test its features with different settings. It was really fun to work with this tool.

The reading blinds will become darker if you click on the tool more than once. With FireBug enabled, I noticed that the scripts are injected more than once.

To get rid of this reading blinds, just refresh the web page. The original web page is reloaded and the JavaScript inject is gone.

The reading blinds does not work with https web URLs since https URL is an encrypted HTML protocol and no JavaScript injection are allowed.

This tool is a very good example to enhance web page display by using JavaScript injection!

By the way, I feel much easy to read web pages and my eyes are more relaxed with this great tool for a long period of time, especially with the browser maximized. I think this may protect screen as well.

Read More...

Friday, April 10, 2009

Code Syntax Highlight

toolsThe main reason I prefer to use Blogger than WordPress is that Blogger allows me to customize my blog layout with customized scripts and css styles in my blog template. For example, I have some blogs with source codes in C#, JavaScript, HTML, and SQL. It is very easy to include codes in a blog, by using <pre> tag. However, to highlight syntax, you need to sepcify style or to use css class style. Blogger provides this feature for free but you have to pay for this feature at WordPress.

Here are the steps I use to highlight my code syntax:

  1. Add some style to my blog HTML. This can be done through Blogger's Layout|Edit HTML. Check the Expand Widget Tempates to get the whole HTML layout settings and save it as backup before you make any changes.



    Add css classes (defined in { and } area) will be used to highlight code syntax.

  2. Generate HTML codes for my codes. I like to use some web's tools to get the job done. The tool I use is ASP.Net's forum page. I use this forum's post new topic editor to write/past my codes from its Source Code widget. The widget supports several languages which covers my areas. Those snap-shots are examples how I get my codes to HTML.

    1. Click on Source Code widget button




    2. Type in my codes




    3. Click OK to close it.

    4. Click on Edit HTML widget button




    5. Edit or copy the HTML codes



    6. Copy section HTLM codes between <pre ...> and </pre>


    Note: you may notice that ASP.NET's HTML codes contain some css classes. I added those classes to my Blogger's HTML template.

  3. In my blog entry, I paste the HTML codes to my blog. Normally I put the HTML codes between div tags. If the codes are not too long, I use the div with border. If the codes are too long, then I use a div with fixed height so that codes are within a block with scroll bar available to view codes.

    You can see those HTML tags by viewing section of codes (right click a selection of text).


There are many good tools available for syntax highlighting such as Google's syntaxhighlihter, SHJS, HIGHLIGHT.JS, and Highlight. The problem is that most of those tools are for people who have web server. I only have hosted web server like Blogger or WordPress. I cannot put any of my codes and css style files to the server. Blogger provides a way to embed css styles, but I would not want too many css definition codes there. It is just hard for me to maintain.

That's why I like to use ASP.NET's tool to get HTML codes with classes. Another great advantage of ASP.NET's widget is that it will be able to convert some special HTML codes such as > and <.

Read More...

Saturday, April 04, 2009

SQLite Manager Add-on for Fixfox

toolsI found a very handy tool: SQLite Manager an add-on for FireFox. I have been using another application called as SQLite Database Browser in my Mac for my SQLite databases. FireFox add-on actually is much better than the application, at least from the UI.

For example, the UI provides Database Settings for database generic settings and UIs for Views, Indexes and Triggers. The table UI layout is also much better than SQLite Browser.



I have not tried this one in Windows yet. I think it should work without any problem.

Read More...