Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoDb C# driver: mapping events to read database in cqrs solution
    primarykey
    data
    text
    <p>We're using MongoDb as a datasource for our application, which is built using cqrs and event sourcing. The problem that we faced today is what is the best way to implement mapping (denormalization) of events to read database. For example, we have a user MongoDb collection which contains all info about user.We have event like this:</p> <pre><code>[Serializable] public class PasswordChangedEvent : DomainEvent { private string _hashedPassword; private string _salt; public PasswordChangedEvent() { } public PasswordChangedEvent(string hashedPassword, string salt, DateTime createdDate) :base(createdDate) { HashedPassword = hashedPassword; Salt = salt; } public string HashedPassword { private set { _hashedPassword = value; } get { return _hashedPassword; } } public string Salt { private set { _salt = value; } get { return _salt; } } } </code></pre> <p>And read DTO like </p> <pre><code>public class User : BaseReportDataObject { public string Name { get; set; } public string Email { get; set; } public string Gender { get; set; } public DateTime? BirthDate { get; set; } public string HashedPassword { get; set; } public string Salt { get; set; } public string RestoreHash { get; set; } public string OpenIdIdentifyer { get; set; } } </code></pre> <p>Our current solution for updating documents with events goes like this: we have some mapping code for our events (BsonClassMap.RegisterClassMap etc.) and code for update:</p> <pre><code>MongoCollection.Update(Query&lt;PasswordChangedEvent&gt;.EQ(ev =&gt; ev.AggregateId, evnt.AggregateId), Update&lt;PasswordChangedEvent&gt; .Set(ev =&gt; ev.HashedPassword, evnt.HashedPassword) .Set(ev =&gt; ev.Salt, evnt.Salt)); </code></pre> <p>The code looks little ugly and redundant to me: with all that lambda stuff we still need to provide property values explicitly. Another way is to replace PasswordChangedEvent with User dto, so we do not need event mapping anymore:</p> <pre><code>MongoCollection.Update(Query&lt;ReadDto.User&gt;.EQ(u =&gt; u.Id, evnt.AggregateId), Update&lt;ReadDto.User&gt;.Set(u =&gt; u.HashedPassword, evnt.HashedPassword)); </code></pre> <p>So the question again: is there any better way to do such a type of mapping? Two types of objects (Events and DTO) mapped to the same mongo db collection. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload