Mark van Venrooij

Mark van Venrooij's blog

Applying Domain-Driven Design

tl;dr

In this post I apply Domain-Driven Design to my favorite pet project. The picture above contains the resulting model. The resulting code can be found at GitHub.

My attempt on Domain-Driven design

As mentioned in my previous post I need to learn Domain-Driven Design for my new job. In this post I apply the things I learned till now to an application that gives me insight in my financial situation. The domain is very familiar to me as I used it before to learn new things. The application mainly tells me where I spent my money. Next to that I set yearly budgets on each category in the beginning of a year. During that year I will compare the budget against the actual situation to assess if I’m still on track. Ideally the application also incorporates some functionality to calculate my savings rate and some other metrics related to reaching financial independence.

Ubiquitous language

Eric Evans’ book emphasizes the importance of the ubiquitous language. So let me try to define the ubiquitous language for this application.

To achieve financial insight the application needs to import and categorize financial transactions. I want to set budgets for a year and compare them to the actual amounts. Furthermore based on the actual numbers the savings rate and years till financial independence must be calculated. In order to calculate the years till financial independence there is something to assess my savings and investments.

The bold words seem fundamental concepts to me. These words should play a major role in my domain. Probably I miss a couple of concepts right now, but that is fine. I will refine the ubiquitous language later. The most interesting part of the application to me is the last part: calculating the metrics on FI. However these are a result. In order to achieve FI earlier there are two possibilities:

  • Increase your income
  • Reduce your spendings

The application will not help in getting more income. Getting insight in your expenses can help to see when budgets are exceeded and where expenses are high. So for now I first focus on categorizing financial transactions and creating budgets.

Please note that for my personal finances I don’t use double bookkeeping as it seems overkill. Next to that I ignore any cash transactions. Not categorizing cash transactions will not influence my insights much as I don’t use a lot of cash. Next to that keeping all receipts is too much of a burden to me.

Entities versus value objects

After defining the ubiquitous language the next step in Domain-Driven Design is to try to make the model. Important parts of the model are the entities and value objects. As the focus is on categorizing and budgeting I find at least 3 objects of interest: Category, Transaction and Budget. To me a Category is an entity. Mainly because in time the amount of money spent in a Category will change via adding new transactions. Furthermore there must be a Budget more specifically a Budget per Category per year. Budgets are entities as pretty much anything can change in time.

If I look at Transaction I’m a bit in doubt. During the life cycle of my financial administration a transaction will not change. That is because they are originating in an external source: my bank. Next to that I’m only concerned about the values of a Transaction. If I need to make a guess I think I need an account number, a contra account, the date, the amount and the description of the Transaction. These fields are also the important ones in my naive Bayesian classifier to categorize my transactions. For now I make a choice to make a Transaction a value object. This might be revisited later.

Identities & associations

For Category the name is a good identity. There is a unidirectional association from category to zero or more Transactions. Next to that there is a unidirectional association between a Budget and a Category. Because setting a Budget without being able to categorize real Transactions doesn’t make sense, it is not possible to have a Budget without a corresponding Category. Over the years there are many Budgets per Category, however per year there can be only 1. This means that for the identity of the Budget it is probably wise to include the year. To me combining the Category and a year seems a good choice as identity for the Budget.

Aggregates

Time to define some aggregates in the model. Typically a Budget is created at the beginning of each year and Categories are created once (and maybe adjusted once in a while). However during the year I import and categorize my Transactions. For me this leads to a logical split. Making Budget an aggregate with only one entity. Category and Transactions are an aggregate as well. Category is the aggregate root. As Budget is a separate aggregate root I don’t want to include the Category as an object. I just want to have a reference to a Category through its identity. This has implications for the Budget and its identity. It identity is now made of a Category name and a year.

Repositories

Time to choose the repositories. My first attempt is a repository per aggregate. Given the requirements this makes sense to me now. In the future I probably want to persist my data somehow. Maybe with a relational database, maybe an object database or maybe just plain old CSV files. For exploring and building the domain this is not relevant. Furthermore maybe some future feature(s) will give me more information what storage technology will suit my needs. For now I will just use an plain old java implementation of my repository, using the collection classes.

Creation of aggregates

