avatarMatthew Maniaci

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4376

Abstract

<span class="hljs-keyword">private</span> <span class="hljs-keyword">readonly</span> paymentService: PaymentService, <span class="hljs-keyword">private</span> <span class="hljs-keyword">readonly</span> moderationService: ModerationService </span>) {}

<span class="hljs-keyword">public</span> <span class="hljs-title function_">createPost</span>(<span class="hljs-attr">args</span>: <span class="hljs-title class_">CreatePostArgs</span>): <span class="hljs-title class_">Post</span> { <span class="hljs-keyword">const</span> post = <span class="hljs-variable language_">this</span>.<span class="hljs-property">postRepository</span>.<span class="hljs-title function_">create</span>(args);

<span class="hljs-variable language_">this</span>.<span class="hljs-property">reputationService</span>.<span class="hljs-title function_">increaseReputation</span>(args.<span class="hljs-property">userId</span>);
<span class="hljs-variable language_">this</span>.<span class="hljs-property">notificationService</span>.<span class="hljs-title function_">notifyFollowersAboutPost</span>(post);
<span class="hljs-variable language_">this</span>.<span class="hljs-property">userService</span>.<span class="hljs-title function_">updateUserActivity</span>(<span class="hljs-string">'post.created'</span>, post);
<span class="hljs-variable language_">this</span>.<span class="hljs-property">trackingService</span>.<span class="hljs-title function_">registerTrackable</span>(<span class="hljs-string">'post'</span>, post);
<span class="hljs-variable language_">this</span>.<span class="hljs-property">moderationService</span>.<span class="hljs-title function_">checkPostContentForViolations</span>(post.<span class="hljs-property">content</span>);    

<span class="hljs-keyword">if</span> (args.<span class="hljs-property">isPremium</span>) {
  <span class="hljs-variable language_">this</span>.<span class="hljs-property">paymentService</span>.<span class="hljs-title function_">chargeUserForPremiumPost</span>(args.<span class="hljs-property">userId</span>);
}

<span class="hljs-keyword">return</span> post;

} }</pre></div><p id="154d">This example is obviously made up, so please don’t pay too much attention to the details. I just want you to notice how many services <code>PostService</code> is dependent on and imagine that these services may also be dependent on PostService. For example, the notification service may require additional data from the post service to dispatch notifications, or the <code>ModerationService</code> may need to modify the post again via <code>PostService</code> after moderation. This is where circular dependencies occur.</p><p id="96c0">So now that we have a grasp of the problem, let’s explore the solution that I want to propose.</p><h1 id="c471">The solution</h1><p id="72f0">The solution I want to propose for this problem is to use the well-known concept of Event-Driven Architecture. Instead of calling all subsequent actions from the <code>createPost</code> method, we will simply create the post there and emit an event. Then, any module interested in performing some action related to the event may do so without crossing its logical borders.</p><p id="53d2">NestJS already comes with handy tools that we can use to benefit from events. If you don't yet have the package installed, you can simply add <code>@nestjs/event-emitter</code> to your application.</p><div id="9629"><pre>yarn <span class="hljs-keyword">add</span> @nestjs/<span class="hljs-keyword">event</span>-emitter</pre></div><p id="32cf">And when the package is installed, add the <code>EventEmitterModule</code> to the root module of your application.</p><div id="e0ec"><pre><span class="hljs-keyword">import</span> { Module } <span class="hljs-keyword">from</span> <span class="hljs-string">'@nestjs/common'</span>; <span class="hljs-keyword">import</span> { EventEmitterModule } <span class="hljs-keyword">from</span> <span class="hljs-string">'@nestjs/event-emitter'</span>;

