Tagged: C#

Design Patterns : Singleton

A Singleton, in software engineering, is a design pattern that is used when you only want/need to allow one instance of a class. This is specifically useful when the class instantiation of the said class is resource expensive (e.g. database connection, graphics driver, network resource and file streams…).

Below is a UML describing the implementation details of a Singleton.

designpatternsworkbook_singleton_diagram

Here is an example of a Thread-safe Singleton implemented in C#. In this example, we are trying to simulate a DbConnection object. The DbConnection object has a private constructor that blocks users of this class from ‘new-ing’ this object. Instead, the DbConnection object has a property called Instance that allows users creates a copy of DbConnection.

designpatternsworkbook_singleton_classdiagram

Running the above code will have this output:

designpatternsworkbook_singleton_outputresult

 

How To: Determine what Adobe Flash version is currently installed in a machine using C#

Here are two ways you can determine what version of Adobe Flash is currently installed in a machine using C#.

1. You can read the Registry entry for Adobe Flash player located in HKEY_LOCAL_MACHINE\SOFTWARE\MACROMEDIA\FLASHPLAYER and read the value for CurrentVersion key

2. Use Reflection by creating a new SWFObject (Flash Object) and reading its version property

Both functions will give you the version number in the form of a string for the currently installed Adobe Flash in your machine.

I personally preffer the Reflection method since it is cleaner and not dependent on registry key paths.

My tools of the trade 2012

Rusty Winch by Geoff Lawrence

A friend of mine asked me the other day about the tools I am using while developing applications and it took me a good few minutes to tell him all of things I used. I figured I should put it online just in case someone ask again in the future since it was a lot to talk about. So here it goes:

Microsoft Visual Studio – My development best friend.
NetBeans – C++/Java/PHP development IDE. I used to use if for Ruby too but support has since been dropped.
Eclipse – For my Android obsession. I used this for Java as well but not as much compared to Netbeans.
Xcode – Emergency relief when I’m bored with Android and C#.
CodeRush – Code Refactoring add-on for Microsoft Visual studio that doesn’t suck. I love ReSharper too but I haven’t found a reason that would make me switch.
WCF Storm – Web Services tester that is way better than the standard WCF Test client from Microsoft
Fiddler – Web debugging too for HTTP testing. This thing rocks. Trust me.
StyleCop – Code analysis tool ti make sure you are following a set of coding guidelines(style).
LinqPad – More than just a LINQ expression builder. A good way to test expressions before loading them to your actual code.
JsFiddle – Javascript, HTML and CSS tester that helps you isolate bugs
BrowserShots – A nice site that shows you how your site looks like in multiple browsers.
JsLint – Javascript code evaluator that tells you if your code sucks.
JetBrains dotPeek – Free .NET code decompiler from JetBrains. A replacement for Reflector.
BitBucket – For my private source code repositories. GitHub for my not so private code repositories
SourceTree – Mercurial, Git and Subervision(SVN) repository manager for Mac. I use TortoiseSVN for Subversion and TortoiseHG for Mercurial repository management access in Windows
Evernote – Notes and more notes. I used this as repository for ideas, meeting notes and anything else that I need to remember.
Skitch – Screenshot tool from the makers of Evernote for the Mac
Balsamiq – My preferred wire framing tool. SketchFlow comes second.
OmniGraffle – Microsoft Visio replacement for Macs. Can be used for wire framing too.
LinkChecker – Link validator for websites. Scans websites for broken links and generates a report on what they are.
Console2 – Probably the best Windows Command prompt replacement. This thing is glued with awesomeness.
PixResizer – A free batch picture resizer for Windows that doesn’t kill your machine’s resources.
7zip – a free Winzip replacement.
Notepad++ – Best notepad replacement in Windows. Notepad2 is also nice. I use TextWrangler when I’m on a Mac.
WinMerge – a free Document comparison and merging tool
Dropbox – the must have Cloud-based storage system
Trello – Task Management made easy. Real-time collaboration with notifications. Works on your phone too!
RegExr – Simple yet elegant Regular Expression(RegEx) testing tool

Here’s some Firefox Add-Ons that i can’t live without.
Firebug – Must have add-on for testing and debugging pages in Firefox. Enables you to modify HTML, CSS and Javascript on the fly to see how they render in the browser
Web Developer – Get this compliment to FireBug.
ColorZilla – Color Picker and Gradient Generator. Also try Rainbow Color Tools
Pixlr Grabber – Image grabber. Allows you to save images on a web page.
ShowIP – A really neat tool that allows users to see the IP address of the current web page in their status bar.
fontinfo – Tells the user which font are used on the current page
YSlow – A tool that helps analyze why pages are slow

