268
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns
•
Inversion of Control Containers and the Dependency Injection pattern
https://martinfowler.com/articles/injection.html
•
Steve Smith. New is Glue
https://ardalis.com/new-is-glue
Implement the microservice application layer using
the Web API
Use Dependency Injection to inject infrastructure objects into your
application layer
As mentioned previously, the application layer can be implemented as part of the artifact (assembly)
you are building, such as within a Web API project or an MVC web app project.
In the case of a
microservice built with ASP.NET Core, the application layer will usually be your Web API library. If you
want to separate what is coming from ASP.NET Core (its infrastructure plus your controllers) from your
custom application layer code, you could also place your application layer in a
separate class library,
but that is optional.
For instance, the application layer code of the ordering microservice is directly implemented as part of
the
Ordering.API
project (an ASP.NET Core Web API project), as shown in Figure 7-23.
Figure 7-23. The application layer in the Ordering.API ASP.NET Core Web API project
ASP.NET Core includes
a simple
built-in IoC container
(represented by the IServiceProvider interface)
that supports constructor injection by default, and ASP.NET makes certain services available through
DI. ASP.NET Core uses
the term
service
for any of the types you register that will be injected through
DI. You configure the built-
in container’s services in your application’s
Program.cs
file. Your
269
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns
dependencies are implemented in the services that a type needs and that you register in the IoC
container.
Typically, you want to inject dependencies that implement infrastructure objects.
A typical
dependency to inject is a repository. But you could inject any other infrastructure dependency that
you may have. For simpler implementations, you could directly inject your Unit
of Work pattern object
(the EF DbContext object), because the DBContext is also the implementation of your infrastructure
persistence objects.
In the following example, you can see how .NET is injecting the required repository
objects through
the constructor. The class is a command handler, which will get covered in the next section.
Dostları ilə paylaş: