On the way to be great…

Coding with passion and discipline

Implement ELMAH For Error logging

leave a comment »

Following the suggestion from Scott and Jeff, I decide to have ELMAH implemented in my current ASP.NET application as well. The whole progress is really straightforward and painless. Most of the work is to modify the application’s web.config file.

    • Download the latest version of bin, currently 1.0 version, from project’s web site, hosted on Google code.
    • Based on the version of your ASP.NET application, reference the corresponding Elmah.dll from the subfolder. You might need to reference SQLite.dll if you need to use SQLLite for the log storage.
    • Modify the Web.config based on the sample Web.config file coming with the sample. You can check it from here as well.

 

That is it! Then you can find the unhandled exception by access http://yourwebsite/elmah.axd. You will see the error page is something like

elmah.axd-with-error

From what I see so far, ELMAH is

  • Easy to implement and set it up
  • Flexible to offer multiple log storage providers, including SQL Server, SQLLite, Oracle, XML, and In-memory.
  • Readable, the log is formatted in a HTML table, which can be also export to CVS file and be queried by Microsoft’s Log Parser tool.
  • Reliable, it provides multiple notification channels, including Email, RSS and even Tweet.

Just as Jeff Atwood said in his blog above, the main purpose of setting up ELMAH in our application is to find out the accurate, valuable and real time “feedback” of the applications we built automatically.

Happy coding, till next time!

Written by liangwu

May 20, 2009 at 1:23 pm

Posted in ASP.NET

Really Useful Windows Shortcuts

leave a comment »

KeyboardOne the of the tips I heard to become a productive developer is to use mouse as few as possible, put the fingers on the keyboard.

Mouse clicking is sort of distraction from we are doing. Combining with any of the launcher tool, such as Slickrun, you will find you can definitely accomplish your work faster, more effectively and a lot fun!

Don’t try to memorize all of them at once. When you feel you need to use mouse, just do some search to see if any shortcut available. Here is the list I am using a lot as a .NET developer (visual studio and Resharper, of course!)

Here are some of the window-based shortcuts I collected and use very often. Hope it will help.

Visual Studio Shortcut Keys

Shortcut Keys Description
Ctrl + X Cut the current selected item to clipboard
Ctrl + C Copies the current selected item to the clipboard
Ctrl + V Pastes the item in the clipboard at the cursor
Ctrl + Z Undo previous editing action
Ctrl + Y Redo the previous undo action
Esc Closes a menu or dialog, cancels an operation in progress, or places focus in the current document window
Ctrl + S Saves the selected files in the current project (usually the file that is being edited)
Ctrl + Shift + S Saves all documents and projects
Shift + F12 Finds a reference to the selected item or the item under the cursor
Ctrl + End Moves the cursor to the end of the document
Ctrl + Home Moves the cursor to the start of the document
Ctrl + G Displays the Go to Line dialog
Ctrl + Right Arrow Moves the cursor one word to the right
Ctrl + Left Arrow Moves the cursor one word to the left
Ctrl + K, Ctrl + C Marks the current line or selected lines of code as a comment
Ctrl + K, Ctrl + U Removes the comment syntax from the current line or currently selected lines of code
Ctl + J Lists members for statement completion when editing code
Ctrl + R, Ctrl + W Shows or hides spaces and tab marks
Shift-Left Arrow Moves the cursor to the left one character, extending the selection
Ctrl + Shift + End Moves the cursor to the end of the document, extending the selection
Ctrl + Shift + Home Moves the cursor to the start of the document, extending the selection
Shift + Down Arrow Moves the cursor down one line, extending the selection
Shift + End Moves the cursor to the end of the current line, extending the selection
Shift + Home Moves the cursor to the start of the line, extending the selection
Shift + Up Arrow Moves the cursor up one line, extending the selection
Shift + Page Down Extends selection down one page
Shift + Page Up Extends selection up one page
Ctrl + A Selects everything in the current document
Ctrl + Shift + Page Down Moves the cursor to the last line in view, extending the selection
Ctrl + Shift + Page Up Moves the cursor to the top of the current window,
Ctrl + Shift + Alt + Right Arrow Moves the cursor to the right one word, extending the column selection
Ctrl + Shift + Left + Arrow Moves the cursor one word to the left, extending the selection
Ctrl + Shift + B Build the solution
Ctrl + Shift + N Display the New Project dialog
Ctrl + O Display open the file dialog
Ctrl + Shift + O Display open project dialog
Shift + Alt + A Display the Add Existing Item dialog
Ctrl + Shift + A Display the Add New Item dialog
Shift + Alt + Enter Toggles full screen mode
Ctrl + Tab Cycles through the MDI child windows one by one
Ctrl + F Display the Find dialog
Ctrl + Shift + F Display the Find in Files dialog
F3 Find Next
Ctrl + H Display the Replace dialog
Ctrl + Shift + H Display the Replace in Files dialog


