如果存储库调用另一个存储库?还是应该储存库调用一个服务层? [英] Should A repository call another repository? Or should a repository call a service layer?

查看:204
本文介绍了如果存储库调用另一个存储库?还是应该储存库调用一个服务层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何解决这个问题。我有一些数据插入到表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)创建表答:我创建马上为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.

现在这里的问题。表B表中应该只有信息插入到该表,当且仅当它是不等于喜

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

因此​​这将意味着我将不得不缠上了我的表B这样

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();
}

现在我不知道我是否应该在这里做这个,因为这看起来几乎像业务逻辑。但在同一时间,我不知道如何做到这一点的业务逻辑,因为无论哪种方式,我仍然在价值传递给插入,即使它是空的创建方法(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是什么。如果好然后去表B库,并把它呀?

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 - >表B服务层 - > 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.Gender == Gender.Female)保存在这里...)或规范(如果(FemaleSpecification.IsSatisfiedBy(人))保存在这里...)。

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... ).

这篇关于如果存储库调用另一个存储库?还是应该储存库调用一个服务层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