Friday, 26 February 2010

ORM – peril of the first time users

My team has recently got to grips with Linq-to-Sql and in the whole its all gone smoothly but there was one issue that the team had to overcome.

Our team is normally very good about data access, up to this point we have used stored procedures for all data access but only when needed. However, with Linq-to-sql that started to go out the window with calls to the database being made all over the code.

One of the worst examples that killed performance was calling the database to retrieve data for each record that is being displayed in a grid, now this is something that normally the team would have never done.

We had been using linq-to-objects for a while and so were used to working with objects in memory with no performance problems.  Now linq-to-sql syntax looks like linq-to-objects and you can believe you are simply calling a method on an object rather than the database and it is this reason that I believe we had the a problem, simply we forgot we were calling across the network to the database.

So the cautionary tale is this – when using an ORM don’t forget all your previous database knowledge and if you are not containing the data access to its own layer then be mindful of where you are using it.

Sunday, 14 February 2010

Asp.Net Dynamic Data – Data Type Part 2

In my previous post I listed the different DataType’s that dynamic data supports through its DataType attribute.

Like some of the previous attributes the DataType attribute supports multiple arguments, the difference between this attribute and the others with multiple arguments is you want to be looking to use the named parameters.

The available arguments are:

Argument Use
ErrorMessage Sets message to display if the control fails validation.
ErrorMessageResourceName Name of resource to use for displaying an error message. Often used to display a localized error message
ErrorMessageResourceType Used in conjunction with ErrorMessageResourceName to specify the type of resource

To use you simply specify the relevant named parameter and value to use:

[DataType(DateType.Date, ErrorMessage=”You must enter a valid date.”)]

And this should then display your error message when an invalid value is entered in the field but unfortunately this does not work.

It appears that there is a bug in the current implementation of Dynamic Data where it does not pick up the error message, or resource, that is specified  on the attribute.  It is possible to get Dynamic Data to use the attribute error message but that involves further code where you set the CompareValidator error message explicitly see this blog post by Rick Anderson for details.

Friday, 12 February 2010

Asp.Net Dynamic Data – Data Type Part 1

When dynamic data is building the columns to display it works out the field template to use from the data type of the field it is going to display.

The data type attribute allows you to override what dynamic data wants to use with a more specific data type.
The standard data types supported are:

Data Type Description
DateTime Represents an instant in time, expressed as a date and time of day.
Date Represents a date value
Time Represents a time value
Duration Represents a continuous time during which an object exists.
PhoneNumber Represents a phone number value
Currency Represents a currency value
Text Represents text that is displayed.
Html Represents an HTML file
MultilineText Represents multi-line text.
EmailAddress Represents an e-mail address.
Password Represent a password value.
Url Represents a URL value.

As well as those listed another type of custom is supported allowing you to extend this functionality further yourself.

To use the DataType attribute you add the attribute to the display meta data:
public object OrderDate {get; set;}

This will then change the way the OrderDate field, a DateTime field, so that only the date is shown


The image shows the order date field with the DataType attribute set and the RequiredDate field that doesn’t.

Thursday, 11 February 2010

Asp.Net Dynamic Data – UI Hint Part 2

Like the DisplayColumn attribute the UIHint supports multiple arguments allowing you to pass additional data to the field template.

The signature for the attribute is:

UIHInt[field template to use,Presentation Layer, Control parameters]

The arguments are:

Field template – covered in the last post

Presentation Layer – From what I can discover this is not currently used but the suggested values from intellisense of HTML, Silverlight, WPF or Winforms indicate the maybe its roll will change in the future.

Control Parameters – a param object array allowing you to pass as many additional parameters to the field template as you want but you need to pass key value pairs e.g. “argument1”,10

If you build your own custom field template then the control parameters may provide you a mechanism to provide additional data you need.  The only problem is that if you are using the attribute on a class the values will be hardcoded, which doesn’t allow for much flexibility.

Wednesday, 10 February 2010

Asp.Net Dynamic Data – UI Hint Part 1

The UIHint attribute allows you to tell Dynamic Data what field template you would like to use when working with the property.

Before I get ahead of myself lets briefly cover Field Templates.  A field template is what dynamic data uses to display the value for a data field.   Each field template is implemented as a normal Asp.Net user control which contains the basic UI definition and validation.  You can easily create your own custom field templates to provide the display or functionality you need.

What the UIHint attribute allows you to do is to specify the field template that you wish to use for a particular field/property on the table.

By doing this you override the default behaviour of dynamic data which normally decides the field template to use based on the data type of the field.

public object ShippedDate { get; set;}

Doing this ensures that when ShippedDate is displayed in a list, detail or edit it is the DateTime field template that will be used.

Tuesday, 9 February 2010

Asp.Net Dynamic Data – DisplayColumn Part 2

In my previous post I outlined how to use the DisplayColumn attribute to specify the column you want to use to display to represent the foreign key.
The attribute is smarter than that though in that it supports multiple arguments.
The signature for the attribute is:
DisplayColumn[“Column to display”, “Column to sort by”, Sort order]

This means you pick the column you want to show, the column to use when sorting and the sort order.

Most times you set the display and sort columns to the same value but the flexibility is there to pick another column if you need to.

Monday, 8 February 2010

Asp.Net Dynamic Data – DisplayColumn Part 1

This post is broken in 2 to cover the basic use of the attribute and then some of its options in the next post.

This attribute doesn’t appear on the meta information that controls display, but rather it is meta information on the partial class that matches the entity in the ORM.

When a table contains a foreign key to another table that is included in the ORM dynamic data normally uses the first string/text column that it finds in the related table to display to the user as link in both the list and detail view and a dropdown list in the edit view.

This attribute provides the ability for you to choose which column you want to use to display for the foreign key rather than the column dynamic data would choose.

To implement this attribute you first have to create the partial class that matches the entity in the ORM (see my post here about a gotcha with extending the data context) and then add the attribute:
partial class Customer

By doing this you will then see the Customer.ContactName instead of the Contact.CompanyName value displayed for the link in the list and detail views and used to populate the drop down list in the edit view.

Check here for the MSDN definition of the attribute.

Sunday, 7 February 2010

Asp.Net Dynamic Data - Description

So I’m feeling foolish now, this attribute is as simple as the previous 2, so because of this now assume all the attributes are simple unless I state otherwise.

The attribute allows you to enter text that will be displayed as a tooltip but only when the property is displayed in edit mode.

As per usual just decorate the property with the attribute and set its value:

[Description("Name of the company")]
public object CompanyName { get; set;}

Which in edit mode you then get:


Wednesday, 3 February 2010

Asp.Net Dynamic Data – Default Value

Ok perhaps I was wrong in my last post, Display Name may have to vie for the title of ‘simplest hint’ with Default Value.

This attribute allows you to specify the default value of a field when performing an insert of a new record. To use it simply decorate the property in the meta data class with the attribute and the value you wish to use:


public object Country { get; set;}

When entering a new record the screen now populates the country field with UK:


Monday, 1 February 2010

Asp.Net Dynamic Data – Display Name

This is perhaps the simplest hint to understand.

You put this in the meta data and tell dynamic data what you want displayed as a column header in the list/listdetail view and a field name in the detail & edit views.

The attribute is as easy to use as it sounds simply state the name you want to see:
[DisplayName("Company Name")]
public object CompanyName { get; set;}

And it appears in the UI: