Repository pattern - Do I "need" a service layer in REST API? : <p><em>Im making this question to get your opinions on the matter, and to see if I'm being dumb or pedantic.</em></p>

<hr>

<h1>Question (tl;dr;)</h1>

<p>Do I "need" the service layer (for good practice) when the repository layer already can do most of the things (like FindUserByID) with queries in a faster way, when the service has to do all these things manually in a much more inefficient and super manual way?</p>

<h1>Description (for those who want to read about my motives)</h1>

<p>So, I've been dealing with Clean Architecture, which is great, but not for REST architecture. Since, Clean Architecture is literally made with a Controllers layer, and RPC is the one that handles controllers, not REST. REST handles resources. <strong>So, I removed the "controller" layer, and I'm also thinking if I really need a "service" layer as well.</strong></p>

<p>So REST needs <strong>"services"</strong> and/or <strong>"repositories"</strong>. The strange thing, is that I think these two overlap. I know that <strong>services</strong> are supposed to <strong>handle "business rules"</strong>. But here's the thing:</p>

<h2>Repositories can do the same job with services, but repositories can do the <strong>same job in a much more efficient way</strong>.</h2>

<p>Since Repositories are allowed to have direct communication with the database, they can use database queries (sql, or nosql ones). Which are much more <strong>efficient to write</strong>, much more <strong>efficient to read</strong>, much more <strong>efficient on performance</strong>.</p>

<h3>Service way:</h3>

<p>Say, you need to find a user by his ID, and then pick a specific friend from his friendlist by his name.</p>

<ul>
<li>You get all the users from <strong>repository</strong></li>
<li>You make a <strong>loop</strong> for all the users</li>
<li>You make a <strong>condition</strong> where you check which user has the ID you want</li>
<li>You make a <strong>loop</strong> for all his friends in the friendlist</li>
<li>You make a <strong>condition</strong> to check the friend's name</li>
</ul>

<p><strong>Summary? Like 20-30 lines of code and slower performance?</strong></p>

<h3>Repository way:</h3>

<pre class="lang-golang prettyprint-override"><code>filter := bson.M{
    "_id": id,
    "friendlist.name": friendsName,
}
projection := bson.M{
    "friendlist": 1
}
friend, err := mongoDb.find(filter, projection)
</code></pre>

<p><strong>Summary? Like 3-4 lines of code, and 99% database performance.</strong></p>

<p>So, is there any real need for the "Service layer" part? Is there any real architectural benefit on splitting it to a service layer and a repository layer, when repository can already do all the jobs much more efficiently by itself?</p>
