Image representing the Articles category

Prefixing ID columns with the table name in Entity Framework

Published 10 years ago

I recently got a question about the database configuration part of my article series on N-Layer design in ASP.NET. In my model, all classes inherit DomainEntity<T> that defines a primary key called Id. This in turn means all primary key columns in the database are called Id as well. The reader wanted to know if it was possible to prefix that column with the entity / table name. So, the Id column in the People table would be called PersonId, the Id in EmailAddress would be called EmailAddressId and so on. This has always been possible (and easy to do) for separate entities, but EF 6's custom conventions makes it a lot easier to do it for the entire model.

Read on ...
Image representing the Articles category

Fixing Issues with HTML Help Workshop

Published 14 years ago

I am working on an application that ships with documentation in the form of a Windows Compiled Help file. To build the help file I use Sandcastle and the excellent Sandcastle Help File Builder. Building the help file is part of a continuous integration plan so the help file is always up to date.

Read on ...
Image representing the Articles category

Automatically Generating Class Diagrams from a Type Using Reflection

Published 15 years ago

Before I launched my web site www.dotnetattributes.com in early 2009 I generated most of the content automatically using reflection and web look ups. I extracted all the various attributes from .NET assemblies using the Managed GAC API Wrappers and I extracted a lot of class details and documentation from the MSDN site using the HttpWebRequest class. One thing I hadn't automated at first and was planning to do manually using Visual Studio was generating class diagrams. However, soon after the first full import attempt I ended up with more than 1,200 attributes so manually creating the diagrams suddenly did not seem so attractive anymore.

Read on ...
Image representing the Articles category

Fun With Extension Methods - Extending Object Part 1

Published 16 years ago

Some time ago I was showing a colleague how to enhance an object's Design Time capabilities (or actually Debug Time) by adding a DebuggerDisplayAttribute. I blogged about this attribute earlier, so I won't go into it again now. But what I do want to talk about is the way the attribute gets its data.

Read on ...
Image representing the Articles category

Fun With Extension Methods - Extending IDataRecord Part 2

Published 16 years ago

It's not uncommon that you have a method that accepts an object that implements the IDataRecord or IDataReader interface. It's also not uncommon that you cannot (fully) control the query that drives the IDataRecord. It could be the result of a SELECT * operation (bad idea) or it could be the results of a Stored Procedure for example. Especially in the latter case, it can be useful if you can check whether the IDataRecord contains a specific field. For example, you may want to check if the IDataRecord has a field called LastModified before you try to read and store this field in a local DateTime variable.

Unfortunately, the IDataRecord and IDataReader do not implement this behavior directly. However, it's easy to add with a simple extension method.

Read on ...
Image representing the Articles category

Fun With Extension Methods - Extending IDataRecord

Published 16 years ago

For some reason, the IDataRecord interface and classes that implement it (DbDataReader, SqlDataReader and so on) only have Get* methods that accept the zero-based column index of a column in the result set. They don't allow you to get data by specifying a column name. As an example, consider the private FillDataRecord method to fill an e-mail address, as discussed in my article series about N-Layer development.

private static EmailAddress FillDataRecord(IDataRecord myDataRecord)
{
  EmailAddress myEmailAddress = new EmailAddress();
  myEmailAddress.Id = 
          myDataRecord.GetInt32(myDataRecord.GetOrdinal("Id"));
  myEmailAddress.Email = 
          myDataRecord.GetString(myDataRecord.GetOrdinal("Email"));
  // More fields here
  return myEmailAddress;
}		

The GetInt32 and GetString methods only have a single overload: one that accepts the zero-based index of the column. To satisfy these method signatures and make your code more readable at the same time, you can use GetOrdinal as shown in the previous example. Based on the column's name, GetOrdinal returns the column index. So, given the fact that the Id column is the first in the result set and Email the second, the previous piece of code equates to this:

myEmailAddress.Id = myDataRecord.GetInt32(0);
myEmailAddress.Email = myDataRecord.GetString(1);

Clearly, this is much more difficult to read and maintain than the previous example as you need to know the column indices and you should take great care not to mess with the column order in your select statements.

With a few extension methods, you can have the short syntax of the latter example, but still have readable code as the first example.

Read on ...
Image representing the Articles category

Fun With Extension Methods - Extending String to Provide a Better Split Method

Published 16 years ago

Do you feel that the standard Split implementation of the String class is a bit awkward to use? Do you keep forgetting you have to declare a char array for the separator? And do you often need to split on multiple characters, like \r\n to split on a line break? In that case, read on. A simple extension method might fix that for you.

Read on ...
Image representing the Articles category

Fun With Extension Methods - Extending Response.Redirect

Published 17 years ago

How often have you written code that redirects to another page and passes some local variables? You probably use string.Format to make your code easier to read. E.g.:

int categoryId;
// Code to assign a value to categoryId here
Response.Redirect(string.Format("SomePage.aspx?CatId={0}", categoryId.ToString()));

Have you ever wished there was an overload of the Redirect method that allowed you to omit the call to string.Format and simply let you write something like this:

int categoryId;
// Code to assign a value to categoryId here
Response.Redirect("SomePage.aspx?CatId={0}", categoryId.ToString());

This code makes it much easier to redirect to a page with a number of variables in the Query String. All you need to do is provide a composite format string as the new URL and a bunch of values that are used instead of the placeholders.

With Extension Methods - that come with .NET 3.5 - you can easily accomplish this yourself.

Read on ...
Image representing the Articles category

Building a Simple Rating Control for ASP.NET 2.0

Published 17 years ago

UPDATE: 16-7-2008 I have updated the control and wrote a new article about it. This new release fixes a few bugs and comes with a better sample web site to try out the control. Check out the new article here.



To show visitors of your site how other visitors feel about the content you're presenting on your site, it's good idea to let your visitors rate your content.

Many web sites take this approach. For example, Amazon uses a 5 star rating approach to rate the articles they're selling. Microsoft's MSDN site uses a nine-bar graph to display the quality of their (technical) articles. My own site uses a five-bar graph to let users rate an article (in the left hand column). Since there are many uses for a rating feature, it makes sense to encapsulate the rating mechanism in a custom ASP.NET Server Control. This article shows you how to build such a control.

Read on ...
Image representing the Articles category

Retrieving the Parameter Marker Format When Using Generic Database Factories

Published 18 years ago

With .NET 2, Microsoft introduced something called DbProviderFactories that allows you to work with databases in a generic fashion. That is, you don't create strongly typed objects like a SqlConnection or an OleDbConnection at design-time, but defer the decision what object to create till run-time. This way, you can write code that works against a number of different databases, like SQL Server, Oracle and OleDb databases like Microsoft Access.

One of the big differences between how these databases operate is the way you must name the parameters you send to a stored procedure or query. For example, SQL Server uses the @ symbol (@userName) and even allows you to leave out a prefix altogether. Microsoft Access in turn uses a question mark without a specific name while Oracle uses a colon as the prefix. Now, how do you deal with these differences when you don't know what database you're working with till run-time?

Read on ...

Mobile: False

Crawler: True

I: False