This forum is used by the Opf3 developers to announce new features or to discuss with you about ideas on new features.
 | |
| | The next version includes also the possibility to create custom mapping engines. It means that you don't need to put attributes on all persistent objects and members. You could create a custom mapping provider that creates the mapping by parsing an XML file or any other logic.
The following example shows how to implement such a provider:
public class CustomTypMappingProvider : ITypeMappingProvider { // Invoked each time the persistent attribute for a type is required. public PersistentAttribute GetPersistentAttribute(Type type) { // Upper the name and add an s if not already present. // Entities in our database are always in plurar. string name = type.Name.ToUpper(); if (!name.EndsWith("S")) name += "S";
// Returns the persistent attribute with the entity name. return new PersistentAttribute(name); }
// Invoked each time the field attribute for a member is required. public FieldAttribute GetMemberFieldAttribute(Type type, MemberInfo member) { // If the member is no property, return null (is not mapped then). if (!(member is PropertyInfo)) return null;
// Upper the name of the field attribute. FieldAttribute attribute = new FieldAttribute(type.Name.ToUpper()); // If the name of the member is ID, it is an identifier. if (member.Name.ToUpper() == "ID") attribute.Identifier = true;
// Return the attribute. return attribute; }
// Invoked each time the relation attribute for a member is required. public RelationAttribute GetMemberRelationAttribute(Type type, MemberInfo member, Type relatedType) { // Relations are not supported by the mapping provider. We always // return null. return null; } }
The example shows a simple implementation of the ITypeMappingProvider. It uses the names of the members and persistent type to create the mapping.
The class could also parse an XML file and create the mapping on the information found in that file. Or it could parse the database schema and create the mapping depending on that schema.
Greetings Christian
|
 | |
| | VERY nice - I would very much like to see some kind of tutorial showing how to map classes/tables though XML - can it be done the way f.e. NHibernate does it?
The current documentation on this interface is a bit "thin".
|
 | |
| | The interface has only one purpose: Instead of looking on the the persistent type's members for the PersistentAttribute, FieldAttribute or RelationAttribute, Opf3 queries the interface to get those attributes.
Let's take the example of creating an XML mapping provider. You have to implement the ITypeMappingProvider interface in your own mapping provider class. Let's call it XmlMappingProvider.
Now the TypeMapping for a persistent class type (let's call it Foo) is required.
Opf3 will ask you for the persistent attribute (by calling the GetPersistentAttribute method). You have to parse the XML for the given type (in this case "Foo") and return an instance of the PersistentAttribute (holding the name of the entity in the database).
After that for each property you get then a GetMemberFieldAttribute method call. You can return null (the property is not mapped) or an instance of the FieldAttribute (with the FieldName and other properties of the attribute set), if the property is mapped.
The same happens with relations (here for each ObjectSetHolder, ObjectHolder or ObjectListHolder in the persistent class type). You should return here an instance of the RelationAttribute (with the properties of the attribute set) if a relation is defined or null (no relation is defined on the given holder).
Christian
|