Posts

Silverlight Context Menu

Silverlight 4. I can't believe how many times I've had to search for this one and remembered what it was. So, it gets added to my permanent list of memories. The only real trick to get the right namespace is to drag a MenuItem onto the page, then delete it and add this code inside whatever you want to have a context menu. < toolkit : ContextMenuService.ContextMenu >   < toolkit : ContextMenu >     < toolkit : MenuItem Header ="Add child"/>     < toolkit : MenuItem Header ="Remove" />   < toolkit : ContextMenu > < toolkit : ContextMenuService.ContextMenu >

Sorting the results of a MS SQL table-valued function

For whatever reason, MS SQL table-valued functions don't get the respect they deserve. Linq to entities pretends that they don't exist at all. I mean, seriously, how hard would it be to allow you to import them in the same way that you can import stored procedures? Anyway, as a mechanism for abstracting your database schema, I like them. They are pre-compiled, strongly typed, and results cached, so they can get very fast. One thing they don't do well is return sorted values. The following code won't work well when contained in a table-valued function: SELECT * FROM Table1 ORDER BY Column1 This requires us to resort to doing something like this: SELECT TOP 100 PERCENT * FROM Table1 ORDER BY Column1 And even that result can be ignored sometimes. Consider a table that consists of a key, HierarchyId , and a description. We want to unwind the hierarchyId so that we have key, parent key, and description. This is necessary if we are working with the hierarchy outside of...

An infinite date table

Here is the creation script for a neat function that will generate a table full of every date in the given range. -- ============================================= -- Author: Aaron D. Wells -- Create date: 9/23/2010 -- Description: Get a list of dates in a range -- ============================================= CREATE FUNCTION fn_Dates (@fromDate date, @toDate date) RETURNS @dateTable TABLE( [Date] Date NOT NULL, FiscalYear int NOT NULL, FiscalMonth int NOT NULL ) AS BEGIN WITH CTE_DatesTable([date]) AS ( SELECT @fromDate AS [date] UNION ALL SELECT DATEADD(dd, 1, [date]) FROM CTE_DatesTable WHERE DATEADD(dd, 1, [date]) 9 THEN YEAR(date) + 1 ELSE YEAR(date) END FiscalYear, CASE WHEN MONTH(date) > 9 THEN MONTH(date) - 9 ELSE MONTH(date) + 3 END FiscalMonth FROM CTE_DatesTable OPTION (MAXRECURSION 0) RETURN END Using the function is pretty simple, just give it a starting and ending date. By the w...

Generic Dynamic Class Factory (.net)

This code provides a simple way to dynamically load all the assemblies in a directory (by default the bin directory in your project) using System.Text; using System.Reflection; using System.IO; public class DynamicFactory { List allAssemblies = new List (); public DynamicFactory() { } public void LoadAssemblies() { Assembly thisAssembly = Assembly.GetExecutingAssembly(); Uri thisUri = new Uri(thisAssembly.CodeBase); string path = Path.GetDirectoryName(thisUri.LocalPath); LoadAssemblies(path); } public void LoadAssemblies(string path) { allAssemblies.Clear(); foreach (string dll in Directory.GetFiles(path, "*.dll")) { allAssemblies.Add(Assembly.LoadFile(dll)); } } public IEnumerable EnumerateTypes() { foreach (var assembly in allAssemblies) foreach (Type type in assembly.GetExportedTypes()) if (typeof(T).IsAssignableFrom(type)) yield return ...

Software Developers Do Not Build Widgets...

This post will be quite different from my normal posts. It is not a quick how-to, or a design pattern, but a rebuttal... I was recently reading the introduction to a book on enterprise architecture. And the author tried to make the point that software development is a cottage industry because the "widgets" we create (software) does not have the associated factories, structures, and automation that making cars or cookies has. He points out that the auto industry spends 80% of its time designing the factory and only 20% designing the cars. At the end of building a particular type of automobile, the factory line is thrown away (or massively retooled) for the next model of car. That author, and almost every other like-minded author or speaker I've ever heard of is building a case for their "software creation" automation tools, process, or methodology (shudder). And their analogy is completely wrong. The truth is, we don't build cars or cookies. Every custom deve...

SQL random number column

Here's a quickie. If you need to have a new random number on a column, this works really well. rand(cast(cast(NEWID() as varbinary) as int)) number Of course NEWID generates a random guid. Casting it as a varbinary first, then as an int gives you a random number which can then be used as a seed for the random number generator.

Dynamically Generated List of Dates Using Common Table Expressions (CTE)

Here is a nice use for Common Table Expressions (CTEs) to generate a dates table or other list of stuff. I won't take credit for this technique since I saw it someplace else (but can't remember where). Of course my blog is for stuff I want to remember, so thanks to whoever you are... declare @loops int = 1000 declare @StartDate date = '1/1/2000'; WITH CountTable( RowNumber, [Month], [Year], FirstDay ) AS ( SELECT 1 RowNumber, MONTH(@StartDate) [Month], YEAR(@StartDate) [Year], -- compute the first day of the month from whatever day was provided DATEADD(dd,-(DAY(@StartDate)-1),@StartDate) FirstDay UNION ALL SELECT RowNumber + 1 RowNumber, MONTH(DATEADD(MONTH, 1, FirstDay)), YEAR(DATEADD(MONTH, 1, FirstDay)), DATEADD(MONTH, 1, FirstDay) FirstDay FROM CountTable t WHERE RowNumber The results look like this:  1 1 2000 2000-01-01 2 2 2000 2000-02-01 3 3 2000 2000-03-01 4 4 2000 2000-04-01 5 5 2000 2000-05-01 6 6 2000 2000-06-01 7 ...