<span class="hljs-meta">@Module(<span class="hljs-params">{ imports: [ EventEmitterModule.forRoot(<span class="hljs-params"></span>), // ... ], }</span>)</span> export <span class="hljs-keyword">class</span> <span class="hljs-title class_">AppModule</span> {}</pre></div><p id="f073">When we have it ready, we may simply ge

Options

t rid of all <code>PostService</code> dependencies and replace them with only one — <code>EventEmitter2</code>. After this, we may also remove subsequent method invocations from the <code>createPost</code> method and instead emit an event with its key and payload. The payload may be defined as a separate interface or class if you wish, but here, for presentation purposes, I will just emit the created post as a payload.</p><div id="86c2"><pre><span class="hljs-keyword">class</span> <span class="hljs-title class_">PostsService</span> { <span class="hljs-title function_">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">private</span> eventEmitter: EventEmitter2</span>) {}

<span class="hljs-keyword">public</span> <span class="hljs-title function_">createPost</span>(<span class="hljs-attr">args</span>: <span class="hljs-title class_">CreatePostArgs</span>): <span class="hljs-title class_">Post</span> { <span class="hljs-keyword">const</span> post = <span class="hljs-variable language_">this</span>.<span class="hljs-property">db</span>.<span class="hljs-property">posts</span>.<span class="hljs-title function_">create</span>({ ... }); <span class="hljs-variable language_">this</span>.<span class="hljs-property">eventEmitter</span>.<span class="hljs-title function_">emit</span>(<span class="hljs-string">'post.created'</span>, post);

<span class="hljs-keyword">return</span> post;

} }</pre></div><p id="a801">The last thing we have to do is to add a listener to the modules that may be interested in this event. For example, in the notification module, we may have a listener as below. In the other modules, the code will be pretty much the same, just other services will be involved in event handling.</p><div id="c0ad"><pre><span class="hljs-keyword">class</span> <span class="hljs-title class_">NotificationsListener</span> { <span class="hljs-title function_">constructor</span>(<span class="hljs-params"> <span class="hljs-keyword">private</span> <span class="hljs-keyword">readonly</span> notificationsService: NotificationsService </span>) {}

<span class="hljs-meta">@OnEvent</span>(<span class="hljs-string">'post.created'</span>) <span class="hljs-title function_">handlePostCreated</span>(<span class="hljs-params">post: Post</span>) { <span class="hljs-variable language_">this</span>.<span class="hljs-property">reputationService</span>.<span class="hljs-title function_">notifyFollowersAboutPost</span>(post); } }</pre></div><p id="9b3f">This way, the <code>NotificationService</code> and <code>PostService</code> are only loosely coupled now. The <code>PostService</code> is no longer dependent on <code>NotificationService</code>. We get rid of circular dependency here, yet we keep the functionality still working — Yay!</p><h1 id="ea7f">Summary</h1><p id="bf46">To sum up, in this article we explored the proposition of introducing events into your application to solve the issue of circular dependencies. This approach helps modules to stay within their borders yet react to actions performed in another module as well. Even though this way may not be applicable to all solutions it definitely may fix the one presented today.</p><p id="c9dc">I want to say thank you to all who read this article. I would love to hear your thoughts about this proposal and your ways of tackling circular dependencies in your applications, so feel free to share.</p><p id="913c">Don’t forget to check out my other articles for more tips and insights. Happy hacking!</p><h1 id="7cf6">Stackademic 🎓</h1><p id="d5c3">Thank you for reading until the end. Before you go:</p><ul><li>Please consider <b>clapping</b> and <b>following</b> the writer! 👏</li><li>Follow us <a href="https://twitter.com/stackademichq"><b>X</b></a><b> | <a href="https://www.linkedin.com/company/stackademic">LinkedIn</a> | <a href="https://www.youtube.com/c/stackademic">YouTube</a> | <a href="https://discord.gg/in-plain-english-709094664682340443">Discord</a></b></li><li>Visit our other platforms: <a href="https://plainenglish.io"><b>In Plain English</b></a><b> | <a href="https://cofeed.app/">CoFeed</a> | <a href="https://venturemagazine.net/">Venture</a> | <a href="https://blog.cubed.run">Cubed</a></b></li><li>More content at <a href="https://stackademic.com"><b>Stackademic.com</b></a></li></ul></article></body>

Divorce is Not Inherently Bad

How the laws around divorce have empowered generations of women.

Photo by Zoriana Stakhniv on Unsplash

I have heard a lot about how bad divorce is. Growing up, I had two parents who had been together since high school and showed no signs of getting tired of each other. As far as I know, they’re still doing well. I lived in an upper-middle-class neighborhood where very few parents got divorced. I knew as a teenager that divorce was common, but I had minimal experience with it.

My partner is the product of divorce in many ways. My partner’s mother divorced her second husband, my partner’s biological father, when my partner was in middle school. He was (and as far as I know, still is) an abusive piece of human garbage, and the divorce was warranted.

People divorce for many reasons, including abuse, but also less severe reasons. A disconnect between partners on religion, politics, money, or a whole host of other issues can lead to divorce. People often say that divorce is an inherently bad thing, as a marriage is being broken up. A loving couple has separated, and the world is worse for it.

Considering the abuse my partner’s father put her and her mother through, I wouldn’t say that’s a loving couple whose fantastic relationship is being broken up by petty issues. I’d say that’s a woman escaping her abuser.

I’m not saying that all divorces are that clear-cut — far from it. However, a divorce generally heralds the separation of an incompatible couple that got together for any number of reasons and only realized too late that they were incompatible.

I’ve seen couples split for many reasons. One person in my orbit divorced her husband because she was adamant that she didn’t want kids, and while her husband initially agreed, he married her with the idea that he could change her mind and get her to have a kid. She decided that was a dealbreaker, and they split.

I’ve also seen couples get married for many reasons that are just begging for a divorce. There have been many, many people in my life that I have seen get married because they are having relationship issues and think that marriage will magically fix everything. People often have kids for the same reason, which only compounds the eventual issues.

There have also been many people during the past year who discovered that they just can’t live with each other. Spending 8+ hours a day at a job keeps you from seeing a lot of the flaws your partner has. One of my former coworkers leaped at the chance to go back to the office, as her husband worked from home pre-pandemic and she discovered that spending all day every day with him drove her bonkers and she couldn’t stand him.

Many people view divorce as an inherently bad thing. A relationship is breaking up, they reason, which is always bad. I disagree. Divorce doesn’t destroy good marriages, it rightfully breaks up bad marriages. Many of the negative outcomes are the result of how the divorce happens — many divorces are bitter, and there are often children involved, which is traumatic for them.

The march of time has changed both perceptions of divorce and perceptions of marriage as a whole. This article has a lot of interesting bits of data about divorce in America. Among other things, divorce is more common among Boomers, as many Boomer couples got together as teenagers or in their early 20s, which is an indicator of higher divorce rates. It also notes the rise in no-fault divorce laws that enabled a lot of people in bad marriages to escape.

That’s one key element here: women have been empowered to escape bad marriages due to these laws. For a long time, marriage was an issue of financial stability for women. A few generations ago, women were largely absent from the workplace, couldn’t open bank accounts or manage their own money without their husband’s permission, and were generally excluded from many of the institutions that might enable them to have financial independence.

However, as these institutions have opened to women and coupled with the advent of no-fault divorces, more women have been able to escape abusive or otherwise bad marriages and have access to the institutions that would enable them to be independent. In the past, marriage was a means of ensuring their financial stability; now that a husband isn’t required to access banking and credit institutions, marriage is less necessary.

On top of that, Millennials have been changing marriage as an institution. They are less likely to be married, more likely to get married later in life, more likely to be college-educated before getting married, and more likely to cohabitate with their partner before marriage. They also tend to spend time growing their assets and income before marriage and increasingly signing prenups, while some aren’t getting married at all, viewing it as an outdated institution.

As a result, the divorce rate is coming down overall, even when taking into account the “gray divorce” trend. This seems to imply that the huge divorce rate in the 80s and 90s indicates that many Boomers and Gen Xers were getting out of bad marriages as no-fault divorce became more common. As many Millennials were the product of those divorces, they are naturally more marriage-averse and more likely to take steps to avoid such an outcome.

None of this is to say that Millennials don’t get divorced — the acquaintance I mentioned above is one of several examples in my circles. In those cases, as in the cases of the Boomers and Gen Xers that divorced before us, it was bad marriages that broke up, not good ones that were destroyed.

Many people will say that these days, people don’t work out their problems as they did in the past. Instead, they just jump straight to divorce, making the rate skyrocket. I have two responses for that: first off, for reasons I already talked about, marriage in the past was an uneven arrangement. The man had all the power, forcing the woman to acquiesce to his wants. In this case, the options were to work out your issues or face ostracization from society due to leaving your husband. Heck, in the not-too-distant past, you didn’t even have to like or know each other before getting married — it was a business contract more than a love match.

Second off, as I mentioned above, younger people are increasingly getting to know each other and communicating their wants and needs before attaching a legal document to their relationship. That is to say, they’re working out their problems and talking about their issues before marriage even becomes an option. Turns out the best way to prevent divorce when you have relationship issues is to not be married in the first place.

Ultimately, marriage and divorce are changing whether we like it or not. The divorce rate is declining in no small part due to the marriage rate also declining. People these days are more open to talking out their issues, and they’re discovering that working out the issues before marriage saves a lot of trouble and paperwork.

I encourage you to reconsider your views on divorce if you think it’s inherently bad. There are many good reasons for divorce, and I feel that not recognizing that is a danger to women and men everywhere that may find themselves in abusive or toxic relationships.

And, if you find yourself in a situation where you think you’re incompatible with your spouse, think about it. If you feel you can work through your problems, then I encourage you to seek counseling. If not, and your marriage has become toxic or otherwise untenable, I encourage you to consider divorce as an option. There is no shame in separating from your spouse if the relationship simply isn’t working out.

And, if you’re having reservations about getting married, I encourage you to have an open, honest conversation with your partner. Better to find out your relationship won’t work before you sign all the papers.

And, always remember, communication is key. One of the best ways to figure out whether you’re compatible with someone or not is to have an open, honest conversation with them about what you both want. Sometimes, just having an open, honest conversation, with your partner or anyone else in your circle, can help you realize what it is you want and what is best for you, and you will be better for it.

If you liked this, please subscribe to my publication, Thing a Day. I publish something every day on a variety of topics, so you never know what you’re going to see!

Here are some other things I’ve written:

Divorce
Relationships
Marriage
Love
Life
Recommended from ReadMedium