web 2.0

Dynamic Master Pages in MVC

I have been working on a new project where I wanted to introduce the concepts of themes. This means that end users will be allowed to change the look and feel of the web application from a configuration menu. In order to deliver this functionality I needed to be able to dynamically change the master page on the fly. My initial response was to use the Page PreInit event. The Page PreInit event is called write before the master page is assigned and therefore makes it a good candidate for dynamically assign the the master page. Unfortunately this is a Page level event. In order to use this solution I would need to override this method on every page. This would be a maintenance nightmare so I immediately eliminated this as a viable solution. Another solution is to use the View method to assign the master page. public ActionResult Index() { return View("Index", MyApp.Properties.Settings.Default.Theme); } Unfortunately, this solution is also a maintenance nightmare bec... [More]

The Missing LINQ - Beware of Generated Code

LINQ (Language Integrated Query) to SQL is a great tool because it allows developers to concentrate on business problems instead of worrying about writing SQL. Unfortunately, generated code typically comes with a catch. My general rule of thumb is that "I never trust any tools that have a wizard or generate code". After all, every time you release code into production you are putting your reputation on the line. Therefore, don't you think it is important to know what your code is really doing under the covers? Since I started getting involved with Database Administration about 3 years ago I have become extremely conscious of the SQL that my code generates. After all, most database performance problems stem from the fact that developers test on empty databases and everything seems to work fine until millions of records trickle into the system. Then ugly problems like missing indexes, functions in the where clause and poorly written queries bubble to the surface. The occurrenc... [More]

LINQ for JavaScript Lovers

LINQ was a major advancement in programming. Traditionally when you wanted to find a few items in a object collection you would write a loop and inspect the properties of each object and then return the items that matched. This was cumbersome and often resulted in the developer writing a lot of functions to return objects based on different types of searches. LINQ solved this problem completely because now you can use a SQL like syntax to return objects from a collection. LINQ comes in many different flavors, there is LINQ to SQL, LINQ to XML and LINQ to entities. However, today I ran across a project called jLinq, which is LINQ for JSON!. JSON stands for JavaScript Object Notation. JSON is simply a way to serialize object(s) to a string so they can be marshaled and eventually consumed by the client. For example here is the JSON string representing a Person: { "firstName": "John", "lastName": "Smith", "address": { "streetAddress": "21 2nd Street", ... [More]

Export to CSV using Reflection and IQueryable

Whenever I build an application that contains any kind of statistical data I commonly get the request to make all of the grids and/or tables to include a export to CSV capability. In my mind, there are two options to approach this functionality. Build a method that iterates over a control. For example in a grid you can iterate over the columns and rows to build the CSV file. Build a method that iterates over the data. In my opinion approach #2 is more flexible and lends itself better to code re-use. Tying export functionality to a specific control is really not a good idea unless you have a unique situation which requires it. In my MVC application I am using LINQ to SQL and I used the Repository pattern that you may have seen before if you have ever looked at the nerddinner.com source code. The majority of my data access code returns an IQueryable<T> result. Therefore my export function is an extension method on the IQueryable<T> type. In order to get the column... [More]

LINQ - How to Use Custom SQL Statements

I have a table where I stored encrypted passwords. In order to decrypt the passwords I wrote a UDF (user defined function) which utilizes SQL Server's DecryptByPassphrase function. By having the password encrypted in the table it prevents people from browsing the table and seeing the passwords in plain text. When you need to retrieve a password you have to issue a sql statement like this: select dbo.Decrypt(encryptedPassword) as Password from Password I control the security on the UDF's so only a select few people can execute them. In any case, I have an application which used LINQ to SQL to access the passwords. The problem I ran into was that the SQL statements generated by LINQ have no idea to how to encrypt/decrypt my passwords. Therefore I had to build some custom procedures for LINQ to use. The Insert Statement: The two important things to notice here are: I call the UDF to encrypt the password when I insert the record The identity column is an output parameter. Th... [More]