Codoxide.Common Library Version 0.0.1 (Concept Phase): Database Wrapper, Basic Design Patterns, Configuration Handlers

Today I’m so excited to announce the first release of the Codoxide.Common Library. I haven’t been over ambitious about the version numbering as I want you to treat this library to still be in concept stage.

Unfortunately, this release had taken longer than expected cos of my newly cramped up schedule. Worst part of that is I’m posting this code without proper documentation (yep, excuses!). Nevertheless, there’s plenty of code in here to prove useful to many. So here goes! Continue reading “Codoxide.Common Library Version 0.0.1 (Concept Phase): Database Wrapper, Basic Design Patterns, Configuration Handlers”

VSTO App Deployment using NSIS – Part 2

Detecting and Deploying Prerequisites

This is the 2nd of a multi-part series on deploying VSTO Applications using NSIS. Part 1 of the series can be found here.

Step 4: Ensuring MSI 3.1 is available

Before proceeding you need to make sure the client computer has MSI 3.1 installed. Click here to find out how.

Step 5: Checking whether Excel is Installed

Being an Excel Worksheet-Level Customization, it makes no sense for this app to be deployed on a computer that doesn’t have MSO Excel 2003 Professional installed. Lets see how we can achieve this. If you do a bit of research you can find out that with VS Deployment Projects, this is achieved by doing a Windows Installer search for the component ID {A2B280D4-20FB-4720-99F7-40C09FBCE10A}.

Since performing a Windows Installer Search is a common and desirable feature, I came up with the following macro:

!macro GenerateMsiSearch functionName componentId 0
    Function ${functionName}
        Push $0
        Push $1
        StrCpy $0 ""
        StrCpy $1 ""

        System::Call "msi::MsiLocateComponent(t '${componentId}', t, *i) i .r1"
        DetailPrint "Component ID: ${componentId} Install State: $1"

        ${If} $1 == '3' ; installed on local drive
            StrCpy $0 '1'
        ${ElseIf} $1 == '4' ; run from source, CD or net
            StrCpy $0 '1'
        ${ElseIf} $1 == '-3' ; Path  to the component returned more data than our buffer can handle. Which means we do have this installed.
        ${Else}
            StrCpy $0 '0'
        ${EndIf}

        Pop $1
        Exch $0
    FunctionEnd
!macroend

When invoked, this macro will generate a function, with the give name that will search for the specified Component ID. So the code to detect Excel 2003 becomes:

!insertmacro GenerateMsiSearch "IsExcelInstalled" "{A2B280D4-20FB-4720-99F7-40C09FBCE10A}" ; Version 2003

Section "MainSection" SEC01 ; Determine whether the user has the required version of Excel installed
    Call IsExcelInstalled
    Pop $0
    ${If} $0 == '0'
        MessageBox MB_OK "A compatible version of Microsoft Office Excel was not found. Setup cannot continue."
        Abort
    ${EndIf}

So, why did I make this a macro that generates a function, and not just a function that accepts the component ID as a parameter? Well, parameter passing is not that straight forward with NSIS, and I believe this leads to much cleaner code at the end.

Step 6: Ensuring .NET 2.0 Runtime is Present

You can find plenty of ways to do this on NSIS web site. Here’s one of them.

Step 7: Ensuring VSTO is Present


!insertmacro GenerateMsiSearch "IsVstoInstalled" "{D2AC2FF1-6F93-40D1-8C0F-0E135956A2DA}"

Function InstallVSTOSE

    Call IsVstoInstalled
    Pop $0

    ${If} $0 == '0'
        SetOutPath '$TEMP'
        SetOverwrite on

        Banner::show /NOUNLOAD "Microsoft VSTO SE..."
        File 'C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\vstor\vstor.exe'
        ExecWait '$TEMP\vstor.exe /q:a /c:"install"' $1 ; '$TEMP\vstor.exe /q:a /c:"install /q"' $1
        Delete '$TEMP\vstor.exe'
        Banner::destroy

        ${If} $1 == '0'
            DetailPrint "VSTO 2005 SE was installed successfully."
        ${ElseIf} $0 == '3010'
            SetRebootFlag true
        ${Else}
            DetailPrint 'VSTO 2005 SE Installation failed with error $1'
            Abort
        ${EndIf}
    ${Else}
        DetailPrint 'VSTO 2005 SE already installed.'
    ${EndIf}
FunctionEnd

