A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/GreanTech/AtomEventStore/wiki/DataContractContentSerializer below:

DataContractContentSerializer · GreanTech/AtomEventStore Wiki · GitHub

DataContractContentSerializer is one of AtomEventStore's built-in XML serializers. It's an Adapter of IContentSerializer over DataContractSerializer.

In order to use it, your event types must be annotated with the appropriate Data Contract attributes, such as the DataContract attribute.

Additionally, when creating a DataContractContentSerializer instance, you must supply an ITypeResolver, which enables AtomEventStore to determine the destination type when deserializing XML.

Consider a simple user sign-up process. A new user wishes to sign up for a service, so first provides a user name, password, and email address. Upon receiving the data, the system sends an email to the user, asking her or him to validate the address.

The events in the system may include:

[DataContract(Name = "user-created", Namespace = "urn:grean:samples:user-on-boarding")]
public class UserCreated
{
    [DataMember(Name = "user-id")]
    public Guid UserId { get; set; }

    [DataMember(Name = "user-name")]
    public string UserName { get; set; }

    [DataMember(Name = "password")]
    public string Password { get; set; }

    [DataMember(Name = "email")]
    public string Email { get; set; }
}

[DataContract(Name = "email-verified", Namespace = "urn:grean:samples:user-on-boarding")]
public class EmailVerified
{
    [DataMember(Name = "user-id")]
    public Guid UserId { get; set; }

    [DataMember(Name = "email")]
    public string Email { get; set; }
}

[DataContract(Name = "email-changed", Namespace = "urn:grean:samples:user-on-boarding")]
public class EmailChanged
{
    [DataMember(Name = "user-id")]
    public Guid UserId { get; set; }

    [DataMember(Name = "new-email")]
    public string NewEmail { get; set; }
}

(All the code in this example is available in the unit test library in the AtomEventStore code base.)

Notice that each of these event classes are annotated with the [DataContract] and [DataMember] attributes.

In order to create a DataContractContentSerializer, you can ask it to supply an implementation of ITypeResolver that scans an assembly for types annotated with [DataContract] attributes:

var serializer = new DataContractContentSerializer(
    DataContractContentSerializer.CreateTypeResolver(
        typeof(UserCreated).Assembly));

However, a simpler convenience method called Scan exists to do that:

var serializer = DataContractContentSerializer.Scan(
    typeof(UserCreated).Assembly);

The serializer can now be passed to readers or writers of event streams.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4