Creating a category is straight forward as there are no special requirements here. So a constructor will suffice. However for creating a Budget we need to make sure that there actually is a Category available. To check if a Category exists I need to find the Category by name using the CategoryRepository. I could make an association in Budget to that repository. However that seems bad practice. I think creating a BudgetFactory makes sense in this case.

Conclusion

Given the small number of objects in the domain now I don’t find it necessary to split these classes among different modules yet. The model created seems to cover my basic needs of storing the budgets and categorize transactions. The complete design is shown below.

The resulting DDD model

The resulting DDD model

Next to that the resulting code, including unit tests, can be found on GitHub. However I didn’t implement comparing budgets to actual expenditures yet. Next to that I only implemented the model so at this moment there is no application to run. These are topics for another blog post.

Learning domain-driven design

On my new job the department is applying Domain-Driven Design. I did read Domain-Driven Design – Tackling Complexity in the Heart of Software by Eric Evans a couple of years back. However as I never used it I need to learn how to apply it. So in order to really learn DDD I started to reread Eric Evans’ book and plan on reading Implementing Domain-Driven Design by Vaughn Vernon. As theory and practice are only equal in theory, I want to apply DDD to my favorite pet project. To recap an earlier blog post, many years ago I started categorizing all my financial transactions. At first I used to build a tool myself to give me insight in my spendings, later I bought something to do this for me. However this is now the domain I know pretty well and use to learn new things.

Current progress

Currently I read till chapter 5 of Eric Evans’ book. The key things I learned in the first chapters:

  • Make domain concepts explicit by capturing the knowledge in the design
  • Define a ubiquitous language using domain terms
  • The feedback loop from coding back to the design is often missing
  • The domain must be isolated from the user interface, db glue and other supporting code in order to achieve separation of concerns
  • The smart UI cannot be combined with DDD

Moving forward

My plan is to finish reading both books before the end of the year. (I hope I can find the time, given that I have a young daugther.) In the same time I plan to work on my pet project. As I learned long ago a good way to learn things is to teach others. So I want to share my progress in implementing DDD on my pet project through this blog. So the coming weeks expect some new blog posts on this topic. Basically me struggling to apply the DDD concepts.

quick update

A long overdue update from my side. As mentioned in previous post I got married last year. Next to that this year we welcomed a newborn daughter. So my life changed a lot the last 12 months.

Update on my path to financial independence

We saved a bit less than the goal (25%) of last year, this is basically due to buying a nursery and all other things for my daughter. However having a young daughter compensates for that a easily. The Savings as percentage of FI goal basically hit the goal as the stock market was pretty good. Ignoring any market changes and interest it would take approximately 36 years to be financial independent giving the total savings of last year. The goals for this year are a bit more modest compared to last year. It should be possible to reach the 20% savings rate and that would result in reaching 18% of my set FI goal.

Savings rate 2016

Savings rate year 18.5%
Savings as percentage of FI goal 14
years till financial independence using total amount of savings of last 12 months 36

Financial goals 2017

Savings rate 20%
Savings as percentage of FI goal 18%

financial independence

Happy new year to everyone I wish you all a healthy and succesful 2016! In my previous post I mentioned that I would explain more about my path to becoming financially independent.

What is financial independence

According to wikipedia financial independence means:

Financial independence is generally used to describe the state of having sufficient personal wealth to live, without having to work actively for basic necessities. For financially independent people, their assets generate income that is greater than their expenses

How does it work

There are many blogs that explain how to get financial independent. Let me try to give a quick summary.

As stated the main goal is to let my assets generate enough income to cover for my expenses. The question is then how much assets do I need. This question is not easy to answer and it depends on your personal situation. There many ways to generate income with money, e.g. becoming a landlord/landlady. In my example however I’m going to use the stock market to generate income as this is the most common scenario.

As we all know stock markets go up and down and nobody can predict it correctly. Given that there is this much of uncertainty, how much assets do we need to have to make sure we won’t have to work again. Or in other words how much can I withdraw from my assets without depleting the assets too quickly. The Trinity study tries to answer this question. Basically they used historical data to check if withdrawing X% of the initial portfolio each year, this was possible for at least 30 years without being out of money. Within this study a safe withdrawal rate is determined at 3 to 4% of the initial total portfolio value. As we all know results in the past are no guarantee for the future, but we have to work with something.

