ActiveRecord with pre-assigned PKeys..

7 views
Skip to first unread message

Michael J. Ryan

unread,
Nov 5, 2007, 6:13:09 PM11/5/07
to castle-pro...@googlegroups.com
I am using ActiveRecord in a project where some data will be replicated from
one site to another... I'd *LIKE* to be able to use the same classes to input
the data from a foreign site into the local dbms... I've been using the
Guid/GuidComb generators, but would like to not overwrite the PKey(GUID) if
it's already assigned...

Is this possible... iirc, it was overwriting the assigned pkey in the new
object... ie: have the generator check the value, if it isn't (new Guid())
(all 0 bits), then generate, otherwise leave it...

If I could get an example of a custom generator I'd be happy to use that...

--
Michael J. Ryan - tracker1(at)theroughnecks(dot)com - www.theroughnecks.net
icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

... TODO: re-create tagline file... stupid reinstall...

Ayende Rahien

unread,
Nov 5, 2007, 6:21:46 PM11/5/07
to castle-pro...@googlegroups.com
Possible, you need to use Create and Update explicitly, rather than Save()

Shawn Carr

unread,
Nov 5, 2007, 6:28:28 PM11/5/07
to castle-pro...@googlegroups.com

Michael this is how I have our projects setup. Seems to work well for us.

 

public abstract class EntityGuidBase<T> : ActiveRecordValidationBase<T>, IGuidEntity where T : class

{

        private Guid? _id = null;

 

        [PrimaryKey(Generator = PrimaryKeyType.Assigned)]

        public virtual Guid? Id

        {

            get { return _id; }

            set { _id = value; }

        }

 

        public override void Save()

        {

            if (_id.HasValue)

                UpdateAndFlush();

            else

            {

                _id = Guid.NewGuid();

                CreateAndFlush();

            }

        }

                …..

<br

josh robb

unread,
Nov 6, 2007, 6:55:31 AM11/6/07
to castle-pro...@googlegroups.com
On 11/5/07, Shawn Carr <shawn....@gmail.com> wrote:
> Michael this is how I have our projects setup. Seems to work well for us.
> public abstract class EntityGuidBase<T> : ActiveRecordValidationBase<T>,
> IGuidEntity where T : class

Wow - clever!

j.

Michael J. Ryan

unread,
Nov 6, 2007, 8:48:20 PM11/6/07
to castle-pro...@googlegroups.com
Nice... one more question... do you just inherit for EntityGuidBase then?
instead of ActiveRecordBase<T>?

On 11/5/2007 4:28 PM, Shawn Carr wrote:
> Michael this is how I have our projects setup. Seems to work well for us.
>
>
>
> public abstract class EntityGuidBase<T> : ActiveRecordValidationBase<T>,
> IGuidEntity where T : class
>
> {
>
> private Guid? _id = null;
>
>
>
> [PrimaryKey(Generator = PrimaryKeyType.Assigned)]
>
> public virtual Guid? Id
>
> {
>
> get { return _id; }
>
> set { _id = value; }
>
> }
>
>
>
> public override void Save()
>
> {
>
> if (_id.HasValue)
>
> UpdateAndFlush();
>
> else
>
> {
>
> _id = Guid.NewGuid();
>
> CreateAndFlush();
>
> }
>
> }
>
> …..
>
> }
>
>
>

> *From:* castle-pro...@googlegroups.com
> [mailto:castle-pro...@googlegroups.com] *On Behalf Of *Ayende Rahien
> *Sent:* Monday, November 05, 2007 6:22 PM
> *To:* castle-pro...@googlegroups.com
> *Subject:* Re: ActiveRecord with pre-assigned PKeys..


>
>
>
> Possible, you need to use Create and Update explicitly, rather than Save()
>

> On 11/6/07, *Michael J. Ryan* < tracker...@theroughnecks.com

> <mailto:tracker...@theroughnecks.com>> wrote:
>
>
> I am using ActiveRecord in a project where some data will be replicated
> from
> one site to another... I'd *LIKE* to be able to use the same classes to
> input
> the data from a foreign site into the local dbms... I've been using the
> Guid/GuidComb generators, but would like to not overwrite the PKey(GUID) if
> it's already assigned...
>
> Is this possible... iirc, it was overwriting the assigned pkey in the new
> object... ie: have the generator check the value, if it isn't (new Guid())
> (all 0 bits), then generate, otherwise leave it...
>
> If I could get an example of a custom generator I'd be happy to use that...
>
> --
> Michael J. Ryan - tracker1(at)theroughnecks(dot)com -

> www.theroughnecks.net <http://www.theroughnecks.net>

Michael J. Ryan

unread,
Nov 6, 2007, 9:21:26 PM11/6/07
to castle-pro...@googlegroups.com
So, with a pre-assigned Guid, but not having it in the local database
UpdateAndFlush will create the record? or should I track the "is new" state
based on the onload call?

josh robb

unread,
Nov 7, 2007, 6:55:28 AM11/7/07
to castle-pro...@googlegroups.com
On 11/7/07, Michael J. Ryan <tracker...@theroughnecks.com> wrote:
> So, with a pre-assigned Guid, but not having it in the local database
> UpdateAndFlush will create the record? or should I track the "is new" state
> based on the onload call?

Shawn's code will throw if you call Save when the entity is not
persisted in the database. (it will do an UPDATE - find 0 rows
affected and throw). You will need to come up with a convention that
will manage this your self. isNew might work depending on how it's
implemented. How will you know if it's new or already imported?

The key thing is that if the row already exists then you need to call
Update and if it's new you need to call Create.

And yes - he is creating his classes like:

[ActiveRecord]
public class MyEntity : EntityGuidBase<MyEntity> {}

j.

Shawn Carr

unread,
Nov 7, 2007, 10:23:37 AM11/7/07
to castle-pro...@googlegroups.com
In my code I say

public override void Save()
{
if (_id.HasValue)
UpdateAndFlush();
else
{
_id = Guid.NewGuid();
CreateAndFlush();
}
}

The key part of this method is the _id.HasValue. This is the isNew
functionality that you are referring to because when a new entity is created
its id is null until the save which at this point it figures this out and
assigns a new Guid to it. When it is loaded via the db the ID will have a
value so it know to do an update.

Whatever method you use you need some sort of isNew functionality so you can
call update or create directly.

Reply all
Reply to author
Forward
0 new messages