-
A few days ago I made a post about using SQL CE 3.5 with LINQ to SQL. I described a way to use connection pooling with SQL CE. A gracious blog reader (Mike Brown) pointed out a way I could make my solution much simpler by using the [ThreadStatic] attribute. I never heard of this attribute but it is really nifty. You mark a field with it and then each thread that accesses that field will be accessing a unique instance of it!
Using this attribute my code for connection pooling went from this:
/// <summary>
/// Manages open connections on a per-thread basis
/// </summary>
public class ConnectionPool
{
private Dictionary<int, IDbConnection> threadConnectionMap;
/// <summary>
/// Gets the connection string.
/// </summary>
/// <value>The connection string.</value>
public string ConnectionString
{
get;
private set;
}
/// <summary>
/// Gets a connection.
/// </summary>
/// <returns>An open connection</returns>
public IDbConnection Connection
{
get
{
lock (threadConnectionMap)
{
int threadId = Threading.Thread.CurrentThread.ManagedThreadId;
IDbConnection connection = null;
if (threadConnectionMap.ContainsKey(threadId))
{
connection = threadConnectionMap[threadId];
}
else
{
connection = new SqlCeConnection(ConnectionString);
connection.Open();
threadConnectionMap.Add(threadId, connection);
}
return connection;
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ConnectionPool"/> class.
/// </summary>
/// <param name="connectionString">The connection string.</param>
public ConnectionPool(string connectionString)
{
threadConnectionMap = new Dictionary<int, IDbConnection>();
ConnectionString = connectionString;
}
to this:
/// <summary>
/// Manages open connections on a per-thread basis
/// </summary>
public class ConnectionPool
{
[ThreadStatic]
private IDbConnection threadConnection;
/// <summary>
/// Gets the connection string.
/// </summary>
/// <value>The connection string.</value>
public string ConnectionString
{
get;
private set;
}
/// <summary>
/// Gets a connection.
/// </summary>
/// <returns>An open connection</returns>
public IDbConnection Connection
{
get
{
if (threadConnection == null)
{
threadConnection = new SqlCeConnection(ConnectionString);
threadConnection.Open();
}
return threadConnection;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ConnectionPool"/> class.
/// </summary>
/// <param name="connectionString">The connection string.</param>
public ConnectionPool(string connectionString)
{
ConnectionString = connectionString;
}
}
Very nice and clean.
Thanks Mike!
-
Some History
The Snippet Designer was started as an intern project of mine during the Summer of 2006. The idea was to make snippet files (which were introduced to Visual Studio in 2005) a first class entity. Following this idea I created a Visual Studio plug-in which included an editor for snippet files and a search tool window to find snippets. The plan then was to release it on Codeplex but when my internship ended the project was forgotten about.
Today
Now after two year of sitting there I found the code, cleaned it up a bit and am releasing it on Codeplex as an open source project at http://www.codeplex.com/SnippetDesigner. My goal in releasing it on Codeplex is two fold. First I would like people to try it out, give me feedback and see if we can make it better (it still has plenty of rough edges). Second I would love to turn this into a community developed project where we all can collaborate on it.
Features
The Snippet Designer has two main features:
A Snippet Editor hosted in the IDE which lets you edit the code, make replacements and change snippet properties:
A Snippet Explorer which lets you search for Snippet files on your computer an quickly open and edit them.
In addition to these two features the plug-in adds a context menu item inside of the VB, C# and XML editor that lets you export any highlighted code into the snippet editor:
Getting Started
After installing the Snippet Designer you are ready to create and edit snippets. Either open up any .snippet file or in Visual Studio go to View -> Other Windows -> Snippet Explorer and use the Snippet Explorer to search for snippets.
I look forward to any and all feedback!
-
Using LINQ to SQL with SQL CE 3.5 can be a bit of a challenge. First off, the LINQ to SQL Visual Studio designer doesn't support SQL CE so you need to run sqlmetal from the command line to create the object model (or write it by hand). Once you get past this point then you can use LINQ to SQL the same way you would for SQL Sever BUT there is a catch.
The way LINQ to SQL is built makes it work well with SQL Server and its connection pooling ability. If you look at this FAQ under the "Database Connection: Open How Long?" section it says:
A connection typically remains open until you consume the query results.
Therefore given the following code:
1: using(Northwind db = new Northwind(MyConnectionString))
2: {
3: (from p in db.Products).ToList();
4: (from p in db.Customers).ToList();
5: }
On line 3 and 4 in the above code will open a database connection (which will be pulled from the open connections in connection pool), execute the query, and then close the connection after the operation is completed (which will return the connection to the pool).
This works great with connection pooling but when you move to SQL CE you don't have that luxury. What happens now is that for each query a new SQL CE connection will be opened, the query will be executed and then the connection is closed. Each query is incurring the cost of opening a new connection. To make matters even worse, in SQL CE the first open connection brings the database engine into memory, and once you have no open connections anymore it removes the engine from memory. What this means is that each time we close the only open connection and then re-open we are incurring a HUGE cost.
One way to get around this is to pass into the DataContext an open SQLCeConnection object. LINQ to SQL will only automatically close a connection if it opens it. Therefore, if you pass it an open connection then you won't incur this cost over and over again. This will work fine in a single threaded application but once you move to a mutlithreaded app where you are performing database operations from different threads you encounter a problem: The SQLCeConnection object is not thread safe. You need to have a different connection object per thread in order to make this work. What you want is to be able to request a connection from any thread and get an already opened one for that thread. This sounds a lot like a simple connection pooler, which could look something like this:
/// <summary>
/// Manages open connections on a per-thread basis
/// </summary>
public class ConnectionPool
{
private Dictionary<int, IDbConnection> threadConnectionMap;
/// <summary>
/// Gets the connection string.
/// </summary>
/// <value>The connection string.</value>
public string ConnectionString
{
get;
private set;
}
/// <summary>
/// Gets a connection.
/// </summary>
/// <returns>An open connection</returns>
public IDbConnection Connection
{
get
{
lock (threadConnectionMap)
{
int threadId = Threading.Thread.CurrentThread.ManagedThreadId;
IDbConnection connection = null;
if (threadConnectionMap.ContainsKey(threadId))
{
connection = threadConnectionMap[threadId];
}
else
{
connection = new SqlCeConnection(ConnectionString);
connection.Open();
threadConnectionMap.Add(threadId, connection);
}
return connection;
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ConnectionPool"/> class.
/// </summary>
/// <param name="connectionString">The connection string.</param>
public ConnectionPool(string connectionString)
{
threadConnectionMap = new Dictionary<int, IDbConnection>();
ConnectionString = connectionString;
}
With this you create a DataContext like this:
1: // Defined somewhere
2: ConnectionPool connectionPool = new ConnectionPool(MyConnectionString);
3:
4: // ...
5: // ...
6:
7: using(Northwind db = new Northwind(connectionPool.Connection))
8: {
9: (from p in db.Products).ToList();
10: (from p in db.Customers).ToList();
11: }
Now each thread will have its own open connection which will minimize the cost of opening connections.
-
Well, maybe not best friend but its a nice function. When working with bound collections in WPF you often end up dealing with a CollectionView. This is the MSDN documentation description of a CollectionView :
You can think of a collection view as a layer on top of a binding source collection that allows you to navigate and display the collection based on sort, filter, and group queries, all without having to manipulate the underlying source collection itself.
As stated above the main use of the CollectionView is to enable modifying the visible collection without actually changing the underlying data. In the application I am working on I let the user change what a ListBox is sorted by. This is the code I originally had:
1: ICollectionView dataView = CollectionViewSource.GetDefaultView(listBox.ItemsSource);
2: dataView.SortDescriptions.Clear();
3: SortDescription sd = new SortDescription(newField, ListSortDirection.Ascending);
4: dataView.SortDescriptions.Add(sd);
The problem with this code is that it causes the view to refresh twice! The Clear and Add function both trigger a SortDescriptionsChanged event which leads to a Refresh of the view. This problem becomes worse when you are also adding or changing filter in addition to sort descriptions and your collection is very large. This could visibly slow you application.
This is where my friend DeferRefresh comes in. If you change the code above to this:
1: ICollectionView dataView = CollectionViewSource.GetDefaultView(listBox.ItemsSource);
2: using (dataView.DeferRefresh())
3: {
4: dataView.SortDescriptions.Clear();
5: SortDescription sd = new SortDescription(newField, ListSortDirection.Ascending);
6: dataView.SortDescriptions.Add(sd);
7: }
Now the CollectionView will only be refreshed once! All the methods inside the using block will no longer cause a refresh, they will just indicate that a refresh is needed. Then when the code leaves the using block the method EndDefer() is called which just refreshes the CollectionView if needed.
-
I decided to do some Project Euler problems using F#.
So here is my first one, Problem # 31. Nothing in this solution really shows off anything special about F# but you have to start somewhere ;)
1: #light
2:
3: let rec combos amt (denoms:int list) =
4: if amt = 0 then 1
5: elif denoms.IsEmpty || amt < 0 then 0
6: else
7: combos (amt - denoms.Head) denoms +
8: combos amt denoms.Tail
9:
10:
11: printf "\n\nThe Anwer is: %i \n" (combos 200 [200;100;50;20;10;5;2;1])
12:
-
Quick code snippet time!
The following are generic methods for inserting and updating a detached entity into a database using LINQ to SQL.
1: /// <summary>
2: /// Updates the database with item.
3: /// </summary>
4: /// <typeparam name="T">Type of the item</typeparam>
5: /// <param name="item">The item.</param>
6: public static void UpdateDatabaseWithItem<T>(T item) where T : class
7: {
8: var store = GetNewDataContext();
9: var table = store.GetTable<T>();
10: table.Attach(item);
11: store.Refresh(RefreshMode.KeepCurrentValues, item);
12: store.SubmitChanges();
13: }
14:
15:
16: /// <summary>
17: /// Inserts the item into database.
18: /// </summary>
19: /// <typeparam name="T">Type of the item</typeparam>
20: /// <param name="item">The item.</param>
21: public static void InsertItemIntoDatabase<T>(T item) where T : class
22: {
23: var store = GetNewDataContext();
24: var table = store.GetTable<T>();
25: table.InsertOnSubmit(item);
26: store.SubmitChanges();
27: }
GetNewDataContext() is a method (not shown) which does what its name says, returns a data context.
The only part that is not really obvious is on line 11. That line is a "hack" to allow you to attach an entity as modified without using a timestamp or turning off optimistic concurrency or attaching a previous version of the entity. What this means is that this update will throw a fit if there is a concurrency error! However, for my use of this code ( mainly for unit test preparation ) it works great.
-
Take a look at the following code:
1: var sw = new Stopwatch();
2: sw.Start();
3: Enumerable.Range(0, 3).SelectMany((i) => Enumerable.Range(0, 50000)).OrderBy(i => i).ToList();
4: Console.WriteLine(sw.ElapsedMilliseconds);
5:
6: sw.Reset();
7: sw.Start();
8: Enumerable.Range(0, 2).SelectMany((i) => Enumerable.Range(0, 50000)).OrderBy(i => i).ToList();
9: Console.WriteLine(sw.ElapsedMilliseconds);
10: sw.Reset();
Line 3 is saying:
Generate a list which looks like 0,1,2..49999,0,1,2..49999,0,1,2..49999
Then Sort it.
While Line 8 is saying:
Generate a list which looks like 0,1,2..49999,0,1,2..49999
Then Sort it.
If you run this program you will see that line 3 executes ( depending on your machine) in less than a second while line 8 executes in around 30 seconds. This may jump out as really strange at first since you are sorting less numbers in line 3. But that isn't the issue in this scenario. The issue is the sorting algorithm.
Underneath the covers the OrderBy function is using a version of QuickSort. The version of quicksort used always chooses the middle element of the list as the pivot position. Since both left and right lists will always be sorted we hit the worst case of this quicksort algorithm which is O(n^2).
-
I have been working a lot with WPF and I found the following two FREE tools to be extremely helpful.
The first is:
XamlPadX - (http://blogs.msdn.com/llobo/archive/2007/12/19/xamlpadx-v3-0.aspx)
This is an enhanced version of XamlPad which comes with the .NET SDK. This is a light weight tool which parses and renders XAML code visually. It adds (in addition to just rendering XAML) a command interpreter, visual tree explorer, add-in support and much more.
I find this tool very usefull when I want to just play around with XAML and see how things render. It is quicker than firing up VS and using the Cider designer.
The other tool is:
Snoop - (http://www.blois.us/Snoop/)
Snoop is a visual debugger for WPF application. When any WPF application is running you can use snoop to view its visual tree and all the elements properties. You can search for any element, view its properties, change its properties and see how the application changes.
This program really helps in understanding what is going on in your WPF application and finding problems.

-
I thought I know how the foreach construct worked under the covers. I figured the compiler would check if the type being iterated over implement IEnumerable or IEnumerator. And if so it will call MoveNext and Current to loop over the elements. But then I read this:
http://blogs.msdn.com/kcwalina/archive/2007/07/18/DuckNotation.aspx
In that post Krzysztof reveals that in reality implementation of those interfaces are NOT checked. In fact, it uses duck typing to determine if iteration can occur. The compiler will just look for a method that matches a certain signature and use that.
Very interesting!
-
With the previous two modules in place we are now set up to use a DFA to match against a string. In my implementation I support either a greedy match or an short match. In a full featured regular expression engine this ability to choose greedy or not would be per operator but for simplicity I have it for the overall match.
To do the matching I have a general function which will create a list of all matches. Then the difference between short and greedy matching is which of the candidate solutions does it choose.
This is the method:
1: doMatch func machine st [] = doAccept machine st []
2: doMatch func machine st string = func $ map (\f -> doMatch' st f []) (tails string)
3: where
4: doMatch' state [] soFar = doAccept machine st soFar
5: doMatch' state (s:str) soFar =
6: case findTransition machine s state of
7: Nothing -> doAccept machine state soFar
8: Just (from, to, val) -> case doMatch' to str (soFar ++ [s]) of
9: (False,_) -> case canAccept machine to of
10: True -> (True, soFar ++ [s])
11: False -> doMatch' to str (soFar ++ [s])
12: (True,res) -> (True,res)
This creates the list of matches and uses the passed in function to determine how to filter to either the shortest or longest match.
For short or long matches I pass in one of these two functions:
1: -- Get the shortest match
2: shortest matches = case filter (\s->fst s) (sort matches) of
3: [] -> (False,"")
4: ms -> head ms
5:
6: -- Get the longest match
7: longest matches = last.sort $ matches
I created aliases for the functions to make it more handy:
1: (=~) = greedyMatch
2: (=~?) = shortMatch
And then the final result:
1: *SimpleRegex> "hiphiphiphorray" =~? "hip(hip)*"
2: (True,"hip")
3:
4: *SimpleRegex> "hiphiphiphorray" =~ "hip(hip)*"
5: (True,"hiphiphip")
I attached a zip of all the files for this project.
Enjoy!
-
The third module in the simple regular expression parser is called: NFAtoDFA. Which as you might have guessed, takes the NFA that resulted from the first module and converts it into a DFA. The structure that the DFA uses is the same that the NFA uses since they are both finite state machines.
Converting an NFA to a DFA requires mapping sets of nodes in the NFA to a single node in the DFA. Many nodes in a NFA will correspond to one node in the DFA. Making this change requires updating transitions to point to and from sets of nodes. To manage this transformation I create a state monad using the following context:
1: -- The state which we pass to build the DFA
2: data ConvertContext = ConvertContext { nfa :: FiniteMachine, 3: trans :: [Transition],
4: setMap :: Map.Map (Set Node) Integer,
5: setStack :: [Set Node],
6: begin :: Node,
7: accept :: Set Node,
8: nextNode :: Node
9: } deriving (Show, Eq)
10: type ConvertState a = State ConvertContext a
Most of the code in this module is just managing this context and updating it according to two operation:
- Epsilon Closure
- Set Move
These are explained in more detail in this article.
Basically, epsilon closure is the process of taking a set of initial nodes and returning a new set of all nodes you can traverse to purely on epsilon transitions. To help with this I created some smaller methods to build up to an epsilon closure.
First are a couple methods (findToNodes and closure):
1: closure trans value nodes oldSet = Set.union (findToNodes trans value nodes) oldSet
2:
3: -- Search the table of transitions to find all nodes you can reach given an initial set of nodes
4: findToNodes trans value fromNodes = foldr match Set.empty trans
5: where
6: match (from, to, val) nodes
7: | (from == fromNodes) && (val == value) = Set.insert to nodes
8: | otherwise = nodes
findToNodes searches a transition table for all nodes which go from any node in fromNodes on value. It will builds up a set with all the to nodes that match.
closure wraps findToNodes to let us easily union together an initial set and the nodes we can reach from that set.
With this in hand we can write clearly a epsilon closure function:
1: -- Given an initial set of nodes, find the set of all nodes you can reach by taking
2: -- transitions on epsilon only
3: epsilonClosure trans nodes = foldUntilRepeat Set.union Set.empty $
4: iterate (Set.fold (closure trans epsilon) Set.empty) nodes
5:
6:
This function takes full advantage of the lazy nature of Haskell. It repeats the closure on epsilon over and over and streams its results into our function foldUntilRepeat. This method does what it says, it will fold the values that are streamed in until it sees the same value twice.
The set move is just combination of what you have already seen:
1: -- Given a starting set of nodes the set of all nodes that you can reach on a given value
2: -- This includes epislonClosure on the desitination nodes
3: moveClosure trans value nodes = epsilonClosure trans $
4: Set.fold (closure trans value) Set.empty nodes
5:
With these functions in hand, this module just becomes calling them and updating the context until we have no more nodes in the NFA to process.
In the next installment I will discuss using the output of this modules to match a regex against a string.
Also, once again the code is attached.
-
The first module in my simple regular expression parse is called RegexToNFA. This module exposes the types that make up a finite state machine and also the functions to convert a regular expression string into a finite state machine.
My structure for a FSM follows closely from the mathematical definition:
1: data FiniteMachine = FiniteMachine{ table :: [Transition], 2: alphabet :: Set Char,
3: start :: Node,
4: final :: Set Node
5:
6:
7: -- NFA node