我正在想办法解决这个问题.我必须将一些数据插入到 2 个表中,我们称它们为表 A 和表 B.
I am trying to figure out how to tackle this problem. I have to insert some data into 2 tables lets call them Table A and Table B.
Table A has these columns
AId<PK>
A1
A2
A3
Table B has
AId<PK>
A1
B2
B3
B4
现在我的第一个问题是另一个存储库是否应该调用另一个存储库?我不认为这会解决我当前的问题,但我只是想知道这个以供将来参考?
Now my first question was should another repository call another repository? I don't think this will solve my current problem but I just want to know this for future reference?
现在开始我的问题.
当我在存储库层 (TableARepository) 中调用 create 来创建表 A 时.我也立即为 tableB 创建了字段.
when I call a create in my repository layer(TableARepository) to Create Table A. I create right away the fields for tableB too.
// linq to sql.
TableA myATable = new TableA();
dbContext.myATable.A1 = "hi"; // all these values would come from parameters.
dbContext.myATable.A2 = "bye";
dbContext.myATable.A3 = "go";
dbContext.myATable.insertOnSubmit(TableA);
dbContext.SubmitChanges();
TableB myBTable = new TableB();
dbContext.myBTable.AId = myATable.AId;
dbContext.myBTable.A1 = myATable.A1;
dbContext.myBTable.B2 = "2";
dbContext.myBTable.B3 = "3";
dbContext.myBTable.B4 = "4";
dbContext.myATable.insertOnSubmit(TableB);
dbContext.SubmitChanges();
所以我认为这很好,我认为我不需要为此或服务层调用 myBTable 存储库(以创建 tableB).
So I think this is fine and I don't think I would need to call myBTable repository(to create tableB) for this or a service layer.
现在问题来了.当且仅当它不等于hi"时,TableB 表应该只将信息插入到该表中.
Now here is the problem. TableB table should only have the information inserted into this table if and only if it is not equal to "hi".
so param1 != "hi" // insert
param1 == "hi" // ignore and only insert table A
所以这意味着我必须像这样包装我的 TableB
so this would mean I would have to wrap my TableB like this
if(param1 != "hi")
{
TableB myBTable = new TableB();
dbContext.myBTable.AId = myATable.AId;
dbContext.myBTable.A1 = myATable.A1;
dbContext.myBTable.B2 = "2";
dbContext.myBTable.B3 = "3";
dbContext.myBTable.B4 = "4";
dbContext.myATable.insertOnSubmit(TableB);
dbContext.SubmitChanges();
}
现在我不确定我是否应该在这里这样做,因为这看起来几乎像业务逻辑.但同时我不确定如何执行此业务逻辑,因为无论哪种方式,我仍然必须传入值以插入到 create 方法中,即使它为空(A1 是可空字段).
Now I am not sure if I should be doing this here since this seems almost like business logic. yet at the same time I am not sure how to do this business logic since either way I still have to pass in the value to insert into the create method even if it is null(A1 is a nullable field).
那么我应该在TableA.Id,A1中调用tableB服务层传递并检查A1是什么.如果好,那么转到TableB存储库并以这种方式插入?
So should I call the tableB service layer pass in the TableA.Id, A1 and check what A1 is. If good then go to the TableB repository and insert it that way?
所以 TableARepostiory -> TableB 服务层 -> TableBRepository(如果发现那个值!= "hi").
So TableARepostiory -> TableB service layer -> TableBRepository(if found that that value != "hi").
所以我不知道该怎么做.
So I am not sure what to do.
不 - 我想不出一个存储库调用另一个存储库或另一个服务的原因.他们唯一关心的应该是持久化您的实体并从数据存储中检索实体.除了底层域之外,他们应该对您的应用程序的大多数方面一无所知.
No - i cannot think of a reason for a repository to call another repository, nor another service. Their only concern should be persisting your entities and retrieving entities from a datastore. They should be ignorant to most aspects of your application except for the underlying domain.
听起来您认为它们应该是每个表的存储库,这是不正确的.每个聚合根应该有一个存储库,该存储库应该负责将数据存储到所有底层相关表.存储库可以有一些关于在何处或如何保存数据的逻辑,但最好尽可能将其封装在公共区域中.
It sounds like you are assuming their should be a repository per table, which is incorrect. There should be a repository per aggregate root, and that repository should take care of storing data to all of the underlying, related tables. It is OK for the repository to have some logic pertaining to where or how to save the data, but it would be best for that to be encapsulated in a common area when possible.
例如,如果您有 person 对象并且需要根据性别保存到不同的表,您可以使用继承( if (person is Woman) save here... )或对象上的属性( if (person.Gender == Gender.Female) 保存在这里...)或规范(如果 (FemaleSpecification.IsSatisfiedBy(person)) 保存在这里...)
For example if you were to have person objects and needed to save to different tables according to gender, you could do this using inheritance ( if (person is Woman) save here... ) or properties on the object ( if (person.Gender == Gender.Female) save here... ) or Specifications ( if (FemaleSpecification.IsSatisfiedBy(person)) save here... ).
这篇关于一个存储库应该调用另一个存储库吗?还是存储库应该调用服务层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!