<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Stanko Culaja on Medium]]></title>
        <description><![CDATA[Stories by Stanko Culaja on Medium]]></description>
        <link>https://medium.com/@culaja?source=rss-624a1acf1365------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*KgGyCdcSzSiyLlIG.jpg</url>
            <title>Stories by Stanko Culaja on Medium</title>
            <link>https://medium.com/@culaja?source=rss-624a1acf1365------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 05:22:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@culaja/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[To be or not to be? Maybe.]]></title>
            <link>https://medium.com/kolotree-blog/to-be-or-not-to-be-maybe-d09f32450972?source=rss-624a1acf1365------2</link>
            <guid isPermaLink="false">https://medium.com/p/d09f32450972</guid>
            <category><![CDATA[orm]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[ddd]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Stanko Culaja]]></dc:creator>
            <pubDate>Mon, 13 Jan 2020 15:17:44 GMT</pubDate>
            <atom:updated>2020-01-13T15:48:51.311Z</atom:updated>
            <content:encoded><![CDATA[<p>Yes, you are right, too much time has passed from our previous blog post. We will try to provide new content regularly, while keeping the quality bar high. The aim of this blog post is to show how we can use optional values (Maybe) in context of Domain Driven Design and store our model in Mongo DB storage without affecting domain model with various mappings. We will also describe the iterative process in which we have chosen this approach.</p><p>Working on one e-commerce system recently, we wanted to put functional approach in use along with the DDD. We had experience in some functional languages like Scala, but this project demanded .NET technology stack, so we found great inspiration in works by Eric Evans and Vladimir Khorikov. As you probably know Eric has written <a href="https://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215">The Big Blue Book</a> on DDD and Vladimir has two great courses on Pluralsight: <a href="https://www.pluralsight.com/courses/domain-driven-design-in-practice">Domain Driven Design in Practice</a> and <a href="https://www.pluralsight.com/courses/csharp-applying-functional-principles">Applying functional principles in C#</a>.</p><p>OK, before the project kick-off, we needed to have a discussion about system architecture. Among other things, we wanted to keep our code clean, and one of the ways to do this is to get rid of the null values and null checking. Our idea was to use <a href="https://github.com/vkhorikov/CSharpFunctionalExtensions">library </a>created by Vladimir, which provides functional extensions to C#. One of the things you can find there is the <a href="https://github.com/vkhorikov/CSharpFunctionalExtensions/blob/master/CSharpFunctionalExtensions/Maybe/Maybe.cs">Maybe</a> class (struct to be more precise). It is a simple wrapper that wraps optional values. It has direct analogy with the Option class in Scala (which we described in our blog <a href="https://medium.com/kolotree-blog/monads-here-and-back-again-10f2fa4e869f">post</a>). Since this is Web based application, Maybe really comes in handy while going from application layer (Web API Controllers in this case) into domain layer (Domain service). Looking at the Maybe class, we can see that there is an implicit conversion from some type T to Maybe.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9bc2e17c3c7483cdc6232887b6127710/href">https://medium.com/media/9bc2e17c3c7483cdc6232887b6127710/href</a></iframe><p>What this actually means is that if a method in a service expects for example Maybe, and we pass in null, implicit conversion will convert this null to Maybe.None. On the other hand Maybe is declared as a struct:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/324a6f6a4d0b602f0589cab9b88ce4a1/href">https://medium.com/media/324a6f6a4d0b602f0589cab9b88ce4a1/href</a></iframe><p>Since struct is a value type in .NET, it cannot be null in any case :). Using this approach we are trying to mitigate the <a href="https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare">Billion Dollar Mistake</a>.</p><p>Our idea was to eliminate nulls totally from our projects, and in order to ensure this, we used <a href="https://github.com/Fody/NullGuard">NullGuard</a>, a <a href="https://github.com/Fody/Fody/wiki">Fody</a> add-in. NullGuard utilizes Fody to insert null checking in the generated MSIL (Microsoft intermediate language). It is up to a user is to define what they want to check for nulls, – arguments to methods, return values, out values, properties, etc. For Release builds, NullGuard will weave code that throws ArgumentNullException. For Debug builds, NullGuard weaves Debug.Assert. But this can be changed easily through configuration and for more details please check the link provided above.</p><p>Let’s get back to our project. Another important decision we have made was to use MSSQL server. Among two choices: MS Entity Framework and NHibernate, we settled with the latter one as our default ORM. You can find really good article on comparison of these two ORMs <a href="http://enterprisecraftsmanship.com/2014/11/29/entity-framework-6-7-vs-nhibernate-4-ddd-perspective/">here </a>from DDD perspective. But we made one promise to ourselves, which was to create repositories in a such a way, so we can later easily switch from NHibernate to something else should a need arise. And, as it turned out later on, this was very important, as you will see.</p><p>After project kick-off, everything was going fine until we stumbled upon the first issue. We had to map Maybe wrapper on some value type or string using NHibernate <a href="https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping">Fluent Mapping</a>. One example would be Customer mapping with secondary email which is optional and in this case Maybe. NHibernate Fluent Mapping for <a href="https://github.com/vkhorikov/FuntionalPrinciplesCsharp/blob/master/New/CustomerManagement.Logic/Model/Customer.cs">Customer </a>class using CustomType you can find <a href="https://github.com/vkhorikov/FuntionalPrinciplesCsharp/blob/master/New/CustomerManagement.Logic/Model/CustomerMap.cs">here </a>on github.</p><p>The real problem came when we had optional fields, which was not value type or string, but some complex type, with its own object graph.</p><p>Consider example with Customer class given bellow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53d5fc2620a23a5161201acfdce89aa5/href">https://medium.com/media/53d5fc2620a23a5161201acfdce89aa5/href</a></iframe><p>and Address class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffe384d13e4e0833e0270280638a464b/href">https://medium.com/media/ffe384d13e4e0833e0270280638a464b/href</a></iframe><p>So we had a class Customer that among other usual fields (Name, Age, etc.) has optional Billing address, which we don’t need until the customer makes the first purchase. This was one of the UX requirements. So billing address is needed for the purchase but we will not ask user to put it in until it’s really necessary. Address, on the other hand is a value object with its own properties.</p><p>We didn’t find the way to properly map something like this in NHibernate (probably due to the lack of the documentation). Still we were not 100% sure even if this is possible, but we already didn’t like previous approach with the Custom Type mappings we mentioned earlier. After two days of trying we decided to make an architectural change on a project! We are going to try things with Mongo DB, and this is why that promise we made earlier was so important!</p><p>But wait, going from relational databases to document database, can this somehow be justified? Actually it can, and it makes perfect sense when adding DDD in the context, — <strong>making one document collection for one aggregate root in the domain model</strong>. While working with the relational database model, we were constantly wrestling where and how to store value objects of particular aggregate root, especially if we have a <a href="http://enterprisecraftsmanship.com/2016/08/04/representing-a-collection-as-a-value-object/">list of value objects</a>. In this case, we needed a separate table in relational database and had to make artificial entities in the model. From this perspective, I don’t know why we didn’t try using Mongo DB initially. One can say that we lose atomicity when using No SQL database, but this can be avoided by changing architecture and applying Event Sourcing for example.</p><p>OK, let’s now return to our example and mapping of our Customer class. You will see that our custom Maybe serializer solved all the mapping issues we had earlier.</p><p>Mongo mappers for domain classes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f3043425650d9eb835de0f7d35476b9/href">https://medium.com/media/6f3043425650d9eb835de0f7d35476b9/href</a></iframe><p>Our custom Mongo Maybe serializer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca8e7c5dbc0988646acce486509e4c2e/href">https://medium.com/media/ca8e7c5dbc0988646acce486509e4c2e/href</a></iframe><p>You can find our whole test project (.NET Core) on <a href="https://github.com/kolotree/MaybeMongo">github </a>and check it out. This is our approach which may not be the best one, but we are open for suggestions and opinions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d09f32450972" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kolotree-blog/to-be-or-not-to-be-maybe-d09f32450972">To be or not to be? Maybe.</a> was originally published in <a href="https://medium.com/kolotree-blog">KoloTree Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>