Basic Windows shortcut keys

Shortcut Keys Description
Alt+F File menu options in current program.
Alt+E Edit options in current program
F1 Universal Help in almost every Windows program.
Ctrl+A Select all text.
Ctrl+X Cut selected item.
Shift+Del Cut selected item.
Ctrl+C Copy selected item.
Ctrl+Ins Copy selected item
Ctrl+V Paste
Shift+Ins Paste
Home Goes to beginning of current line.
Ctrl+Home Goes to beginning of document.
End Goes to end of current line.
Ctrl+End Goes to end of document.
Shift+Home Highlights from current position to beginning of line.
Shift+End Highlights from current position to end of line.
Ctrl+Left arrow Moves one word to the left at a time.
Ctrl+Right arrow Moves one word to the right at a time.
Alt+Tab Switch between open applications.
Alt+Shift+Tab Switch backwards between open applications.
Alt+double-click Display the properties of the object you double-click on.
Ctrl+Tab Switches between program groups or document windows in applications
Ctrl+Shift+Tab Same as above but backwards.
Alt+Print Screen Create a screen shot only for the program you are currently in.
Ctrl+Alt+Del Reboot the computer and/or bring up the Windows task manager.
Ctrl+Esc Bring up the Windows Start menu.
Alt+Esc Switch Between open applications on taskbar.
Alt+F4 Closes Current open program.
Ctrl+F4 Closes Window in Program.
Shift + Del Delete programs/files without throwing them into the recycle bin.
Holding Shift Boot Safe Mode or by pass system files as the computer is booting
Holding Shift When putting in an audio CD, will prevent CD Player from playing.
Ctrl+F9 Minimize current window.
Ctrl+F10 Maximize currently selected window.
Windows Logo Start menu
Windows Logo+R Run dialog box
Windows Logo+M Minimize all
SHIFT+Windows Logo+M Undo minimize all
Windows Logo+F1 Help
Windows Logo+E Windows Explorer
Windows Logo+F Find files or folders
Windows Logo+D Minimizes all open windows and displays the desktop
CTRL+Windows Logo+F Find computer
CTRL+Windows Logo+TAB Moves focus from Start, to the Quick Launch toolbar, to the system tray
Windows Logo+TAB Cycle through taskbar buttons
Windows Logo+Break System Properties dialog box
Windows Logo+L Log off Windows
Windows Logo+P Starts Print Manager
Windows Logo+C Opens Control Panel
Windows Logo+V Starts Clipboard
Windows Logo+R Open and Run Window


Web Browser common shortcut Keys

Shortcut Key Description
Ctrl + N Open a new window
Ctrl + T Open a new tab
Ctrl + W Close the current window
Ctrl + R Refresh
Esc Stop
Alt + <- Back
Alt + -> Forward
Alt + Home Go to your homepage
Alt + D Move focus to the address bar to type URL
Ctrl + Enter Add “http://www.” and “.com” around an address


Mozilla Firefox shortcut Keys

Shortcut Keys Description
Alt + Home Home
Alt + Left Arrow Back a page.
Backspace Back a page.
Alt + Right Arrow Forward a page.
F5 Refresh current page, frame, or tab.
F11 Display the current website in full screen mode. Pressing F11 again will exit this mode.
F3 Find Again
Esc Stop page or download from loading.
Ctrl + (- or +) Increase or decrease the font size, pressing ‘-’ will decrease and ‘+’ will increase.
Ctrl + Enter Quickly complete an address. For example, type computerhope in the address bar and press CTRL + ENTER to get http://www.computerhope.com
Shift + Enter Complete a .net instead of a .com address.
Ctrl + Shift + Enter Complete a .org address.
Ctrl + Shift + Del Open the Clear Data window to quickly clear private data.
Ctrl + W Close the current window or tab
Ctrl + D Add a bookmark for the page currentlys opened.
Ctrl + I Display available bookmarks.
Ctrl + J Display the download window.
Ctrl + N Open New browser window.
Ctrl + P Print current page / frame.
Ctrl + T Opens a new tab.
Ctrl + F4 Closes the currently selected tab.
Ctrl + Shift + T Undo the close of a window.
Ctrl + Tab Moves through each of the open tabs.
Ctrl + Shift + Tab Swich to the previous tab
Ctrl + G Find the previous occurrence of a search phrase
Ctrl + K Move cursor to the Web Search Widget (top right of the screen)
Spacebar Moves down a page at a time.
Shift + Spacebar Moves up a page at a time.
Alt + Down arrow Display all previous text entered in a text box and/or available options on drop down menu.


Internet Explorer Shortcut

Shortcut Keys Description
Alt + Left Arrow Back a page
Backspace Back a page
Alt + Right Arrow Forward a page.
F5 Refresh current page, frame, or tab.
F11 Display the current website in full screen mode
Esc Stop page or download from loading.
Ctrl + (- or +) Increase or decrease the font size, pressing ‘-’ will decrease and ‘+’ will increase.
Ctrl + Enter Quickly complete an address. For example, type computerhope in the address bar and press CTRL + ENTER to get http://www.computerhope.com.
Ctrl + D Add a Favorite for the page currently opened.
Ctrl + I Display available bookmarks.
Ctrl + N Open New browser window.
Ctrl + P Print current page / frame.
Ctrl + T Opens a new tab.
Ctrl + F4 Closes the currently selected tab.
Ctrl + Tab Moves through each of the open tabs.
Spacebar Moves down a page at a time.
Shift + Spacebar Moves up a page at a time.
Alt + Down arrow Display all previous text entered in a text box and/or available options on drop down menu.

Microsoft Excel

Shortcut Keys Description
F2 Edit the selected cell.
F5 Goto a specific cell. For example, C6.
F7 Spell check selected text and/or document.
F11 Create chart.
Ctrl + Shift + ; Enter the current time.
Ctrl + ; Enter the current date.
Alt + Shift + F1 Insert New Worksheet.
Shift + F3 Open the Excel formula window.
Shift + F5 Bring up search box.
Ctrl + A Select all contents of the worksheet.
Ctrl + B Bold highlighted selection.
Ctrl + I Italic highlighted selection.
Ctrl + K Insert link.
Ctrl + U Underline highlighted selection.
Ctrl + 5 Strikethrough highlighted selection.
Ctrl + P Bring up the print dialog box to begin printing.
Ctrl + Z Undo last action.
Ctrl + F6 Switch between open workbooks / windows.
Ctrl + Page up Move between Excel work sheets in the same Excel document.
Ctrl + Page down Move between Excel work sheets in the same Excel document.
Ctrl + Tab Move between Two or more open Excel files.
Alt + = Create a formula to sum all of the above cells
Ctrl + ‘ Insert the value of the above cell into cell currently selected.
Ctrl + Shift + ! Format number in comma format.
Ctrl + Shift + $ Format number in currency format.
Ctrl + Shift + # Format number in date format.
Ctrl + Shift + % Format number in percentage format.
Ctrl + Shift + ^ Format number in scientific format.
Ctrl + Shift + @ Format number in time format.
Ctrl + Arrow key Move to next section of text.
Ctrl + Space Select entire column.
Shift + Space Select entire row.

Microsoft Word

Shortcut Keys Description
Ctrl + A Select all contents of the page.
Ctrl + B Bold highlighted selection.
Ctrl + C Copy selected text.
Ctrl + X Cut selected text.
Ctrl + P Open the print window.
Ctrl + F Open find box.
Ctrl + I Italic highlighted selection.
Ctrl + K Insert link.
Ctrl + U Underline highlighted selection.
Ctrl + V Paste.
Ctrl + Y Redo the last action performed.
Ctrl + Z Undo last action.

Written by liangwu

April 24, 2009 at 9:14 pm

Posted in Productivity

Functional programming in C# 3.0 – (1)

leave a comment »

I had a lot of fun to read Tomas Petricek’s article, "Concepts behind the C# 3.0 language". Instead of listing all of the new syntax changes as most of other tutorials, Tomas shed some lights on the details about influence of the development of C# 3.0 from other languages, especially how Functional programming plays in C# 3.0. Actually, Lambda Expression and LINQ are really part of that.

So what is functional programming? Quote from Wikipedia:

"Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast with the imperative programming style that emphasizes changes in state.[1] "

So what it really tells us? I think the main point is "function is data" which means function can be use to be passed as an parameter or returned argument or modified as any other data in our application. Do we gain benefit for that? Sure, we do.

1. Functional programming can help model data operation. Here is the sample:

1 private static long Watch<T>(Action<T> act, T arg) 2 { 3 Stopwatch watch = new Stopwatch(); 4 watch.Start(); 5 act(arg); 6 watch.Stop(); 7 return watch.ElapsedMilliseconds; 8 }

This Watch function is totally decoupled with the Action, which could be "reading the data from database", or "processing the data from a file". It leads a clean design that Watch function and the Action function can be tested separately.

