Thursday, December 25, 2008

Time Zone ability for SQL Server 2005 and 2008

Few months ago we needed Time Zone functionality for one of our projects. This project is implemented using .NET 2.0 and SQL Server 2005. And as we know, there is no time zone functionality in either of the them. After doing some search, one of my developers  stumpled across this article on code project http://www.codeproject.com/KB/dotnet/WorldClock.aspx. This article describes how to use Windows built in timezone functionality. We used this approch to implement timezone functionality. Next we needed the same functionality in SQL Server. Luckily SQL Server 2005 and up supports .NET CLR. We used the same classes and created .NET CLR functions (http://msdn.microsoft.com/en-us/library/w2kae45k(VS.80).aspx). As this approch makes use of unsafe code, assembly needs to be attached using the unsafe option. By default, this option will not work. SQL Server Database has to be set trusworthy inorder to attach an unsafe assembly.

Hope this helps.

Saturday, October 18, 2008

Database scripts and contiguous integration

Few weeks ago, one of our projects went online for beta testing. Its a SaaS app and follows the one database per customer model. To standardize the process of creating a new customer we decided to use the new SQL Server power shell (sqlps) script. This shell is free can be obtained by downloading SQL Server Management Studio 2008 Express.

We kept our database scripts in VS.net database projects. Idea is to create the database with sql scripts everytime before running the unit tests. This way we can detect any mistakes in the sql server scripts. We wrote two power shell scripts. First one to combine all the sql scripts together into a file and the other one to drop and create the database. Integration with CC.net was done using the Executable Task in CC.net.

Friday, October 10, 2008

Cloud for Windows

Recently Amazon made an announcement that they will be introducing Windows Server and SQL Server in cloud. However, they are not first ones to do that. There is already a cloud service called Go Grid. It been around since March and not many people know about. For our latest project we decided to give it a try and found it to very useful. Few advantages Go Grid have right now over EC2
· 24/7 technical support that comes with the account at no extra change.
· Nice interface for creating and deleting servers.
· Licensed Win 2003, Win 2008 and SQL Server 2005.
· Have prepaid plans.
· Free F5 load balancers.
· They have a $50 trial plan.

Few disadvantages compare to EC2
· Right now they don’t have a cloud storage service. (According to Go Grid blog this feature will be made available very soon).
· They don’t have a backup solution. To backup one has to use some third party solution as mozy.com or S3.
· No support for custom OS images for now.
· Will get charged for server resources even if the server is shutdown. Only way to not incur charges is to delete the server.
· Currently their instances are limited to 2GB of RAM. However, this month they are supposed to be introducing 4GB and 8GB instances. (Can’t wait for that.)

Overall, my experience with the service is good except some initial setup issues. I would recommend anyone who is interested in hosting their application in a cloud give Go Grid a try.

Sunday, October 5, 2008

Memcached 1.2.6 for Windows

Memcached 1.2.6 for Windows can be downloaded from the following link

http://code.jellycan.com/memcached/

It has quite a few bugs and crash fixes.

Sunday, September 7, 2008

Improve ASP.net response time

-         View State

o       Reduce the use of view state as much as possible.

o       If need to store objects in view state consider using type convertors.

o       Move view state to the bottom of the page.

o   Compress View State.

-         Java Script and CSS

o       Try to use ToolkitScriptManager instead of ScriptManager if using ASP.net AJAX Control Toolkit. It has the ability to combine Toolkit scripts together to reduce number of request.

o       Use release mode in production for ToolkitScriptManager to get already compact ASP.net AJAX Control Toolkit script files.

o       Combine custom java script and CSS files into one file.

o       Cache CSS and Java Script in the browser.

o       Compact Java Script and CSS to reduce size.

-         Images

o       Cache Images in browser.

-         Cache

o       Cache data and page output. (Note: ASP.net cache is not a distributed cache and will not work in a Web farm. Consider using memcached and memcached providers.)

Saturday, September 6, 2008

Creating an Alert System with NHibernate, WCF and MSMQ

For a project that I am working on we needed to create an alert system. This system alerts the user about certain changes in the database via email or text. When I started googling for an alert system, nothing relevant came up. But then trying to solve another problem I came across two very interesting posts.

- Manuel Abadia's ASP.NET stuff - NHibernate and calculated properties
- SOA'izing MSMQ with WCF (and Why It's Worth It)

Using these two posts we were able to create a subscriber based alert system. For our solution we created a marker interface IAlert and have all domain objects we needed to alert on inherit from this interface. Instead of using the FindDirty method as mentioned in the first post, we used OnSave [for Insert], OnFlushDirty [for Update] and OnDelete [for delete]. In these functions we place a check if the object is of type IAlert we converted its current and previous state if any into an xml packet. Then that packet was saved on the HttpContext.Current.Items collection. The reason for saving it on the Items collection is that transaction could fail and we don’t want to generate alerts for failed transactions. Then in the PostFlush method in the interceptor we called the WCF service that places that xml packet on to the MSMQ queue. This operation is asynchronous and does not add time to the transaction. On the other side we wrote a WCF service that processes that packet. This way, alert was sent asynchronously and transaction time was not effected. Another advantage of using this approach is processing WCF service has all the information needed to process that alert in the xml packet and only needs to hit that database when it needs to gets that list of subscribers to send messages.

Saturday, August 30, 2008

Context Based Sessions in NHibernate

I have seen a lot of people asking questions about creating context based Sessions in NHibernate. One of the solutions that people have suggested is to create a new Session Factory for each database. This solution sounds attractive, but will become problematic as the application scales up. In this post I would to share my solution that we implement in my current project.

NHibernate lets the developer provider their own ADO.net connection. As we were using Open Session in View strategy we decided to pass the connection string to create a session using ISessionFactory. OpenSession(IdbConnection obj). By using this technique NHibernate’s second level cache is disabled. For our application we were not interested in using a second level cache so it didn’t matter. If you are interested in using second level cache solution is to implement your own IConnectionProvider. Information about which database to use can be passed using HttpContext.Items.

NHibernate context is feature that is very rarely talked about. NHibernate Session Factory provides a method GetCurrentSession() this lets you get current session in the context. NHibernate provides built in implementation for web application. For more information please review section 2.3 of NHibernate documentation. This way the DAO and Business Object don’t have to worry about how to create the session and remain agnostic of which database they are connecting to.



Please comment. Thanks