Can someone provide a sample of creating a custom key generator, I am using DO.net 4.4.

I have tried adding the

[KeyGenerator(typeof(MyGenerator))]

This throws an exception during perform: "[KeyGenerator] attribute on type 'VirtualMedia' requires Name to be set"

So I tried this:

[KeyGenerator(typeof(MyGenerator), Name="foo")]

This throws Service with name "foo" of type 'MyGenerator' is not available.

The sample in the documentation is apparently outdated as classes like KeyProviderInfo are not even in the framework.

asked Apr 24 '12 at 15:37

MikeBenson's gravatar image

MikeBenson
29448


One Answer:

I figure this out, the documentation really needs updated:

Here is the sample demonstrating how to do it:

[KeyGenerator(typeof(MyGenerator), Name = "foo")]
[HierarchyRoot(InheritanceSchema.SingleTable)]
public abstract class MyBaseClass : Xtensive.Orm.Entity
{
}

[Service(typeof(MyGenerator), Name = "foo")]
public class MyGenerator : KeyGenerator<Guid>
{
    public override Xtensive.Tuples.Tuple TryGenerateKey(bool temporaryKey)
    {
        return Xtensive.Tuples.Tuple.Create(Guid.NewGuid());
    }
}

You will also need to register the "MyGenerator" type in the domain configuration.

answered Apr 24 '12 at 16:19

MikeBenson's gravatar image

MikeBenson
29448

Hello Mike, sorry for inconvenience. You are right this is the correct way to create custom key generator. It allows you to create completely customized key generator. The other option is to use standard DO.Net approach with XXX-Generator tables.

By default one table is used for each key type (int or long). You can however declare your own generator table named for example foo-Generator by creating class that inherits CachingKeyGenerator<t> and attributing it the same way.

(Apr 25 '12 at 04:39) Denis Krjuchkov Denis%20Krjuchkov's gravatar image

Ok, documentation still not up-to-date because I just ran into the same thing unnecessarily... :/

(Aug 01 '12 at 08:30) Jos Jos's gravatar image

Yes, sorry for that. We are working on this.

(Aug 03 '12 at 07:02) Dmitri Maximov Dmitri%20Maximov's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA