我目前有一种情况,用户能够完成一个表单,这可能会导致创建几个不同的模型。例如,他们发布了待售汽车的广告,但在此过程中,他们需要创建新品牌和新车型(因为数据库中尚不存在这些)。
目前,此表单正在“AdvertController”中处理。我现在想将我的数据库交互抽象到一个存储库中。我的问题如下...
- 我应该有一个用于数据库交互的存储库,还是每个模型一个?
- 如果每个模型一个,哪个文件(控制器或存储库)应该处理确定是否需要创建新模型的逻辑。
换句话说,以下哪个工作流程是最佳实践(如果确实如此,则两者之一)?
(假设数据库具有以下关系: Advert m-1 CarModel m-1 Brand)
Form completed -> Advert Controller
AdvertController -> tells CarModel repository to create new CarModel model
CarModel_Respository -> creates new CarModel
AdvertController -> sets CarModel relationship with the newly created brand
AdvertController -> tells Brand repository to create new CarModel model
Brand_Respository -> creates new Brand
AdvertController -> tells Advert repository to create new Advert model
Advert_Repository -> creates new Advert
AdvertController -> sets Advert relationship with CarModel
AdvertController -> displays success message
或者......像这样的东西......
Form completed -> AdvertController.
AdvertController -> sends data to DatabaseRepository
DatabaseRepository -> creates new Brand model
DatabaseRepository -> creates new CarModel model
DatabaseRepository -> sets CarModel brand_id as the newly created brand
DatabaseRepository -> creates new Advert model
DatabaseRepository -> sets Advert car_model_id as newly created CarModel
DatabaseRepository -> Sends success message to AdvertController
AdvertController -> sends success message to user.