We now have the scripts in place to deploy the application components as well as the to determine all prerequisites are available. All that is left is to add the boiler place Update Manifest and Set Security code. Easiest way to do this is to bring the code over a new .NET EXE project and pass the installation-dependent (e.g. Installation Path etc.) parameters as command line arguments. You can then execute the EXE as the last step of the installation.

VSTO App Deployment using NSIS – Part 1

For a little over a year, we’ve been working on a very sophisticated Microsoft Excel 2003 Document Level Customization project. We have nothing but pride towards the ingenuity of this application. But, there was one aspect that consistently overshadowed its success: Deployment.

Visual Studio Deployment Projects help you generate a setup app quickly and easily. It has the ability to look up source projects and determine all the components that need to be deployed. This makes things remarkably easy when you want a rudimentary installation. But, for anything out of the ordinary,  you will find that this offers you very limited help. To workaround these limitations you have to pick up a tool called Orca. Now, here’s where things get really dirty.

To use Orca, you have to have some semblance of the MSI database format. That isn’t actually so bad. A simple search on the Internet will fetch you loads of information regarding the subject. But, figuring out the format is just the beginning. You then have to weed your way using some ugly hacks. That’s not all; you have to reapply those hacks with each build. If you are willing to live with the inconveniences, there’s still a lot you can achieve using Orca. Musical Nerdery has a neat example on the subject.

Anyway, the point is we got sick and tired of working within these constraints and started looking for alternatives. After some serious considerations we opt to go with NSIS (Nullsoft Scriptable Installer Suite). We were mostly excited about its low footprint and its powerful scripting language. Continue reading “VSTO App Deployment using NSIS – Part 1”

BreadCrumbs Control: Final Touches and Demo Project

After weeks of inactivity, I finally managed to get a demo project in place for the BreadCrumbs controls. You can download it here. The code includes few updates I have made since the last version. For the sake of flexibility, I have kept the basic control as simple as possible. In the demo, I have added few additional helper classes to provide the full functionality desired.

Extending the control is pretty simple too. For instance, to display the menus for each of the sub items in a list item, you handle the SubItemClicked event. The SubItemClickedEventArgs passed to you along with this event provides you with the row index, column index and the bottom-left corner pixel coordinates of the sub item. In the demo app, this event handled as;

void ListBox_SubItemClicked(object sender, BreadCrumbsListBox.SubItemClickedEventArgs e)    
{    
    ContextMenuStrip menu = null;    
    if (e.ColumnIndex == 1 && breadCrumbsList.ListBox.SelectedItem != null)    
        menu = GetTemplateMenu(breadCrumbsList.ListBox.SelectedIndex);    
    else    
        menu = _entityMenu;    
    menu.Show(breadCrumbsList, breadCrumbsList.PointToClient(e.BottomLeftCorner));    
}    

Here, the _entityMenu is a pre-constructed menu while as the “Template Menu” is reconstructed every time based on the selection.

Finding my way back to the Bread Crumbs Project

Due to a life changing experience that I ran into 2 weeks ago, I’ve been unable to continue on with my regular R&D work. I’ve got a small breather that might last the rest of the week and so figured I should try to finish up some of my projects: 1st off, I managed some work on the Bread Crumbs list control.

Here’s what the control looks right now:


Items support Hot-Tracking

Vista’s Windows Explorer style item selection

Sub-item selection

Sub-item Menu Support

Also added support for item editing:

Design

The controls design is pretty basic and the code not overly elegant at this stage.

Each row on the List Box is an instance of the BreadCrumbsItem class. The BreadCrumbsItem holds an array of strings with each element corresponding to  an individual column. The list box requires that you insert only items with equal number of columns. Otherwise an exception will be thrown.

I had trouble getting the scrollbars inherent in UserControl/ContainerControl to function properly. The OnScroll method wasn’t receiving the proper scroll value and was reverting to 0 all the time. So, I had to go with adding a vertical scroll bar manually. Anyway, check out the code (49.5 KB zipped folder).

My Favorite Database Wrapper for C#

As a guy who started programming back in the days of DAO, I have nothing but love for ADO.NET. But let’s face it, you can still end up writing pretty lousy code with it. Not everybody’s fully aware of the best uses of the ADO.NET. One of the most common problems I keep stumbling onto in other peoples code is the poor use of ADO.NET Connection Pooling feature.