2. Parallel programming. If you have been worked on Multi-thread programming. You know how hard it is. The limitation of imperative language like C# focuses on the state change. But functional programming is passing function as argument as we mentioned above. The state actually is only closed in the function boundary. So there is no other thread can access that data. Microsoft has released PLINQ, a parallel programming add-on to current .NET framework 3.5. It will be a huge benefit for any .NET who will take advantage of multi-processor computers.

So far, so good, right? from next one, I will illustrate the functional programming features in C# 3.0. Definitely will show more code. Please stay tune.

Written by liangwu

January 22, 2008 at 3:30 pm

Posted in C#

Create DTO with NHibernate

with 3 comments

My previous blog is about using DTO in UI/Presenter layers. Just studying Nhibernate source code now, I find a very interesting approach to populate DTO object directly from Nhibernate with Criteria and Projections.
Here is the code:

   1:  IList resultWithAliasedBean = s.CreateCriteria(typeof(Enrolment))
   2:   .CreateAlias("Student", "st")
   3:   .CreateAlias("Course", "co")
   4:   .SetProjection(Projections.ProjectionList()
   5:   .Add(Projections.Property("st.Name"), "studentName")
   6:   .Add(Projections.Property("co.Description"), "courseDescription")
   7:  )
   8:  .AddOrder(Order.Desc("studentName"))
   9:  .SetResultTransformer(NHibernate.Transform.Transformers
  10.  .AliasToBean(typeof(StudentDTO)))
  11:  .List();

The Element and StudentDTO is simple enough.

   1:          [Serializable]
   2:      public class Enrolment
   3:  
   4:          private Student student;
   5:          public virtual Student Student
   6:          {
   7:              get { return student; }
   8:              set { student = value; }
   9:          }
  10:  
  11:          private Course course;
  12:          public virtual Course Course
  13:          {
  14:              get { return course; }
  15:              set { course = value; }
  16:          }
  17:  
  18:          private long studentNumber;
  19:          public virtual long StudentNumber
  20:          {
  21:              get { return studentNumber; }
  22:              set { studentNumber = value; }
  23:          }
  24:  
  25:          private string courseCode = string.Empty;
  26:          public virtual string CourseCode
  27:          {
  28:              get { return courseCode; }
  29:              set { courseCode = value; }
  30:          }
  31:  
  32:          private short year;
  33:          public virtual short Year
  34:          {
  35:              get { return year; }
  36:              set { year = value; }
  37:          }
  38:  
  39:          private short semester;
  40:          public virtual short Semester
  41:          {
  42:              get { return semester; }
  43:              set { semester = value; }
  44:          }
  45:  
  46:          public override bool Equals(object obj)
  47:          {
  48:              Enrolment that = obj as Enrolment;
  49:              if (that == null)
  50:                  return false;
  51:              return studentNumber == that.StudentNumber && courseCode.Equals(that.CourseCode);
  52:          }
  53:  
  54:          public override int GetHashCode()
  55:          {
  56:              return courseCode.GetHashCode();
  57:          }
  58:      }
  59:  public class StudentDTO
  60:      {
  61:          private string studentName;
  62:          private string courseDescription;
  63:  
  64:          public StudentDTO() { }
  65:  
  66:          public string Name
  67:          {
  68:              get { return studentName; }
  69:          }
  70:  
  71:          public string Description
  72:          {
  73:              get { return courseDescription; }
  74:          }
  75:      }

What we can do is to retrieve StudentDTO from Presenter by querying from Repository. Code is elegant and beautiful. How sexy NHibernate is!!

Written by liangwu

March 13, 2007 at 3:16 pm

Posted in DDD, NHibernate

DTO (Data Transfer Object) in Domain Driven Design

with one comment

DTO (Data Transfer Object)is a very common used pattern in enterprise applications. Martin Fowler also documented it in his class book “Pattern Enterprise Application Architecture“. In the meanwhile, DTO is also a very famous “Anti-Pattern”. The pure OO purist think DTO is a devil. It is so easy to build. The object become nothing but a simple data container, which represent the data in the tables instead of the real domain objects. The business logic embedded in the application is going to shift from middle domain logic tier to tables, most of time spreaded among the stored procedures, which is leading to a unmaintainable solution.

So should we give up DTO totally in our application design? Absolutely not, in the other end, I find DTO still useful especially in UI and UI process layers. For example, we have a complex searching page to find customer by first name, last name, SSN, address, phone, email, and so on. those information may stay in different domain objects. It is also very hard to design a method with a very long list of parameters, not just ugly but also unmaintainable. My approach is to design DTO object pass data between UI and UI process (Presenter or Controller). Then access the service objects, and domain objects from presenter/controller. So I can design domain entity with the interfere from the special requirement of UI. Again it is important to implement MVP pattern to protect the domain object, which is also one of the important principles I heard from DDD: don’t leak business logic out of the domain object layer.