There’s probably more things that I forgot to list (like development frameworks e.g Ninject and Moq) so I promise I’ll create a new post in the next few days talking about those frameworks.

How about you? What tools/applications are you using in your day to day life as a developer/architect/programmer?

Cheers!

Keith Rull

Error: The reference assembly could not be resolved because it has a dependency on System.Web

I was working on a sample project today when I encountered this error after adding a new Console Application project to my Visual Studio solution that references other Class Library projects contained in the solution:

.NET 4.0 Client Profile Assembly Error

The referenced assembly “C:devcenterdotnetpersonalKeithRull.BibliaKeithRull.Biblia.Service.RepositorybinDebugKeithRull.Biblia.Service.Repository.dll” could not be resolved because it has a dependency on “System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” which is not in the currently targeted framework “.NETFramework,Version=v4.0,Profile=Client”. Please remove references to assemblies not in the targeted framework or consider retargeting your project. KeithRull.Biblia.ConsoleApp.

There are two possible cause of this error. The first possible reason is when you have an assembly that is referencing System.Web and you try to add that assembly to project that does not have any reference to System.Web (if necessary which most of the time is not). The second possible reason is that your project is using the .NET 4.0 Client Profile which is just a subset of the .NET Framework. Below are the steps that illustrate how to fix this issue.

1. Select your project, right-click and then select “Properties”.

Visual Studio Properties Menu

 

2. The properties tab, change the value for “Target Framework” and then set it’s value to “.NET Framework 4”

Visual Studio Change Target Framework

Compile your application and that should fix the problem.

Interested in learning more? Below are some useful links if you want to read more about this issue and the .NET 4 Client Profile.

Error: This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

Assembly is loaded by a newer runtime error

I received this error while deploying a WCF 4.0 service to a newly created virtual directory.

 

Could not load file or assembly ‘KeithRull.Biblia.Service’ or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.BadImageFormatException: Could not load file or assembly ‘KeithRull.Biblia.Service’ or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Assembly Load Trace: The following information can be helpful to determine why the assembly ‘KeithRull.Biblia.Service’ could not be loaded.

WRN: Assembly binding logging is turned OFF.To enable assembly bind failure logging, set the registry value [HKLMSoftwareMicrosoftFusion!EnableLog] (DWORD) to 1.

Note: There is some performance penalty associated with assembly bind failure logging.

To turn this feature off, remove the registry value [HKLMSoftwareMicrosoftFusion!EnableLog].

 

It turns out that the error occurred because the application pool for this new virtual directory is using .NET 2.0 profile instead of .NET 4.0 which is the .NET framework version that I used to compile my web service.

How To: Create a BulletList in Silverlight

I was working on a prototype application today when I needed to use a BulletList. At first I thought it existed on the toolkit controls but to my surprise it wasn’t so I ended up creating an ItemsControl template that mimics how a BulletList would behave. I wanted to post it here to show you how I did it:

 

As allways you can download a sample project here: KeithRull.SLBulletList

Fun! Now time to get back to my code zen.

Cheers!

My MIX09 10K Contest Entry: Silverlight Pregnancy Calendar

I had some free time during the holidays and saw at the MIX09 website that they have a contest entitled MIX09 10K Challenge where they ask participant to create a web application that is either using Microsoft® Silverlight™ or Windows Presentation Foundation, as a XAML Browser Application running in Partial Trust or as a ClickOnce application in 10 kilobytes or less. I decided to take a stab at it and this is what I’ve came up with

SilverCalendar: A Silverlight Pregancy Calendar

You can see the app live here http://2009.visitmix.com/MIXtify/TenKDisplay.aspx?SubmissionID=0051

Dont forget to vote when you’re there 😛 LOL

My primary motivation when I built the app is the idea of having something that is going to be useful and at the same time fun to build. My wife is currently 34 weeks pregnant and it made me think that a pregnancy calendar would be q great application to do since it relates to me and my current day to day life. I love pregnancy calendars because it gives you a good daily insight on the progress of your baby and what you might expected as you go along in your daily life as a soon to be parent.

The application that i built is using Microsoft Silverlight 2 with a backend WCF web service. You can find the pregnancy calendar web service here. I wasn’t able to add any animations to the application because I decided to concentrate in adding functionality to app rather than the eye candy’ With that said, I’ll be posting the non-10K application this week.

Be sure to comeback on this blog in the coming weeks because I’ll be publishing the source code to both project soon together with a 11 part tutorial and screencast walkthrough that i have prepared to show how to to build an application using .NET 3.5, WCF, WPF and Silverlight.