Edition 0 Updated to asp. Net core 0


The repository instance lifetime in your IoC container



Yüklə 11,82 Mb.
Pdf görüntüsü
səhifə219/288
tarix12.07.2023
ölçüsü11,82 Mb.
#136458
1   ...   215   216   217   218   219   220   221   222   ...   288
NET-Microservices-Architecture-for-Containerized-NET-Applications

The repository instance lifetime in your IoC container 
In a similar way, repository’s lifetime should usually be set as scoped (InstancePerLifetimeScope in 
Autofac). It could also be transient (InstancePerDependency in Autofac), but your service will be more 
efficient in regards to memory when using the scoped lifetime. 
// Registering a Repository in Autofac IoC container
builder.
RegisterType
() 
.
As
() 
.
InstancePerLifetimeScope
(); 
Using the singleton lifetime for the repository could cause you serious concurrency problems when 
your DbContext is set to scoped (InstancePerLifetimeScope) lifetime (the default lifetimes for a 
DBContext). As long as your service lifetimes for your repositories and your DbContext are both 
Scoped, you’ll avoid these issues.
Additional resources 

Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application
https://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-
4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
 

Jonathan Allen. Implementation Strategies for the Repository Pattern with Entity 
Framework, Dapper, and Chain
https://www.infoq.com/articles/repository-implementation-strategies
 

Cesar de la Torre. Comparing ASP.NET Core IoC container service lifetimes with Autofac 
IoC container instance scopes
https://devblogs.microsoft.com/cesardelatorre/comparing-asp-net-core-ioc-service-life-times-
and-autofac-ioc-instance-scopes/
 


251 
CHAPTER 6 | Tackle Business Complexity in a Microservice with DDD and CQRS Patterns 
Table mapping 
Table mapping identifies the table data to be queried from and saved to the database. Previously you 
saw how domain entities (for example, a product or order domain) can be used to generate a related 
database schema. EF is strongly designed around the concept of 
conventions
. Conventions address 
questions like “What will the name of a table be?” or “What property is the primary key?” Conventions 
are typically based on conventional names. For example, it is typical for the primary key to be a 
property that ends with 
Id

By convention, each entity will be set up to map to a table with the same name as the 
DbSet
property that exposes the entity on the derived context. If no 
DbSet
value 
is provided for the given entity, the class name is used. 

Yüklə 11,82 Mb.

Dostları ilə paylaş:
1   ...   215   216   217   218   219   220   221   222   ...   288




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin