<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sanchit Rk</title>
    <description>The latest articles on DEV Community by Sanchit Rk (@sanchitrk).</description>
    <link>https://dev.to/sanchitrk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F111352%2F6cdda757-c1e5-4664-87fe-d63f62ae7a76.png</url>
      <title>DEV Community: Sanchit Rk</title>
      <link>https://dev.to/sanchitrk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sanchitrk"/>
    <language>en</language>
    <item>
      <title>Building SaaS with DDD &amp; Clean Architecture in Python — Issue 1</title>
      <dc:creator>Sanchit Rk</dc:creator>
      <pubDate>Thu, 31 Aug 2023 17:31:45 +0000</pubDate>
      <link>https://dev.to/sanchitrk/building-saas-with-ddd-clean-architecture-in-python-issue-1-5e6h</link>
      <guid>https://dev.to/sanchitrk/building-saas-with-ddd-clean-architecture-in-python-issue-1-5e6h</guid>
      <description>&lt;p&gt;It's been a few weeks, and I decided to work on a SaaS application — I had a bunch of ideas for some time now and was looking to focus on a problem space worth solving.&lt;/p&gt;

&lt;p&gt;I chose Python as the programming language, as I felt a bit rusty — I have been a Node.js developer for a while and wanted to brush up on my Python skills.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Now, I don’t recommend this approach. I suggest focusing on the problem and solution and most importantly talking to customers.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When building real apps, you can select a programming language that you are most familiar with. Focusing on your knowledge and expertise is more important than getting caught up in the complexities of learning or refreshing a new programming language.&lt;/p&gt;

&lt;p&gt;My goal was also to brush up on my Python skills and learn DDD and Clean Architecture practices. Hence, I chose Python.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem Space
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Companies are using Slack for all internal communications, and with shared channels, outside members can also be part of the conversations. Being used to support customer queries, talk to customers, and collaborate with team members, and much more.&lt;br&gt;
Managing conversations with customers and internal teams can be challenging, as it’s easy to get overwhelmed. To address this issue, I decided to build a SaaS application that seamlessly integrates with Slack and offers effective conversational support for teams and companies. This solution aims to streamline communication processes and enhance efficiency in providing support.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In DDD, the first step is to understand the problem space. Now that we have some understanding of the problem that we are solving, we can focus on the solution space — the implementation details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Uncovering Entities &amp;amp; Properties
&lt;/h2&gt;

&lt;p&gt;Entity is a domain object when we care about its individuality when distinguishing it from all the other objects in a system. It's a unique thing capable of being changed in its lifetime; changes can be so extensive that the object might seem different, yet it is the same object by identity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tenant&lt;/strong&gt;&lt;br&gt;
As we are building a SaaS, we want to represent our customers. It's typical to call them Tenants. Tenants can have users, customers, billing information, etc. We only have a little information on the Tenant; let's move on. We can add intrinsic details later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Tenant(AbstractEntity):
    def __init__(self, tenant_id: str | None) -&amp;gt; None:
        self.tenant_id = tenant_id

    def __eq__(self, other: object) -&amp;gt; bool:
        if not isinstance(other, Tenant):
            return False
        return self.tenant_id == other.tenant_id

    def __repr__(self) -&amp;gt; str:
        return f"""Tenant(
            tenant_id={self.tenant_id}
        )"""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will add more details as we build along; now, the entity looks fine for a start.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;User&lt;/strong&gt;&lt;br&gt;
Tenants will have Users, and I can think of Users to have some role. We only have a little information on how Users will register or get created, but I know how to represent them as an entity. With these details, let's define the User entity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UserRole(Enum):
    OWNER = "owner"
    ADMINISTRATOR = "administrator"
    MEMBER = "member"

class User(AbstractEntity):
    def __init__(
        self,
        tenant_id: str,
        user_id: str | None,
        name: str | None,
        role: UserRole.MEMBER,
    ) -&amp;gt; None:
        self.tenant_id = tenant_id
        self.user_id = user_id
        self.name = name
        self.role = role

    def __eq__(self, other: object) -&amp;gt; bool:
        if not isinstance(other, User):
            return False
        return (self.tenant_id == other.tenant_id) \
          and (self.user_id == other.user_id)

    def __repr__(self) -&amp;gt; str:
        return f"""User(
            tenant_id={self.tenant_id},
            user_id={self.user_id},
            name={self.name},
            role={self.role.value}
        )"""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We know that the User should be uniquely identifiable in the Tenant; hence, the equality implementation for the entity object checks for &lt;code&gt;tenant_id&lt;/code&gt; and &lt;code&gt;user_id&lt;/code&gt; properties.&lt;/p&gt;

&lt;p&gt;Understanding &lt;code&gt;__eq__&lt;/code&gt; — in Python is out of the scope of this discussion. I will be writing about it in the future.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Validation&lt;/strong&gt;&lt;br&gt;
We can safely add some validation in the entity, for example, the User entity — making sure we have the tenant_id — When creating/initializing the User entity, e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
def __init__(
        self,
        tenant_id: str,
        user_id: str | None,
        name: str | None,
        role: UserRole.MEMBER,
    ) -&amp;gt; None:
        self.tenant_id = tenant_id
        self.user_id = user_id
        self.name = name
        self.role = role
        ...

        assert tenant_id is not None
        assert role is not None
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet, I'm making sure we don't set the &lt;code&gt;tenant_id&lt;/code&gt; as &lt;code&gt;None/Null&lt;/code&gt; — enforcing the domain contract that the User is always part of the Tenant.&lt;/p&gt;

&lt;p&gt;For now, I will skip these validations, but there is no harm in having those checks in the domain entity.&lt;/p&gt;

&lt;p&gt;In the next issue, #2, I will focus on mapping Slack events APIs to entities and value objects. I will also explore value objects in more detail and intrinsic details on entities vs. value objects.&lt;/p&gt;

&lt;p&gt;Till then, follow this series.&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>ddd</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
