Support the ongoing development of Laravel.io →
Architecture
Last updated 2 years ago.
0

Not an expert by any means but I would say repositories are the data layer in your application. They're an API for your controller to interface with to store or fetch data. Your repository could have multiple dependencies such as models or web services it uses to consolidate the data. Repository methods could also contain logic on how to get or set data.

Last updated 2 years ago.
0

You should read Taylor's book, all your questions will be answered.

Last updated 2 years ago.
0

Service: A class that operates a business logic that doesn't belong to a specific business object. Let's say your website accepts a payment. You probably have classes from your payment gateway service. You also have a user class. Where should I put the logic to charge the user? We can just put the logic into the user object, but I think we have a better approach. I think we can make a service. The following is a pseudo code.

class ChargeUserService
{
	private $paymentGateway;

	public function __construct(aPaymentGatewayClass $paymentGateway)
	{
		$this->paymentGateway = $paymentGateway;
	}

    public function charge($creditcard, $amount, User $user)
    {
    	// psuedo code
    	// charge the amount
    	$this->paymentGateway->charge($creditcard, $amount);
    	
    	// make a log; say that the user has paid
    	// or do whatever you want with the user
            // send an email notification may be..

    }
}

Repository: There're two definitions of a repository as far as I concern. One from Martin Fowler and Eric Evans. They share pretty much the same concept, but they're not the same thing. Eric Evans' repository pattern includes aggregates, entities, and value objects. I think we (php devs generally) talk about Martin Fowler's definition unless we're doing DDD.

Martin Fowler's definition: http://martinfowler.com/eaaCatalog/repository.html Eric Evans: Well..this one is Martin Fowler's definition + Domain Driven Design concepts such as aggregate, entity, and value objects.

Business Logic: You really don't have to think about it too much or think it as an advanced topic. It's pretty simple. Anything that's related to your business can be said as a business logic. If you have a 'suspend' method on your User object, that's your business logic.

Last updated 2 years ago.
0

AnthonyVipond said:

You should read Taylor's book, all your questions will be answered.

Hi AnthonyVipond, I'm searching that reference but I cannot find.

Thanks

Last updated 2 years ago.
0

The book is here: https://leanpub.com/laravel

Don't expect to understand services, and repositories just by reading Taylor's book. It certainly helps, but the book is not about services or repositories.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.