Where to put DQL's? - zend-framework

Reference To This Question
Where one should put the DQL queries? In a service class, or in a controller or maybe in a repository class?
Found a nice article about this, which answers my question. I think it's best to put them in the service class.
How to integrate Doctrine 2 with your Zend Framework application

This highly depends on what your DQLs are doing:
If you have a Query which is doing work on only one entity type I suggest to create your own Repository class for this entity. The repository class already provides you with the methods for find and findAll, so it would fit there good.
Doctrine gives you orm:generate-repositories as CLI tool. Ralph Schindler took this approach as you can see in his example repository.
If you have a Query which affects multiply types of entities, the Service Layer should be the best place to put it.

Related

Repository and IoC Patterns

Previously I asked this question and on a answer I got this comment:
This works, however injecting the container to a part, as far as I know, is not a "normal" use-case of MEF.
In my web app I have a few repositories that, of course, retrieve entities from the DB. To make them as loosely coupled as possible I'm making them return interfaces (eg IUser, IBill, IBlaBlaBla...) and the repository project only references the library project (that contains the interfaces). I use MEF composition capabilities to tie it all up...
Since the repository must have a concrete object to fill with info it got from the DB and the only the Container is aware of which concrete class maps to a specific interface I think that the repository MUST have reference to the container so it can call the "Resolve()", get a new instance and do his job, but that apparently is a mistake.
Can anyone tell me why and what approach would be better?
PS: I don't know if it's relevant but I'm using DDD...
I think the flaw in your design that lead to this problem is the use of interfaces to hide entities behind. Since entities are your core concept in your domain, there should be no use in hiding them behind an abstraction. Let's put it differently: do you ever have a different implementation of IUser?
In other words, ditch the IUser, IBill, etc. interface and let your repositories and business commands depend directly on your aggregate entities.

Entity Framework 4 with Generic Repository

i developed an sample application using Entity Framework 4.0. but now i'm gonne developed a real app for a company. where i would like to implement the DAL with Entity framework with generic repository , unit of work and DI container.
please any one suggest me a real example for the framework..
Thanks
Rusho
Generic repository is nonsense. If you want to use design pattern called Repository you should think about specific repository and aggregate roots.
Generic repository is just a wrapper around ObjectSet / DbSet providing no added value - only additional layer which must be maintained and which makes interaction with EF harder. Also adding repository without clarifying why you want to do that and what it should solve for you is wrong approach - design pattern is a blue print for solving a problem. Not something you should use just because it exists and everybody talks about it.
You can also check these answers where I discuss generic repository and its implications:
Generic Repository With EF 4.1 what is the point
The repository itself is not usually tested?

Design Pattern to use with Entity Framework Code First

I am working on creating a Technical Design Document for my new project that is being developed using Entity Framework 4.1 Code First.
I want my DAL to be to be loosely coupled, easily testable and should be able to dynamically inject it using IoC Container...
Thinking of using Repository Pattern. Are there any good resources for reference?
--
Preetham Reddy
See these tutorials:
http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
http://www.asp.net/entity-framework/tutorials/using-the-entity-framework-and-the-objectdatasource-control-part-2-adding-a-business-logic-layer-and-unit-tests
This is a pretty good post.
http://blogs.msdn.com/b/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx

ASP.NET MVC 2 Where to put logic

I have an ASP.NET MVC 2 application with some complex business rules and I'm trying to decide where to put specific logic.
The logic happens when creating records, based on certain fields of that record other records need to be created.
I'm currently using the repository pattern with an ORM and the easiest place to put this logic would be in my repository class but I feel like this is a pretty feeble location to have important rules, I would put it directly in my partial model classes that have my validation and metadata but I then have to call methods within my controller or repository and that may be extending too much knowledge about implementation to those layers.
What are your best practice tips for me?
Thanks!
You could have a service layer between the controller and the repositories. The repository performs simple CRUD operations with your model. A service method could make use of multiple simple repository calls to compose a business operation. This business operation will be exposed to the controller.

Simplest model code

I am very experienced with the CakePHP framework but am checking out the Zend Framework for an application that will receive massive traffic.
I'm going through the quickstart tutorial in the documentation and got to the "Create a Model and Database Table" page.
Must I or should I create all those model classes it mentions, i.e.
application/models/DbTable/Guestbook.php
application/models/GuestbookMapper.php
application/models/Guestbook.php
Coming from CakePHP it seems like quite a lot of code for some functionality I would of thought of as quite basic and generic.
Or can I just create application/models/Guestbook.php and have it extend Zend_Db_Table_Abstract?
Any help would be much appreciated.
You can create your model after the DbTable class, however one of the benefits of doing a dataMapper class between your model and your DbTable class is that you can abstract more the data engine and create strong business rules.
Zend Framework does not actually impose any restrictions or requirements on your Model classes and you are free to create them however you would like.
Depending on the requirements and scope of the project I generally still go with subclassing Zend_Db_Table_Abstract (often with my own custom extension of it as well). When it comes to a large or complicated project I have found that using a dataMapper pattern has been very helpful.
At the same time I have had some models that do not extend any class at all. They simply are there to provide some logic and do not relate to a database.