Written by liangwu

February 13, 2007 at 2:03 pm

Posted in Architecture, DDD

Astronaut Pen

leave a comment »

I read a story today which make me falling into deep thinking…

“During the space race back in the 1960’s, NASA was faced with a major problem. The astronaut needed a pen that would write in the vacuum of space. NASA went to work. At a cost of $1.5 million they developed the “Astronaut Pen”. Some of you may remember. It enjoyed minor success on the commercial market.

The Russians were faced with the same dilemma. They used a pencil. “

from here.

Written by liangwu

February 9, 2007 at 2:02 pm

Posted in Uncategorized

How to implement Domain model (object) in DDD?

leave a comment »

Inspired by Eric Even’s excellent book, Domain Driven Design,
I have been studying and using domain driven design for a while. There have a lot of advantages offered by implementing DDD in enterprise applications. I will continue to blog what I learned from DDD later. Among those advantage, one of the most important is to clearly lay out the resposibilites for the classs in the doma layer, such as Entity, Value Object, Service, Repository and Fractory. There has a question always in my mind that if we have move application operations to service class. What methods we need to implement for Entity class? We are trying to avoid AnemicDomainModel. So how do we design our Entity class? Here comes the answer:

“The best definition of “domain object” I’ve found in literature is in Chapter
11 of TimHoward’s book TheSmalltalkDevelopersGuideToVisualWorks
pp.379-381:

“a domain object is a logical container of purely domain
information, usually represents a logical entity in the problem domain space …
In general, domain objects should know how to
recognize which [of their]
references indicate aggregation and which ones indicate association
copy
themselves
maintain business logic
compare themselves to other domain
objects of the same type
facilitate other objects that choose to print or
display them
conduct tests on their domain information”
To this list I
would add
identify themselves
validate themselves
RandyStafford
I would also
add
domain objects direct their persistence
there may be collections of
Domain objects
domain object may contain references to other domain objects
(this is assumed by the first one above).
–Fritz Schenk “

Quote from http://c2.com/cgi/wiki?DomainObject

Written by liangwu

January 10, 2007 at 2:03 pm

Posted in Architecture, DDD

NAnt tip

leave a comment »

Currntly I am working on a NAnt script to update all of our open source project source. Basically I need to use checkout and update taskes for SVN and CVS. Using and is smooth, if you have the lastest Subversion (currently 1.4.2) installed . But and will cause issues since it seems that cannot work nicely with . I got a problem that I cannot successfully login to the server. After done some research, here is the workaround.
1. Dowonload the lastest CVSNT.
2. Put cvs.exe into the Nant bin folder.
3. Turn attribute “usesharpcvslib” to false.
Example:

nantsample.png

Written by liangwu

November 22, 2006 at 3:18 pm

Posted in Auto Build

Best posts from Jeremy Miller

leave a comment »

Jeremy has organized his best posts in the last three years. Please check it out frm there. I think I have learned a lot from Jeremy’s postes. Great stuff include MVP pattern, TDD (Test-Driven Development), Mock Object, Depencency Injection, and so on. Every single post from his is worth reading. By the way, just happen to know he is goint to write a book. It must be a MUST READ.

Written by liangwu

August 8, 2006 at 2:03 pm

Posted in Agile, TDD

Data Transfer Object (DTO) in MVP pattern

leave a comment »

Jean-Paul Boodhoo from ThoughtWorks has published an excellent article regarding how to implement MVP (Model-View-Presenter) pattern in ASP.NET. It acutally has very similar implementation I am using currently. The only difference is I declear event in the view, so that ASP.NET page use event to notify the presenters, which makes more loosely coupled between ASP.NE page and presenter. I also use DTO (Data Transfer Object) to pass the data through. Although DTO seems as a Core J2EE Pattern, I still have a concern to use it extensively, becasue DTO is totally against the Object-Oriented principals. It promotes a FLAT object, a really value object. Since I have developed my domain objects with NHibernate/Castle ActiveRecord, it duplicates the efforts as well. The main purpose of using DTO is to utilize the ASP.NET databinding features. Paul Wilson seems to work on a solution to map domain objects to UI. It seesm very intertesting. But I have not seen any sample code yet. Anybody has a better solution?

Written by liangwu

August 2, 2006 at 2:05 pm

Posted in Architecture, DDD