Saturday, 21 July 2012

Back to basics(VSDBCMD.EXE command-line)

This is a command-line tool use in conjunction with new Visual Studio 2010 (VS2010) Database project type.

If you are new to VS2010 Database projects then I strongly recommend having a quick read on MSDN. In a nutshell Database project type allows you to source control the artefacts that you normally add to a SQL Server database (i.e. user defined functions, stored procedures). The output of this type of project is a *.sql file that you can run against the SQL Sever to drop and re-create the database with your artefacts in place. The output script takes care of checking for objects already created and dropping, re-creating in a consistent manner. 

We can "build" this type of project in a TFS build server. However the output you get once you "build" this type of project in TFS or VS2010 is not the final *.sql file. So if you right-click the project and "build", what files do you get? (see the following)

Figure 1: Build output
No *.sql file is generated!. So why is it not generating *.sql file. We will need to build the project as  "Deploy" in order to generate the file. (see below)


Figure 2: Building the project as "Deploy"

Great, now we know how to do this using the UI. But since TFS projects are build as "Release" in the build server, we will need to use the VSDBCMD.EXE to generate the *.sql file.
See the MSDN complete overview of this tool.

This is how I used the command-line tool to generate *.sql deployment script. By the way the VSDBCMD.EXE executable is in "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Deploy" folder.


Figure 3: Executing VSDBCMD.EXE with manifest file.

As per the above log message, the deployment *.sql is now created. 

We can in theory execute this in the TFS build server itself. If you are interested in this, check this article in MSDN.

Wednesday, 18 July 2012

Back to basics (IDisposable)

In simple terms Disposable pattern provides a method to de-allocate resource in a deterministic fashion.

The .NET Framework provides the  "IDisposable" interface which we should use to implement the disposable pattern. The interface is very simple (see below).

        public interface IDisposable
        {
            void Dispose();
        }


The "expensive" resources are considered to be calls to files, databases, streams. We should consider implementing IDisposable pattern and gracefully release resources where necessary. So, how do we go about implementing the IDisposable pattern. Consider the following code snippet.

    internal class ExpensiveResource : IDisposable
    {
        private bool _disposed = false;

        public void DoExpensiveWork()
        {
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                ReleaseExpensiveResources();
                _disposed = true;
            }
        }

        private void ReleaseExpensiveResources()
        {

        }
    }

 To start, we have implemented the "IDisposable" interface for the type "ExpensiveResource". We have a overloaded  "Dispose" method. We use the overloaded method to add our custom code (i.e. code to release resources). 

Most importantly, the "Dispose" method can be called multiple times.Therefore we need to add safe guards in place. This is where "_disposed" field comes into play.

Since we are taking the ownership of disposing the object, there is no need for the Garbage Collector (GC) to call the "Finalize" method to dispose the object. Therefore we can suppress the call to Finalize. This is carried out by calling the "GC.SuppressFinalize(this))".

The C# "using" keyword can be used to elegantly dispose a disposable object. The consumer can write the following.

    internal class MyTestClass
    {
        public void DoStuff()
        {
            using (var expensiveWork = new ExpensiveResource())
            {
                // Do work
            }  - (1)
        }
    }

By the time the execution reach (1) the dispose defined in the type is called and object is disposed. 

.NET Tasks and IDisposable

.NET Framework "Task" by design implements the "IDisposable" interface, therefore an disposable object. But if we have to keep calling dispose at the end of each Task, then the source code will most surely become unreadable.

Read the following post from Stephen Toub - MSFT for more on disposing Tasks.

[The recommendation is not to dispose a Task unless you explicitly cause a Task to allocate a  IAsyncResult.AsyncWaitHandle.]