Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between proxies, repositories and services in Doctrine 2?

I am wondering what are the difference between those 3 patterns.

As far as I understand:

  • Proxies are used only for lazy-load entities
  • Repositories are used to add logic to your model (width some shortcut method for DQL statement)
  • Services are used to rely models and controllers

For this last, services, I don't the point behind it with a Doctrine 2 project as repository are not aware of the persistent system behind, because it use the ORM with DBAL.

I read that proxies could be used to add logic to entities, so what's the difference with repositories?

By the way, should my Entities only contain public getter/setter to their attribute and only that? Is it correct?

I'm kind of lost here.

Do you have any concrete example?

like image 376
ÉricP Avatar asked Mar 06 '11 12:03

ÉricP


People also ask

What is a doctrine repository?

A repository in a term used by many ORMs (Object Relational Mappers), doctrine is just one of these. It means the place where our data can be accessed from, a repository of data. This is to distinguish it from a database as a repository does not care how its data is stored.

What is Doctrine proxy?

A Doctrine proxy is just a wrapper that extends an entity class to provide Lazy Loading for it. By default, when you ask the Entity Manager for an entity that is associated with another entity, the associated entity won't be loaded from the database, but wrapped into a proxy object.

What is the difference between entity and repository?

Entity is an object representing (usually) a row in a db. @Repository annotation defines CRUD operation on table. It is very like DAO pattern to fetch and save entities from/to storage - it represents db table.

What is the difference between @service and @component and @repository?

1 @Service is used on the service layer classes of application 2 @Component can be used generically on any class 3 @Repository is used to annotation repository or persistent layer classes.

What is the difference between repository and PDO?

My current understanding of them is below: Repositories are used between the service layer and the model layer. For example, in a UserRepository you would create methods that contains the code to read/write from the database. In PHP, PDO would be used, or an ORM, within the repo methods.

What is a repository and why do you need one?

The same is true for repositories: repositories are built for the specific kind of assets they are meant to hold and the modes of interaction they must support. Databases, file systems, and content management systems are all repositories of one sort or another, and they are all tailor-made for their specific requirements.

What are repositories services and actions/controllers?

What are repositories, services and actions/controllers? Repositories: The repository is a gateway between your domain/business layer and a data mapping layer, which is the layer that accesses the database and does the operations. Basically the repository is an abstraction to you database access.


1 Answers

A Proxy is a reference to something.

Suppose you are creating a User and one User has one Group attached. If you don't have Proxy support, you would need to do fetch on DB for the Group just to be able to use the Entity. Here is a possible code:

$group = $em->getRepository('Group')->findOneById(1);

With support to Proxies, you don't need to do the DB lookup. A Proxy is considered a reference to a DB thing, without actually fetching it. Your code:

$group = $em->getReference('Group', 1);

The benefit of having Proxy is simple... if you need to get (for example) the Group name, just do it normally: $group->getName(); And Proxy will initialize the Entity (DB fetch on-demand).

A Repository is a data manipulation thing. You use it to manage your Entities. So you can findById, findOneByEmail, etc. You can also extend its basic functionality and implement your own methods that manage your Entities, like: saveUser, retrieveMostActiveUsers.

A Service uses Repositories internally, but it contains more validation and may interact with > 1 Repository. For example, after you successfully save the user, you would like to send him a Congrat email. So inside your createUser method of UserService, you retrieve the NotificationService and dispatch the new registration email.

Is it clear for you now? =)

Cheers,

like image 188
Guilherme Blanco Avatar answered Nov 12 '22 20:11

Guilherme Blanco