To be conservative let’s take 3%. That means I need about 33 times my yearly expenses in assets to be able to call myself financially independent. Next step is then how to get there. I don’t think you have this amount of money lying around, at least I don’t. To become financially independent I need to save this amount of money. As said before I’m using the stock market to earn money with money. How much do I need to invest each month?

There is a metric that is often used to answer this question: Savings rate. This is easy to calculate: savings divided by income (after taxes) * 100%. E.g. assume I have an income of €2500 and save €1000 each month. My savings rate would be: 1000 / 2500 * 100% = 40%. Using the savings rate anyone can determine in how many years one would be financially independent.

Savings rate Years left Savings rate Years left
5% 102 55% 21
10% 78 60% 18
15% 65 65% 15
20% 55 70% 13
25% 47 75% 10
30% 41 80% 8
35% 36 85% 6
40% 31 90% 4
45% 28 95% 2
50% 24 100% immediate

Assumptions used in calculations above: There are no current savings, a conservative ROI of 3% per year, the expenses are just as high as they are now, income will not grow or decline and finally there is no inflation. Sure there will be inflation and probably you will get a raise. But let’s keep that stable for simplicity and probably they will offset anyway. Historically the ROI of the stock market is higher than the 3% I used here, that should compensate for inflation as well.

My road to financial independence

For my path to financial independence I calculated how much money I needed after I paid off my mortgage using current expenses. Using a safe withdrawal rate of 3%, multiplying these expenses by 33, I got my target amount. I don’t use any future ROI of my current savings, because that is used to compensate for inflation.

Given these assumptions the numbers for December are:

Savings rate last 12 months 30,6%
Savings rate year to date 30,6%
savings as percentage of financial independence goal 12,1%
years till financial independence using total amount of savings of last 12 months 32

As you can see it takes me longer than the expected 25 years I was speaking about in pervious post. That is mainly because I don’t take any ROI into account for these calculations.

Financial goals 2016

Savings rate 25%
Savings as percentage of financial independence goal 13.8%

For 2016 I have a goal to save 25% of my income. This is much lower than the savings of last year. Main factor to this drop is that I’m going to marry this year. Taking these new savings and adding these to my current stash (ignoring any market changes) I would be at 13.8% of my end goal at the end of 2016.

Review 2015, plans 2016

Has another year really flown by? My Calendar definitely says yes. So it is time to look back at 2015 and make plans for 2016.

2015

Good things I want to continue

As you probably can see from my previous posts I somewhat obsessed by my personal finance situation. At least that is what other people in my environment tell me. For me I just like numbers statistics, in other words I’m a nerd and I’m perfectly happy with that. In the beginning of 2015 I made plans to be financially independent in approximately 25 years. I was able to stick to this plan since I made it. I won’t go in the details here (sounds like worth a post of his own). Some people in my environment tell me this is impossible, maybe this is true but planning for it makes me at least in the less dependent on my job in the future.

In the beginning of the year I had a job quite far from home. I was lucky to find a job with more responsibilities closer to home. Details about this you can find at my LinkedIn profile. I love this job and hope I’m able to keep it the coming year.

Things I want to improve

One thing I totally failed at in 2015 is blogging. If I look at my post list for 2015 it tells me there are exactly 0 posts so far. So the plan for 2016 is to have a monthly post. Not sure what the content will be, but in this post at least I found 1 subject to explore.

In 2015 I was very busy with my job and lots of other stuff that was really important. I totally forgot to sharpen the knife. So in 2016 I need to spend more time and effort on learning new things. This includes professional skills and personal skils.

Yes I was busy and I totally forgot about my photography hobby. I made some nice pictures, but not nearly enough if you look at the fun it brings me.

Plans 2016

So there are some things to improve on as I mentioned in my review of 2015: Blogging at least once a month, learn new things like exploring the topic of machine learning, spend at least 1 day a month making very nice photos

The biggest plan however is to marry my girlfriend. This will not only affect 2016 but probably the rest of my life. After being together for more than 10 years I feel this is the best way forward. This will affect my plans for being financially independent in a negative way and will cost a lot of time planning that could harm my other plans but I think this is totally worth it.

« Older posts

© 2018 Mark van Venrooij

Theme by Anders NorenUp ↑