Monday, September 07, 2009

Better Naming Logic

This is a non-breaking change and won’t effect your current codebase. Take a look at this SQL table, notice that the table name and column names are full of funky characters and such …

CREATE TABLE [dbo].[Crazy Names # 123 !(*]
(
    [PrimaryKey#] [int] IDENTITY(1,1) NOT NULL,
    [ Field With Spaces ] [nchar](10) NULL,
    [123FieldStartingWithANumber] [nchar](10) NULL,
    [Field with % Special # Characters , In It) ] [nchar](10) NULL,
    [A Field] [nchar](10) NULL,
    [A_Field] [nchar](10) NULL,
    [StandardFieldName] [nchar](10) NULL,
    [_wow_this_is_crazy.man ok] [nchar](10) NULL,
    [this#dba##should###be####fired#####.ok] [nchar](10) NULL,
    [_ALIAS_ME_#$%] [nchar](10) NULL,
)

This is what the resulting class looks like (in the past, we didn’t handle these so well)

[Serializable]
abstract public class esCrazyNames123 : esEntity
{
    public System.Int32? PrimaryKey;
    public System.String FieldWithSpaces;
    public System.String _123FieldStartingWithANumber;
    public System.String FieldWithSpecialCharactersInIt;
    public System.String AField;
    public System.String A_Field;
    public System.String StandardFieldName;
    public System.String _Wow_This_Is_CrazyManOk;
    public System.String ThisDbaShouldBeFiredOk;
    public System.String MyAlias;
}

You may have noticed the last field “MyAlias” is different. That is because we Aliased it to ensure Aliasing still works. Aliasing is done via the Metadata tabl. You might also notice that “underscores” were left in the column names. However, this is not the default. We merely check the “Preserve Underscores” checkbox because of the “A Field” and A_Field” columns so they wouldn’t conflict. “Preserve Underscores” if off by default. Take a look at this screen shot.

image

Notice on the “Naming Conventions” tab we have “Preserve Underscores” checked. Also, you might notice the “Trimlist” field is completely gone, it is not used anymore. But don’t worry, these changes will not effect your current objects.

Here are the rules basically for Property names in .NET


if(Char.IsLetterOrDigit(character))
{
    // this character is valid
}

The only exception to this rule is you can have an “_” underscores and the 1st character cannot be a number. That’s it. So now, no matter how things are created in your database EntitySpaces will always generate valid classes that compile out of the box, no need for aliasing unless you want to.

 

DynamicQuery Parse() Method

You can now capture the SQL Syntax a DynamicQuery would generate without having to execute it. This is done via the Parse() method like so:

CrazyNames123 c = new CrazyNames123();
c.Query.es.Top = 1;
c.Query.Where(c.Query.MyAlias == "asdf");
string str = c.Query.Parse();

This can come in handy when writing your queries …

EntitySpaces

From Mobile Devices to large scale enterprise solutions in need of serious transaction support, EntitySpaces can meet your needs. Whether you’re writing an ASP.NET application with Medium Trust requirements, a Silverlight application, a Mono application, or a Windows.Forms application, the EntitySpaces architecture is there for you. EntitySpaces is provider independent, which means that you can run the same binary code against any of the supported databases. EntitySpaces is available in both C# and VB.NET. EntitySpaces uses no reflection, no XML files, and sports a tiny foot print of less than 200k. Pound for pound, EntitySpaces is one tough, dependable .NET architecture.

EntitySpaces LLC
Persistence Layer and Business Objects for Microsoft .NET
http://www.entityspaces.net

Comments are closed.