我应该在哪里放置接口和实现

时间:2021-08-06 02:17:22

I have Domain assembly and SqlServerRepository assembly.

我有域程序集和SqlServerRepository程序集。

SqlServerRepository assembly has a reference on Domain assembly, Domain assembly doesn't have reference on other assemblies.

SqlServerRepository程序集在Domain程序集上有一个引用,Domain程序集在其他程序集上没有引用。

Domain assembly has interface IRepository (and others for repository), because domain logic works only with interface types and doesn't think about concrete types.

域程序集具有接口IRepository(以及其他存储库),因为域逻辑仅适用于接口类型,并且不考虑具体类型。

SqlServerRepository assembly implements interface IRepository (which is contained in the Domain assembly).

SqlServerRepository程序集实现了接口IRepository(包含在Domain程序集中)。

And it's ok, I can mock IRepository, can use OracleRepository implementation and another...

没关系,我可以模拟IRepository,可以使用OracleRepository实现和另一个...

But I think I have some architecture problem...

但我认为我有一些架构问题......

  1. SqlServerRepository can't work without the Domain assembly
  2. 没有域程序集,SqlServerRepository无法工作
  3. On another hand, if I put interfaces in SqlServerRepository assembly, the Domain will not work without SqlServerRepository assembly, and if I create OracleServerRepository assembly, I must use SqlServerRepository too, because I put the interfaces in the SqlServerRepository and Domain alredy uses them.
  4. 另一方面,如果我将接口放在SqlServerRepository程序集中,那么没有SqlServerRepository程序集就无法使用Domain,如果我创建了OracleServerRepository程序集,我也必须使用SqlServerRepository,因为我将接口放在SqlServerRepository中,而域alredy使用它们。
  5. Where should I map database objects to domain? In the SqlServerRepository or in the Domain assembly?
  6. 我应该在哪里将数据库对象映射到域?在SqlServerRepository中还是在域程序集中?

I thought about an assembly which contains only interfaces, and Domain and SqlServerRepository reference it, but I think it is overhead for my project (microservice)

我想过一个只包含接口的程序集,Domain和SqlServerRepository引用它,但我认为这是我项目的开销(微服务)

So, what must I do?

那么,我该怎么办?

2 个解决方案

#1


4  

The repository interfaces belong in the domain (or rather where your services are, but they are typically part of the domain).

存储库接口属于域(或者更确切地说是您的服务所在,但它们通常是域的一部分)。

The reason is that the repository contracts are created and driven from functionality that the domain need. Without the domain you would not have any repositories.

原因是存储库合同是由域所需的功能创建和驱动的。没有域,您将没有任何存储库。

SqlServerRepository can't work without the Domain assembly

没有域程序集,SqlServerRepository无法工作

Correct. And as I stated above. You would not have a SqlServerRepository if it weren't for the functionality required by the domain.

正确。正如我上面所说的那样。如果不是域所需的功能,则不会有SqlServerRepository。

Where should I map database objects to domain? In the SqlServerRepository or in the Domain assembly?

我应该在哪里将数据库对象映射到域?在SqlServerRepository中还是在域程序集中?

In the repository assembly as it's an implementation detail for the specific data layer abstraction. If you had put them in the domain, the domain would have been forced to have knowledge about implementation details of every persistence layer that you are using (like oracle, sqlserver, files or whatever).

在存储库程序集中,因为它是特定数据层抽象的实现细节。如果您将它们放在域中,那么域将*了解您正在使用的每个持久层的实现细节(如oracle,sqlserver,files或其他)。

Nothing says that SQL Server and Oracle should persist the information using the same database schema. Tables, views etc might vary depending on the pros/cons that the DB engine has.

没有什么说SQL Server和Oracle应该使用相同的数据库模式来保存信息。表,视图等可能会有所不同,具体取决于数据库引擎的优缺点。

#2


0  

I'd suggest you implement things using ports and adapters [Hexagonal architecture]. Following assembly can work for microservices

