Monday, May 31, 2010

Requirements for overriding GetHashCode

The following are the requirements to remember when  you override GetHashCode System.Object function: 
  • Two equal objects must have equal hash codes
  • The Hash code of a particular object must never change (you might want to cache the result. In NHibernate's case you should use the primary keys to generate it since they do not change)
  • It should not throw exceptions 

That's it. Plain and simple.

Enjoy!

Creating a Visual Studio 2010 code template

Great Post:
http://timheuer.com/blog/archive/2010/05/03/create-vsix-files-with-visual-studio-template-deployment.aspx

Enjoy!

"Terminal Server Has Exceeded the Maximum Number of Allowed Connections" Windows Server Error

You can bypass this by connecting to the terminal server using the following on the command line:
mstsc.exe /v:localhost:7000 /admin

Note: in my case I had port forwarded the local 7000 to 3389 of the remote computer.

Got this of here:
http://www.howtogeek.com/howto/windows/command-line-hack-for-terminal-server-has-exceeded-the-maximum-number-of-allowed-connections/

you can also use the command line:
query session /server:servername
and then
reset session [ID] /server:servername
Enjoy!

Saturday, May 29, 2010

Friday, May 28, 2010

Dynamic programming with C#

A good article + code that allows you to use c# as a completely dynamic language:

http://www.codeproject.com/KB/cs/deep_object.aspx

Thought i'd bookmark it. I can come in handy someday :)

Enjoy!

Wednesday, May 26, 2010

C# Decimal type dissected

I was going through C# 4 Essentials. The Decimal type description is sufficient and it is cool to imagine it as +/- N * 10k. But I was not satisfied. The above statement and below url have put me much at ease. Must have been a difficult decision to keep a 128 bit location and leave bits blank after using 102 bits:
http://www.yoda.arachsys.com/csharp/decimal.html

Remember machines are binary and 128 is the closest upper 2^x (for a whole number x) above 102.

Update: Even more amazing explanation:
http://gregs-blog.com/2007/12/10/dot-net-decimal-type-vs-float-type/
If you are even more interested in the mathematics (non .net specific) check out the below (it will help you understand greg's post as well):
http://sandbox.mc.edu/~bennet/cs110/flt/dtof.html
Note: there is something to be found in each of these absent in the rest


Enjoy!

Tuesday, May 25, 2010

Best feature of Visual Studio 2010

We all have our favorite features in Visual Studio 2010. Maybe you love the new WPF design surface. For me it is hands down "Intellitrace". This feature is so unique that I am completely dazzled by it. Here are a few tutorials :
Beginner:
http://blogs.msdn.com/b/habibh/archive/2009/10/20/getting-started-with-visual-studio-2010-intellitrace-hello-intellitrace.aspx
Settings:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=483
Intellitrace Video (at the second half of this presentation). Usage with Team Foundation Server. With the Amazing Testing Manager:
http://channel9.msdn.com/shows/VS2010Launch/Doug-Seven-Improving-Developer-Tester-Collaboration-with-Microsoft-Visual-Studio-2010/

Thursday, May 20, 2010

Calling Dynamic languages from .NET 4.0

This is a cool new feature in .NET 4.0 courtesy of the Dynamic Language Runtime becoming a part of .NET 4.0. As you may know there is a new keyword "dynamic" in C# with .NET 4.0. Here's how you can use it to call python scripts from C#:

Title:Walkthrough: Creating and Using Dynamic Objects
http://msdn.microsoft.com/en-us/library/ee461504(VS.100).aspx


Awesome (cause I love python)!
PS: Any C#/Python programmer should definitely try out http://ironpython.net/
Enjoy!

Wednesday, May 19, 2010

Why use Team foundation server for a single developer

The main requirements of application lifecycle management:
  • Source control. History / branching primarily
  • Clear tracking of issues and work items.
Plus best Source control integration with Visual Studio

Also its super easy to setup TFS2010 basic with Visual Studio 2010.
And it does not need any further installation on your visual studio. I am very careful of what I put in visual studio especially regarding performance and I protect it with a vengeance (as many guys who have done java development in the past are)

Update: During configuration of TFS2010 i ran into a blue screen (windows 7). So to complete the configuration i had to delete the items it created:

  • IIS Website for Team Foundation Server
  • net stop
  • sc delete the
  • Close the configuration window as well as "Team Foundation Administration console" since at least one of them uses the TFS_configuration Database. 
  • sqlcmd -S "Basiee-PC\SqlExpress" -q "drop database Tfs_Configuration"
Enjoy!

Update: I am not going to lie. I did not find much usefulness in it. If you don't mind running SQL Server (taking 400 mb of your ram) in the background for a non database project just for the source control that it may be for you. It is not for me. 

Monday, May 17, 2010

NHibernate 2.1 with SQLite

There is an error in the default configuration file that comes with NHibernate. It recommends the Driver Class :
"NHibernate.Driver.SQLiteDriver" in the hibernate.cfg.xml file. This referes to the SQLite Driver found at:
http://sourceforge.net/projects/adodotnetsqlite/
Wrongly (I believe) mentioned at : http://community.jboss.org/wiki/DatabasessupportedbyNHibernate
This version is no longer valid. The new (and awesome) version is http://sqlite.phxsoftware.com/
For this you will need to specify in the configuration "NHibernate.Driver.SQLite20Driver"

PS: cool link: http://devlicio.us/blogs/krzysztof_kozmic/archive/2009/08/14/testing-with-nhibernate-and-sqlite.aspx

Enjoy!

Friday, May 14, 2010

Setting up a portable Firebird Full with VS 2010

 Setting up a Portable Firebird
  • Downloaded the ziped version of Firebird: 
  • Firebird-2.1.3.18185-0_Win32.zip
  • Extact to a folder
  • I prefer classic architecture running as an application (you do not need FB Guard also The path to file with or without a missing / in the beginning will not cause DB corruption).
  • Start the server with the bat file containing:
    "./FIREBIRD/bin/fb_inet_server.exe" -a -p 3050
  • using this you can change the port to something else. The -p switch is optional. Even allocate the port dynamically from your application. Also you can use the -n switch to start it silently:
    "./FIREBIRD/bin/fb_inet_server.exe" -a -n -p 3050
  • Stopping the portable classic server is easy. Just kill the process fb_inet_server. Of course this is assuming your application is done with everything (no pending transactions). There is no point in leaving the server running.
Changing the SYSDBA Password (default:masterkey) Actually it is masterke (since it is limited to 8 characters). But both work :)
  • FIREBIRD\bin>gsec  -user sysdba -password masterkey
  • GSEC>modify sysdba -pw yourpass
  • GSEC>quit
yourpass will be the new password

Connect to the database (connection String):
  • syntax
    IPAddress_or_ServerName/Port:Drive:\CompletePathToYourDatabasefile.fdb
  • e.g
    150.236.161.183/3051:D:\database.fdb
  • for softwares (e.g. FB development studio) that do not give you the option of port separately you need to specify the server as "IP/port". They use fbclient.dll 
For setting up Visual Studio 2010 with ADO.NET Entity Framework Designer just follow the procedure in the below post:

http://basaratali.blogspot.com/2010/02/setting-up-firebird-development-with-vs.html

    ADDITIONAL NOTES:

    To run as SuperServer

    • Start the server with the bat file containing:
      "./FIREBIRD/bin/fbguard.exe" -a
    • Firebird should now be running fine (with an FB guard icon in the taskbar notification area) Note: there should not be any whitespace after -a switch other than newline. Otherwise FBGuard thinks you are trying to provid additional arguments and does not start.
    • You might want to put a shortcut to this script in the startup. FB Guard ensures DB restart in case it crashes.
    • Alternatively you can use fbserver.exe -a -n -p 3051
    • This way you can even overwrite the default port on the command line (-p switch). But FB Guard is recommended for SuperServer.

    Changing the default Port (3050) via the configuration file:
    • You might want to change it so that your portable version does not conflict with firebird installed by someone else.
    • open firebird.conf (in the main folder)
    • Uncomment and change the following line:
      RemoteServicePort = 3051
    • Restart Firebird
    Choice of Firebird Architectures:
    According to : http://www.slideshare.net/mindthebird/why-firebird-fact-for-decision-makers?from=ss_embed Classic can handle larger data and more users. Enough said :). Also I recommend the other presentations from : http://mindthebird.com/downloadmtb.html 

    Using ISQL to create a new database (Recommended)
    • isql is found in your firebird bin folder. Just double click it
    • Type in the following:
      CREATE DATABASE 'D:\yourdb.fdb' user 'SYSDBA' password 'masterkey';
    • Of course use your own SYSDBA password and the location of where you want the database to be.
     
    Using Firebird Maestro to create a new database
    I used Firebird Maestro to create the database. You need to use :
    • Create New Database
    • Protocol:Embedded Server
    • Database:Complete path to where you want the fdb file
    I feel it is the best manager directed specifically for firebird.

    Thursday, May 13, 2010

    PostgreSQL and .NET (Entity Framework and all)

    Npgsql is the way to go:
    http://npgsql.com/index.php/2008/11/npgsql-version-2-full-support-for-net-35-and-entity-frameworks/

    PS: another great find (I think) :
    http://npgsql.com/index.php/2009/08/how-to-set-up-entity-framework-npgsql-part-1/

    Especially like that you can use EdmGen2 to update your edmx files from the command line (like sqlmetal but for ADO.NET entity framework)



    Enjoy!

    Wednesday, May 12, 2010

    Reading xls and xlsx files from .NET

    Free library to read XLS and XLSX files from .net: 

    http://sourceforge.net/projects/koogra/


    I thought it deserved a mention :). Written in C# and available with MIT license (aka free for commercial use).

    On the commercial front I recommend (you also get to read CSV and fixed column width files reliably from same API) :
    http://www.csvreader.com/
    They have the full version available for free testing so:
    I tested there performance for XLS and XLSX.
    For XLS it can load the sample customers sheet with 65536 rows (max allowed :)) in under 1 second
    For XLSX it can load 80 thousand in reasonable time  (just wanted to check if it could handle the new row limit extension :) Also, Excel 2007 is not that happy opening this file either ).


    There is also gembox spreadsheet. But since they don't have a full featured test version I cannot comment on its performance / my opinion.

    Enjoy!

    Limitations for databases that will affect your software

    There are a large number of things to look in a database upon selection. Here are some that have caused me problems:

    • Column name size. e.g  firebird it is 31 
    • Alter table e.g. SQLite does not allow drop column
    • Authentication e.g. ScimoreDB 3.0 does not have db password protection 
    • Max DB Size : e.g. SQLite can only handle databases of 2TB 
    • Embeddability: e.g. MSSqlServer cannot be packaged as a part of your application (SQL Compact can be ... but it has its own HUGE limitations) . MySQL is a 60MB+ database. 
    • Licence : MySQL is GPL and not free for commercial use. Per installation cost as of 5/12/2010 is about $500
    • Reclaim Disk Space: Deleting data from a firebird does not shirk the database size. It is stored in the firebird free space and reused but cannot be reclaimed without backup/restore. 
    • Performance : test it out ... you never know ... depends on the database as much as on your configuration / your application code / the application interface. 
    Note: all databases mentioned here are great for a there own things e.g: Scimore has the best distributed architecture I have seen , SQLite is the best "Embedded only" database there is with an awesome .NET provider, Firebird has great entity framework support and so on.

    Enjoy! 

    Tuesday, May 11, 2010

    Oracle's select from dual for Firebird

    This will definitely come in handy:
    For firebird there is a system defined table that contains one and only one row RDB$DATABASE.

    so you can get the current time e.g. from the following query:
    select current_time from RDB$DATABASE;

    Enjoy!

    Sunday, May 9, 2010

    Free for commercial use + high performance + feature rich + client server + embeddable databases

    Here's why what is tested and rejected:

    SQLite: Client+server is not good (SQListening has a few limitations so don't go there! e.g. multiple columns with same name cannot be returned)  + writes lock the whole file (under milliseconds and reading is still allowed ... but you should be aware ... ) . And due to inherant complications of reliability of file sharing systems it cannot work over LAN (biggest bummer). Drop column is not supported (and that is something that my database will do quite commonly ... counter activation/deactivation etc. )

    MySQL: Not free for commercial use (GPL). 500$ . Not to mention that oracle now owns it (and I am still convinced it will result in a conflict of interest). PS: it is highly feature limited.
    http://www.borland.com/resources/en/pdf/white_papers/ib_vs_MySQL.pdf  Note : Interbase(IB) was the original source of Firebird in case you did not know.

    ScimoreDB: Great but not feature rich (e.g. no upsert support , limited data types ... improved in version 3.0 (RC version) and password protection to be added by 3.5 ... but not yet )

    So it boils down to:
    Note both are GREAT databases. This is just my opinion as I have to make a choice of one :) .








    Firebird:
    + Easier to Embed
    + Lesser strict system requirements
    + Easier to master :) .. with the right book ... "The Firebird Book"
    + Smaller installation (under 25 MB if you go crazy and include everything redundant as well)
    + Mature ADO.net Entity framework Visual studio designer.
    -ve Does not free disk space on deleting old data (put does recover it for usage by new data). You need to backup and restore. Will be fixed in firebird 2.5 though :)

    PostgreSQL:
    + .net 4.0 compiled driver already available ( may/9/2010 ). .net driver is called npgsql
    + more features (but I don't need them) e.g. PL/Python!  + partitioning + HUGE tables + partitions
    -ve Can only work on NTFS. Not neccessarily a bad thing at all. They dont trust FAT ... and I don't blame them :). I wouldn't put firebird on it either (cause it will be limited to 4GB and once that overflows your database has a high likelihood of corruption)
    -ve 30MB+ install. Not bad at all but still.


    You should try out both. Tell me what you chose. I chose Firebird. It has greater .net support ... and .net guys seem to like it more .. probably because of its easier portability (one file db .. no installation required for running firebird on windows)

    BTW for PostgreSQL I am reading PostgreSQL 8 for windows.
    For Firebird I just read the manual and then reading  "The Firebird Book".

    Enjoy!

    Thursday, May 6, 2010

    SQLite for Visual Studio 2010

    I just downloaded SQLite version "SQLite-1.0.66.0-setup". The EF designer works as expected on Visual Studio 2010. Well Done TEAM! you guys Rule!

    Thanks and I will try to give as much back as I can. Amazing product. Best there is for a startup by FAR!

    Enjoy!

    Update June 19 2011 : As mentioned in the comment below SQLite for .NET now has a home sqlite.org !  : http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki

    You can download the latest version from here : http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

    Update June 28 2011 : As mentioned by John below the Designer support with the latest version is not available as of yet. You are free to use the old version of system.data.sqlite that comes with designer support for VS2010.

    Wednesday, May 5, 2010

    Profiling applications with Visual Studio 2010

    Previously I had Visual Studio 2008 Professional. I read about profiling applications in .NET and discovered that it was not offered with Visual Studio 2008.

    Now I have access to Visual Studio 2010 Ultimate. There are many tutorials for this out there. But only one I could find that showed the basic walk though:
    http://msdn.microsoft.com/en-us/library/ms182398.aspx

    After this if you have used profiling in java (damn easy and free) you should not miss anything.

    Monday, May 3, 2010

    Upserts in various database systems

    Upsert is a common database term that is used to signify the operation "Update the row... if an entity with the same key does not exist then insert it"

    Here's how I do it in various databases:

    MYSQL

    REPLACE / IGNORE Keywords
    It follows the same syntax as insert except that it replaces the old row if the same entity exists:
    REPLACE INTO tablename VALUES ( 'whatever' )
    In case you want to ignore the insert in case the keys exists you could do:
    INSERT IGNORE INTO tablename VALUES ( 'whatever' )

    SQLite
    Did I tell you I love SQLite? Anyways:
    ON CONFLICT clause

    Microsoft SQL Server
    Didn't encounter the REPLACE but used ignore quite commonly. You can acutally do it on a table level

    CREATE TABLE TEST(
        [ID] [int] NOT NULL,
         CONSTRAINT [PK_TEST] PRIMARY KEY CLUSTERED
        (
            [ID] ASC   
        )WITH (IGNORE_DUP_KEY = ON) ON [PRIMARY]

    ) ON [PRIMARY]







    Enjoy!