-
Couldn't load subscription status.
- Fork 14
Built in type resolvers
Some of AtomEventStore's built-in content serializers use an ITypeResolver instance in order to figure out how to deserialize XML into objects.
While you can implement ITypeResolver yourself, AtomEventStore presents a few alternatives.
Apart from writing a custom implementation of ITypeResolver, the most low-level approach is to create an instance of the TypeResolutionTable class.
This example demonstrates how to create a TypeResolutionTable for three event classes:
var resolver =
new TypeResolutionTable(
new TypeResolutionEntry(
"urn:grean:samples:user-on-boarding",
"user-created",
typeof(UserCreated)),
new TypeResolutionEntry(
"urn:grean:samples:user-on-boarding",
"email-verified",
typeof(EmailVerified)),
new TypeResolutionEntry(
"urn:grean:samples:user-on-boarding",
"email-changed",
typeof(EmailChanged)));
var serializer = new DataContractContentSerializer(resolver);(All the code in this example is available in the unit test library in the AtomEventStore code base.)
Notice that in all three cases, the XML namespace in use is "urn:grean:samples:user-on-boarding", and that e.g. the local name "user-created" is mapped to the UserCreated class, the local name "email-verified" is mapped to the EmailVerified class, etc.
Both DataContractContentSerializer and XmlContentSerializer classes come with a convenience method called CreateTypeResolver, which scans an assembly for appropriately annotated event classes and produces an ITypeResolver instance from the associated metadata.
-
DataContractContentSerializer.CreateTypeResolverlooks for classes with the[DataContract]attribute. -
XmlContentSerializer.CreateTypeResolverlooks for classes with the[XmlRoot]attribute.
In both cases, the method scans the assembly for all public types with the appropriate attributes and pulls the XML namespace and local names off that metadata. It then returns an ITypeResolver instance based on that metadata.
This example demonstrates how to use DataContractContentSerializer.CreateTypeResolver to scan an assembly containing all the desired event classes:
var resolver =
DataContractContentSerializer.CreateTypeResolver(
typeof(UserCreated).Assembly);
var serializer = new DataContractContentSerializer(resolver);(All the code in this example is available in the unit test library in the AtomEventStore code base.)
Notice that this example uses an example class (UserCreated) in order to get a type-safe reference to the assembly that contains all the event classes. The CreateTypeResolver method scans that assembly and returns an appropriate ITypeResolver instance, which can then be passed to the DataContractContentSerializer constructor.
Both DataContractContentSerializer and XmlContentSerializer classes also come with a convenience method called Scan, which uses CreateTypeResolver in order to create create the desired ITypeResolver instance in a single method call.
This example demonstrates how to use DataContractContentSerializer.Scan to scan an assembly containing all the desired event classes:
var serializer = DataContractContentSerializer.Scan(
typeof(UserCreated).Assembly);(All the code in this example is available in the unit test library in the AtomEventStore code base.)
Notice that this example uses an example class (UserCreated) in order to get a type-safe reference to the assembly that contains all the event classes. The Scan method scans that assembly and returns an appropriate ITypeResolver instance.