在MVC中设置一个存储库模式 [英] Setting up a repository pattern in MVC
问题描述
据我了解,我想知道Repository模式如何工作,以及如何在自定义MVC模式中实现。存储库是一个简单地从实体类返回数据或将实体类保存到持久层的层。
现在我现在看到这样:
请求进入我的控制器以创建一个用户。只需一个用户名和密码。我的控制器会这样做:
function CreateAction()
{
$ userRepo = new userRepository ();
$ user = new userEntity();
$ user-> setUsername('user');
$ user-> setPassword('123456');
$ userRepo-> create($ user);
}
然后我的 userRepository 类看起来像这样: / p>
class userRepository
{
public function create(User $ user)
{
$ this-> db-> exec(INSERT INTO ... QUERY TO SAVE THE USER);
}
}
我的 userEntity 类看起来像这样:
class userEntity
{
private $ username;
私人$密码;
public function setUsername($ username)
{
$ this-> username = $ username;
}
public function getUsername()
{
return $ this-> username;
}
public function setPassword($ password)
{
$ this-> password = $ password;
}
public function getPassword()
{
return $ this-> password;
}
}
现在我觉得第一件事情是错误的我在仓库类中使用查询。我在哪里实际保存userEntity类到数据库?那么换句话说,我在哪里执行实际的SQL查询?我想,正确的方法是在存储库的创建方法中调用DAO。但是我仍然试图弄清楚一个DAO真正的外观,以及与MVC模式中的Model的Model的区别。
但是除此之外,这是实现存储库模式的正确方法吗?
您的存储库看起来更像是TableDataGateway对我来说。存储库的想法是在域对象和数据库之间进行中介的映射层之上的另一层。它还可用作域对象的内存存储(您的示例中缺少的内容),并可能封装了用于创建新实体的工厂。他们通常还允许通过规范模式查询Repository:
这是一个相当复杂的模式。您可以在
还要检查良好的域驱动设计示例
I'm trying to figure out how the Repository pattern works and how it can be implemented in a custom MVC pattern.
As far as i understand it, the Repository is a layer which simply returns data from an entity class or saves the entity class to a persistent layer.
Now i currently see it like this:
A request comes in into my controller to create a user. Just a username and password. My controller will do something like this:
function CreateAction ( )
{
$userRepo = new userRepository ( );
$user = new userEntity ( );
$user->setUsername('user');
$user->setPassword('123456');
$userRepo->create($user);
}
Then my userRepository class looks like this:
class userRepository
{
public function create ( User $user )
{
$this->db->exec ( "INSERT INTO ... QUERY TO SAVE THE USER" );
}
}
And my userEntity class looks like this:
class userEntity
{
private $username;
private $password;
public function setUsername ( $username )
{
$this->username = $username;
}
public function getUsername ( )
{
return $this->username;
}
public function setPassword ( $password )
{
$this->password = $password;
}
public function getPassword ( )
{
return $this->password;
}
}
Now the first thing that i think is wrong here is that i'm using a query inside the repository class. Where do i actually save the userEntity class to the database? So in other words, where do i perform the actual SQL queries? I guess the proper way would be to call a DAO inside the 'create' method of the repository. But i'm still trying to figure out how a DAO really looks and how different it is compared to a 'Model' in terms of the Model in a MVC pattern.
But other than that, is this the proper way of implementing the repository pattern??
Your Repository looks much more like a TableDataGateway to me. The idea of a Repository is to be another layer on top of the mapping layer that mediates between the domain objects and the database. It also serves as an in-memory storage of domain objects (something that is missing from your example) and may encapsulate a Factory for creating new Entities. They often also allow for querying the Repository by Specification patterns:
It's a rather complex pattern. You can find good write-ups about Repository in
- Fowler: Patterns of Enterprise Application Architecture
- Evans: Domain Driven Design
- http://thinkddd.com/assets/2/Domain_Driven_Design_-_Step_by_Step.pdf
Also check Good Domain Driven Design samples
这篇关于在MVC中设置一个存储库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!