{"id":697,"date":"2023-11-10T14:37:18","date_gmt":"2023-11-10T20:37:18","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=697"},"modified":"2023-11-10T14:37:18","modified_gmt":"2023-11-10T20:37:18","slug":"addscoped-in-program-cs-explained-with-example","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/addscoped-in-program-cs-explained-with-example\/","title":{"rendered":"AddScoped in Program.cs Explained with example"},"content":{"rendered":"\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#5E69FF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">builder.Services.AddScoped&lt;IProductService, ProductService>();\n            builder.Services.AddScoped&lt;ICategoryService, CategoryService>();<\/code><\/pre><\/div><\/div>\n\n\n\n<p>This code is configuring dependency injection in a .NET application, specifically using the <code>AddScoped<\/code> method. <\/p>\n\n\n\n<p>Exaplanation:<\/p>\n\n\n\n<p><strong><code>AddScoped<\/code> Method:<\/strong><\/p>\n\n\n\n<ul>\n<li><code>AddScoped<\/code> is a method provided by the <code>IServiceCollection<\/code> interface in ASP.NET Core for registering services in the dependency injection (DI) container.<\/li>\n\n\n\n<li>Scoped services are created once per request within the scope of the request. That means, for each HTTP request, a new instance of the service will be created and shared within that request.<\/li>\n<\/ul>\n\n\n\n<p><strong><code>IProductService<\/code> and <code>ProductService<\/code>:<\/strong><\/p>\n\n\n\n<ul>\n<li><code>IProductService<\/code> is an interface that defines the contract for a service that deals with product-related functionality.<\/li>\n\n\n\n<li><code>ProductService<\/code> is the concrete implementation of <code>IProductService<\/code>. It provides the actual implementation for the methods declared in the <code>IProductService<\/code> interface.<\/li>\n<\/ul>\n\n\n\n<p><strong><code>ICategoryService<\/code> and <code>CategoryService<\/code>:<\/strong><\/p>\n\n\n\n<ul>\n<li>Similar to <code>IProductService<\/code> and <code>ProductService<\/code>, <code>ICategoryService<\/code> is an interface defining the contract for a service dealing with category-related functionality.<\/li>\n\n\n\n<li><code>CategoryService<\/code> is the concrete implementation of <code>ICategoryService<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><strong>Registration in the DI Container:<\/strong><\/p>\n\n\n\n<ul>\n<li>These lines of code register the services in the DI container, associating the interfaces with their corresponding concrete implementations.<\/li>\n\n\n\n<li>When a component in the application requests an instance of <code>IProductService<\/code> or <code>ICategoryService<\/code>, the DI container will provide an instance of <code>ProductService<\/code> or <code>CategoryService<\/code>, respectively.<\/li>\n<\/ul>\n\n\n\n<p>Breakdown of the relationships:<\/p>\n\n\n\n<ul>\n<li><code>IProductService<\/code> defines the contract for product-related services.<\/li>\n\n\n\n<li><code>ProductService<\/code> provides the actual implementation for the product-related services.<\/li>\n\n\n\n<li><code>ICategoryService<\/code> defines the contract for category-related services.<\/li>\n\n\n\n<li><code>CategoryService<\/code> provides the actual implementation for the category-related services.<\/li>\n<\/ul>\n\n\n\n<p>Services with <code>AddScoped<\/code> ensure that a new instance of each service is created for each incoming HTTP request, promoting a scoped lifecycle for these services within the context of a request.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This code is configuring dependency injection in a .NET application, specifically using the AddScoped method. Exaplanation: AddScoped Method: IProductService and ProductService: ICategoryService and CategoryService: Registration in the DI Container: Breakdown of the relationships: Services with AddScoped ensure that a new instance of each service is created for each incoming HTTP request, promoting a scoped lifecycle [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":256,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[39,41,51,32,40,111],"tags":[84,134,42,53,23,54,83],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/697"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=697"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":698,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/697\/revisions\/698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/256"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}