我建议你使用端口和适配器实现一些东西[六边形架构]。组装后可以用于微服务

  1. Core.Domain : You can put all domain objects here.
  2. Core.Domain:您可以将所有域对象放在此处。
  3. Common.ServiceContracts : It can contain all the service contracts for different services.
  4. Common.ServiceContracts:它可以包含不同服务的所有服务合同。
  5. MyService : It has the service implementation class. It will also have the interfaces for infrastructure. Infrastructure could be database, logging etc. You can expose interface to convert domain to infrastructure objects e.g. IDomain1Mapper.
  6. MyService:它有服务实现类。它还将具有基础设施接口。基础设施可以是数据库,日志等。您可以公开接口以将域转换为基础设施对象,例IDomain1Mapper。
  7. Infrastructure : It can have class like SQLDomain1Mapper, OracleDomain1Mapper and so on. They all will implement IDomain1Mapper. Same is the case for the other domain classes.
  8. 基础设施:它可以有像SQLDomain1Mapper,OracleDomain1Mapper等类。他们都将实现IDomain1Mapper。其他域类的情况也是如此。

MyService assembly will refer to Common.ServiceContracts, Infrastructure and Core.Domain.

MyService程序集将引用Common.ServiceContracts,Infrastructure和Core.Domain。

You can mock any repository/domain in the MyService assembly as per wish. Service consumer can consume MyService assembly.

您可以根据愿望模拟MyService程序集中的任何存储库/域。服务使用者可以使用MyService程序集。

#1


4  

The repository interfaces belong in the domain (or rather where your services are, but they are typically part of the domain).

存储库接口属于域(或者更确切地说是您的服务所在,但它们通常是域的一部分)。

The reason is that the repository contracts are created and driven from functionality that the domain need. Without the domain you would not have any repositories.

原因是存储库合同是由域所需的功能创建和驱动的。没有域,您将没有任何存储库。

SqlServerRepository can't work without the Domain assembly

没有域程序集,SqlServerRepository无法工作

Correct. And as I stated above. You would not have a SqlServerRepository if it weren't for the functionality required by the domain.

正确。正如我上面所说的那样。如果不是域所需的功能,则不会有SqlServerRepository。

Where should I map database objects to domain? In the SqlServerRepository or in the Domain assembly?

我应该在哪里将数据库对象映射到域?在SqlServerRepository中还是在域程序集中?

In the repository assembly as it's an implementation detail for the specific data layer abstraction. If you had put them in the domain, the domain would have been forced to have knowledge about implementation details of every persistence layer that you are using (like oracle, sqlserver, files or whatever).

在存储库程序集中,因为它是特定数据层抽象的实现细节。如果您将它们放在域中,那么域将*了解您正在使用的每个持久层的实现细节(如oracle,sqlserver,files或其他)。

Nothing says that SQL Server and Oracle should persist the information using the same database schema. Tables, views etc might vary depending on the pros/cons that the DB engine has.

没有什么说SQL Server和Oracle应该使用相同的数据库模式来保存信息。表,视图等可能会有所不同,具体取决于数据库引擎的优缺点。

#2


0  

I'd suggest you implement things using ports and adapters [Hexagonal architecture]. Following assembly can work for microservices

我建议你使用端口和适配器实现一些东西[六边形架构]。组装后可以用于微服务

  1. Core.Domain : You can put all domain objects here.
  2. Core.Domain:您可以将所有域对象放在此处。
  3. Common.ServiceContracts : It can contain all the service contracts for different services.
  4. Common.ServiceContracts:它可以包含不同服务的所有服务合同。
  5. MyService : It has the service implementation class. It will also have the interfaces for infrastructure. Infrastructure could be database, logging etc. You can expose interface to convert domain to infrastructure objects e.g. IDomain1Mapper.
  6. MyService:它有服务实现类。它还将具有基础设施接口。基础设施可以是数据库,日志等。您可以公开接口以将域转换为基础设施对象,例IDomain1Mapper。
  7. Infrastructure : It can have class like SQLDomain1Mapper, OracleDomain1Mapper and so on. They all will implement IDomain1Mapper. Same is the case for the other domain classes.
  8. 基础设施:它可以有像SQLDomain1Mapper,OracleDomain1Mapper等类。他们都将实现IDomain1Mapper。其他域类的情况也是如此。

MyService assembly will refer to Common.ServiceContracts, Infrastructure and Core.Domain.

MyService程序集将引用Common.ServiceContracts,Infrastructure和Core.Domain。

You can mock any repository/domain in the MyService assembly as per wish. Service consumer can consume MyService assembly.

您可以根据愿望模拟MyService程序集中的任何存储库/域。服务使用者可以使用MyService程序集。