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 Krjuchkov
1793●2●5