Hi I am trying to create a model in which I need an EntitySet of a generic type:

[Field] public EntitySet<Generic<RecordA>> RecordsA { get; set; }

when I try to build the domain I have this error:

'MyModel.Generic<MyModel.RecordA>' is not registered in the model

The domain is build correctly only if I have somewhere in the model a field of type Generic<RecordA>.

How can I resolve this issue?

thanks.

asked Jan 29 '13 at 06:51

sluzardi's gravatar image

sluzardi
5444


One Answer:

Hello Stefano,

EntitySet<> could be created only with Entity/IEntity derived types. So your Generic<> should be an entity.

DataObjects.Net supports generic entities in two possible scenarios:

1) It could use any generic type if you register all required generic instances in DomainConfiguration explicitly:

var configuration = DomainConfiguration.Load("MyDomain");
configuration.Types.Register(typeof (MyGeneric<MyOtherType>));
configuration.Types.Register(typeof (MyGeneric<MyAnotherType>));

In this sample you could use MyGeneric<MyOtherType> and MyGeneric<MyAnotherType> as a regular Entity. However no other MyGeneric<> instantiations will work.

2) If generic type satisfies some conditions DataObjects.Net applies so called automatic generic instantiation. To have it working all your generic parameters should have a type constraint: either Entity/IEntity or their derived types/interfaces. Let me show you some example:

// This interface marks all entities suitable for audit
public interface IAuditable : IEntity
{
}

// This is audit record.
// Any auditable entities would be automatically substituted as TEntity
// and corresponding generic instance would be available in domain.
[HierarchyRoot]
public class AuditRecord<TEntity> : Entity 
  where TEntity : Entity, IAuditable
{
  [Key, Field]
  public long Id { get; private set; }

  [Field]
  public TEntity Target { get; private set; }
}

// This entity does not match AuditRecord<> constraints.
// AuditRecord<EntityA> would not be created.
[HierarchyRoot]
public class EntityA : Entity 
{
  [Key, Field]
  public long Id { get; private set; }
}

// This entity matches AuditRecord<> constraints.
// AuditRecord<EntityB> would be created.
[HierarchyRoot]
public class EntityB : Entity, IAuditable
{
  [Key, Field]
  public long Id { get; private set; }
}

// This entity also matches AuditRecord<> constraints,
// however AuditRecord<EntityC> would not be created because
// DataObjects.Net instantiates generics only for the most base type
// in the hierarchy that matches constraint. In our sample it is EntityB.
[HierarchyRoot]
public class EntityC : EntityB 
{
  [Key, Field]
  public long Id { get; private set; }
}

answered Jan 29 '13 at 07:27

Denis%20Krjuchkov's gravatar image

Denis Krjuchkov
179325

edited Jan 29 '13 at 07:33

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