Somewhere, down the line Microsoft probably started noticing that their cool new data access model wasn’t being fully utilized. The birth of the Microsoft Enterprise Library was probably due to this.

As cool as the Enterprise Library is, I’ve found it to be an over kill for most of my projects. In fact one of my favorite projects featured on CodeProject, was because Enterprise Library’s Offline Application Block was so clunky (and very limited).

Around the same time that I worked on on the SCOAB (Smart Client Offline Application Block), I also wrote a ADO.NET wrapper similar to the Enterprise Library. It pretty much follows the public interface of the Enterprise Library, but it’s a very tiny wrapper compared to the enormous DAAB (Data Access Application Block).

You can download the .cs file here: AbstractDatabase.zip (1.88 kb)

This contains a generic abstract base class called AbstractDatabase. You can extend this class to use it with any type of relational database type. If you take a look at my SQLite Membership Provider, you’ll find this class derived to be used with SQLite ADO.NET Wrapper. I have simply called that class Database, and it’s code looks something like this:

public class Database : AbstractDatabase<SQLiteConnection, SQLiteCommand, SQLiteDataAdapter>
{
    protected override string  GetConnectionString()
    {
        return string.Format(ConfigurationManager.ConnectionStrings["BlogEngine"].ConnectionString,
                            HttpContext.Current.Server.MapPath("~/App_Data"));
    }
}

As you see, the only member you need to override is the GetConnectionString() method. This brings up the question as to why I made this abstract. Why not read the connection string value from the web.config? Well, I’ve done this because I use this class on both web and desktop projects. And in some cases, especially with WinForms applications, the database is SQLite and resides in the application directory. When that happens, I can always hard code the database file name.

Here’s a example of the the Database class’s usage (taken from the SQLiteBlogProvier class).

using (Database db = new Database())
    using (DbTransaction txn = db.BeginTransaction())
    {
        // SQLite doesn't support TRUNCATE
        DbCommand cmd = db.GetSqlStringCommand("DELETE FROM be_Settings"); 
        db.ExecuteNonQuery(cmd, txn);

        cmd = db.GetSqlStringCommand(
                    "INSERT INTO be_Settings (SettingName, SettingValue) VALUES (@name, @value)");
        db.AddInParameter(cmd, "@name", DbType.String, "");
        db.AddInParameter(cmd, "@value", DbType.String, "");
        
        foreach (string key in settings.Keys)
        {
            cmd.Parameters[0].Value = key;
            cmd.Parameters[1].Value = settings[key];
            cmd.ExecuteNonQuery();
        }
        txn.Commit();
    }

A quick rundown of the public interface of AbstractDatabase class:

void AddInParameter(System.Data.Common.DbCommand, string, System.Data.DbType, object)
void AddInParameter(System.Data.Common.DbCommand, string, System.Data.DbType, int, object)
DbTransaction BeginTransaction()
void Dispose()
DataSet ExecuteDataSet(System.Data.Common.DbCommand)
int ExecuteNonQuery(System.Data.Common.DbCommand)
ExecuteNonQuery(System.Data.Common.DbCommand, System.Data.Common.DbTransaction)
DbReader ExecuteReader(System.Data.Common.DbCommand)
ExecuteReader(System.Data.Common.DbCommand, System.Data.CommandBehavior)
T ExecuteScalar<T>(System.Data.Common.DbCommand, T)
abstract string GetConnectionString()
DbCommand GetSqlStringCommand(string)
DbCommand GetSqlStringCommand(string, params object[])
DbCommand GetStoredProcedureCommand(string)
Connection { get; }

Update (June 20, 2008): I have updated this class with a minor bug fix and have added support for other parmeter types including Output and InputOuput parameters (Thanks Yordan).

Update (Oct 20, 2008): This wrapper has been updated and made a part of the Codoxide Common Library

Breadcrumbs User Control – Part 1

Over the past few months, I’ve been working on a pretty high-end VSTO application. There had been some really ground breaking concepts thrown into that project which unfortunately, cannot be posted here for obvious reasons. What I can do is discuss a little widget I’m hoping to build that would eventually find it’s way into that project.

Background

The application contains an Advanced Search feature where the user can build reusable search template (called a Saved Search) as well as a reusable results templates. A “Saved Search” is basically a collection of search criteria linked to a results template. The results template decides which fields from are displayed from the “results set”.

We have 2 wizard UIs for configuring Saved Searches and Result Templates. The first page for each of the  wizards are almost identical in the fact that they asked the user to

  1. Pick an Entity class and then,
  2. Type in the name for a new saved search/result template to create or pick from a list of existing templates.

Since both the UI requirements were similar (and much similar UIs were required on few other places), this was implemented as a User Control that looked like:

All the text labels on the control were configurable and each time the selection changed in the Entity class drop down, it would fire an even asking for an array of existing items. On the wizard page this control was placed, the event was handled and the array filled with either the list of Results templates or saved searches for that particular Entity class.

Limitations of the Existing UI

The above interface was good enough initially. But it became quite cumbersome as the number of Entity classes grew (and it could potentially grow up to hundreds) and as more and more templates/searches were being saved. The user  was forced to go through all the Entity classes one by one if he wished to see all the saved items. I later added a Recent Items list but, the problem still required a better solution.

Adopting Vista Breadcrumbs Concept

One of the coolest new features in Vista is the breadcrumbs bar in Windows Explorer. The ease of which this allows you to navigate through folders is simply amazing.

And that got me thinking; can I adopt this to improve the user experience in the above case?  Here are my initial designs:

You would notice the peculiar menu items at the bottom indicating a character range (e.g. A-N). Well, I hope to make this work like Outook’s Address book buttons; probably using the code found here.

In the coming weeks, I hope to get some code out based on this concept UI. Obviously, there’s a lot of work required for a clean implementation. I can in the meantime get a quick and dirty , proof of concept control done using a auto scrolling Flow Panel and an array of Toolbar Strips. Well, stay tuned…

BlogEngine.NET configuration for SQLite Providers

This is a quick rundown of the changes required for gettting BlogEngine.NET work with SQLite.

  1. Get log4net (http://logging.apache.org/log4net/) and SQLite ADO.NET Provider (http://sqlite.phxsoftware.com).
  2. Change “blogProvider” to SQLiteBlogProvider
    <BlogEngine>
    <blogProvider defaultProvider="SQLiteBlogProvider">
    <providers>
    <add name="SQLiteBlogProvider" type="BlogEngine.Core.Providers.SQLiteProvider.SQLiteBlogProvider" />
    </providers>
    </blogProvider>
    </BlogEngine>
    
  3. Set role and membership providers to SQLite. Remember that you should set the applicationName as soon as possible (trust me, I got burnt a few times by not doing so).
    <membership defaultProvider="SQLiteMembershipProvider">
    <providers>
    <clear/>
    <!-- <add name="XmlMembershipProvider" type="BlogEngine.Core.Providers.XmlMembershipProvider" description="XML membership provider" xmlFileName="~/App_Data/users.xml"/> -->
    <add name="SQLiteMembershipProvider"
    type="BlogEngine.Core.Providers.SQLiteProvider.SQLiteMembershipProvider"
    applicationName="/Codoxide" />
    </providers>
    </membership>
    <roleManager defaultProvider="SQLiteRoleProvider" enabled="true">
    <providers>
    <!-- <add name="XmlRoleProvider" type="BlogEngine.Core.Providers.XmlRoleProvider" description="XML role provider" xmlFileName="~/App_Data/roles.xml"/> -->
    <add name="SQLiteRoleProvider"
    type="BlogEngine.Core.Providers.SQLiteProvider.SQLiteRoleProvider"
    applicationName="/Codoxide" />
    </providers>
    </roleManager>
    
  4. Setup log4net appender of your choosing.

The above is obviously a very lazy piece of documentation. It’s only there to serve as a guide for you to understand the projects web.config file where you’d find the complete customization requried.

Changes affecting sql.config

I have used the sql.config file that was used by the SQLBlogProvider with few modifications. Firstly, the connectionString attribute  is set to “Data Source={0}\blogengine.db”. The SQLite ADO.net provider requries that the data source contains the full path to the SQLite database. Therefore, the above string is used as the pattern for a string.Format operation where {0} receives the absolute file system path to app_data folder.

You will also notice an additional tag for “BlogEngine_LocalPath”, which contains an absolute path to the SQLite db file. Now, unlike the default XML provider in BE.NET, I’ve been too lazy to include a default user account into the database. So, you’d have to use ASP.NET Web Site Administration Tool to create user accounts. When invoked through the Admin Tool it becomes impossible to use Server.MapPath as the current HTTP Context turns out to be null. The “BlogEngine_LocalPath” element is there to be used in this situation.