Suggestions: AutoGenerating fields.

Here's something I'd like to throw out...

It would be nice to specify a property to build itself if its reference is null.

That is, let's say I'm creating a Location object that has an address:

Location l = new Location();

l.Address = new Address();

Generally I'm a lazy programmer, and I'd like to be able to assume that Address is always a non-null value, like such:

Location l = new Location();

l.Address.City = "city";

And in the getter something like this:

public Address Address

{

get {

if ( _Address == null ) {_Address = new Address(); _Address.ID = Guid.NewGuid(); }

return _Address;

}

}

Along such lines, would it also be possible to autogenerate Guids? I use Guids for all my IDs, and it would be nice to save an extra line of code whenever I create an entity (why should identity columns have preferential treatment? :) ).

Anyways, those are the thoughts I see offhand... This is some REALLY wicked work! I'm excited. :)

Mike

[1073 byte] By [Mike-EEE] at [2008-2-7]
# 1
Mike-EEE wrote:

Here's something I'd like to throw out...

It would be nice to specify a property to build itself if its reference is null.

That is, let's say I'm creating a Location object that has an address:

Location l = new Location();

l.Address = new Address();

Generally I'm a lazy programmer, and I'd like to be able to assume that Address is always a non-null value, like such:

Location l = new Location();

l.Address.City = "city";

And in the getter something like this:

public Address Address

{

get {

if ( _Address == null ) {_Address = new Address(); _Address.ID = Guid.NewGuid(); }

return _Address;

}

}


You could of course use the constructor for this. Though you can also use a code generator to generate the classes you're mapping and use a template which generates the code you have above into the destination classes.


Along such lines, would it also be possible to autogenerate Guids? I use Guids for all my IDs, and it would be nice to save an extra line of code whenever I create an entity (why should identity columns have preferential treatment? :) ).

Guid's for ID's are not a good idea in a lot of situations, unless you use the new SqlServer2005's NEWSEQUENTIALID() default value for your Guids. The problem is that Guid's trash an index, as they're not sequential. This means that if you insert a new row, it can't be appended to the data already in the table, as the PK is a clustered index (unless you define it as a non-clustered index).

Auto-gen Guid support is only feasable if the o/r mapper core supports NEWSEQUENTIALID(). I know of only one who does at the moment (see sig ;))
FransBouma-C#MVP at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 2
FYI: Do however realise that using NEWSEQUENTIALID() introduces a security risk, since it is possible "to guess the value" of the next generated GUID.
PaulGielens at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 3

Well, using a constructor would be my first choice. Unfortunately, a parameterless constructor is already generated in the code-gen, so providing one in the partial class generates an error...

As for NEWSEQUENTIALID... I'll give it a looksee... thanks!

Mike-EEE at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 4

DoFinalConstruction is the key to adding logic to the already generated paramaterless constructor. See this blog post: "The Joy of a Custom Built Object" or "What the heck is DoFinalConstruction()?"

Considering the topic of client generation of key values in general, I put together another blog post today that you might want to check out: Fire-and-Forget Key Values.

Please let me know if either of those is unclear or if they generate follow on questions. Thanks for the feedback!

- Danny

DanielSimmons-MSFT at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 5

Another really good post... please keep those coming! :)

Would be *very* nice to have Guid's in that auto-generating functionality. Apparently when NEO saves it to the database, it saves the GUID as empty... and there's no way of telling SQLServer2005 that Guid.Empty is actually "null." :)

Anyways... any line of code we can save is a worthy goal indeed.

Thanks!

Mike-EEE at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...

Visual Studio Orcas

Site Classified