.Net 8 Ioc, Dependecy Injection ve KeyedServices

IoC (inversion of control) and dependecy injection are very important at .net and oop. We'll examine how to work it, what does it mean in this post. And we'll look new di feaute from .net 8 and difference between scope, transient, singleton .

IoC (Inversion of Control) Nedir?

Ioc is a design principle. It providers loose coupled. Let's explain with a example. We have a car and we are driving it, the car is completely at our control. According to IoC principle, we take taxi instead ourselves and just we give directions are enough. as a result the design is more flexible and loose coupled. We don''t prefer our object are be tight coopled at business flow.

Dependecy Injection Nedir?

Dependecy Injection is a design pattern used in the software development process. This pattern reduces the dependency of one class on another and is used for management purposes. If we consider the fundamental features of a car as driving and parking, in my business processes, I'll use the car class as a reference. Whichever object I've linked the car to (BMW, Mercedes, etc.), I'll utilize that. I've used a car with driving and parking features from the bigger picture; one might go faster while another parks better, only the performance varies.

.Net 8.0 Dependecy Injection

there is three method since .net core. These are AddScoped, AddTransient and AddSingleton. Added AddKeyedScoped, AddKeyedTransient and AddKeyedSingleton with the release of .net 8. We'll examine how to work and why use it of this methods. Firstly let's create classes.

iki tane de bu interface’den türeyen sınıf olsun

AddScoped ile bağlayalım

.Net create a scope when first request is called. In this case creating FileStorage instance. This instance uses until the request is end.
GetName() returns assigned value
. br>GetName2() returns returns old value.

AddTransient

A new instance is created with each service (FileStorage) call. We see return old values of both GetName() ve GetName2() when we'll test above the code .

AddSingleton

Singleton uygulama ayağa kalktığında sınıfı oluşturur. istek bitse bile bellekte fonksiyon saklanır.

yeni bir request daha ekleyelim

Önce new name ataması yapan isteği gerçekleştirelim. Daha sonra da file/test adresine istek atalım. Sonuç new name olacaktır yani güncellenmiş değeri gösterecektir.

Key ile Dependecy Injection

.net 8 ile gelen yeni özelliklerdir bir interface’i birden çok sınıfa bağlama imkanı sunar. Sadece scope değil aynı şekilde transient ve singleton da kullanılabilir.

Kullanımı:

FromKeyedServices attribute ile kayıt ettiğimiz keyi parametre olarak vererek servisleri çağırabiliriz.

Scope, Transient ve Singleton ne zaman kullanılmalı?

Örneğin ef core kullandığımız düşünelim bir post işlemi başlattık ve context üzerinden bir veri çağırarak transcationı başlattık bu transaction üzerindeki her değişiklik context üzerinde saklanacaktır transaction bitene kadar yani commit işlemi yapılana kadar değişiklikler üzerinde işlem yaparız. Context’i transient olarak kaydettiğimiz durumda aynı istek içinde farklı bir servisde contexti çağırdığımız zaman yeni bir context oluşturacaktır ve contextleri çoklayacaktır. Bu durumda en ideali scope’dur.
İhtiyaçlarınıza göre bu yöntemleri seçebilirsiniz.

Başka bir gönderide görüşmek üzere esen kalın 🙂

You may also like...

Leave a Reply

Your email address will not be published.

English