Posts

Showing posts from 2010

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]) <= @toDate) INSERT @dateTable SELECT [date], CASE WHEN MONTH(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 (M…

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 type;…

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 develo…

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 < @loops ) SELECT * FROM CountTable OPTION (maxrecursion 32767) The results look like this: 1 1 2000 2000-01-01 2 2 2000 2000-02-01 3 3 2000 2000-03-01 4…

Recursively change unix permissions on a directory

On my freenas server I sometimes need to change the permissions of files that were uploaded via FTP. Here is the command that can be issued to make those changes:

chmod -R 0777 "/mnt/Seagate650/FTP Root"

The quotes are important because my ftp root directory contains a space.

Load a dll into an AppDomain for reflection or execution and Unload it

.Net will allow you to easily load any dll you want to into the current AppDomain by simply calling Assembly.LoadFrom() with the path of the dll in question. However, this is only available for the current AppDomain, and as a result, the dll remains loaded for the duration of the AppDomain's lifetime.This may be ok, but if you want to simply load a dll, inspect it, and run some one-time operations on it, that seems a bit wasteful. In web development, it means you must shut down your web server to recompile the dll you're loading.So, the solution would seem to be to start up a new appDomain, load the dll in question, do your work, and then unload the appDomain.
Gentlemen, start your compilers...namespace Utilities { public class SomeClass{ public static void RegisterApplication( string appPath) { if (File.Exists(appPath)) { var domainSetup = new AppDomainSetup() { ApplicationBase = AppDomain.CurrentDomain .BaseDirectory, Privat…

CSRF (Cross-Site Request Forgery) Handling in MVC

Phil Haack's talk on asp.net reminded me of the importance of handling CSRF attacks. In MVC, this is simply handled with a one-two punch. In your posting form, you need to include the Html.AntiForgeryToken() in the form as follows:
The other thing is to add an [ValidateAntiForgeryToken] attribute to the targeted posting action in your controller as follows:
[ValidateAntiForgeryToken] [HttpPost] // or MVC 1.0 style [AcceptVerbs(HttpVerbs.Post)] public virtual ActionResult Delete(FormCollection form, int id) { // code here to delete stuff return View(); } Also don't forget that we want to make sure that any state-changing operations are always posts!
My new syntax highlighting is accomplished via a method outlined on Carter Cole's most excellent blog.