Helper.Tool.ObjectCreator 1.0.1

dotnet add package Helper.Tool.ObjectCreator --version 1.0.1
NuGet\Install-Package Helper.Tool.ObjectCreator -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Helper.Tool.ObjectCreator" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Helper.Tool.ObjectCreator --version 1.0.1
#r "nuget: Helper.Tool.ObjectCreator, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Helper.Tool.ObjectCreator as a Cake Addin
#addin nuget:?package=Helper.Tool.ObjectCreator&version=1.0.1

// Install Helper.Tool.ObjectCreator as a Cake Tool
#tool nuget:?package=Helper.Tool.ObjectCreator&version=1.0.1

Helper.Tool.ObjectCreator

This library is for backend .Net software developers.

Purpose of Use and Benefits

When used in large and modular .NET Core or .NET 8+ projects, this structure reduces code duplication, simplifies maintenance, and ensures orderly management of dependencies. The fact that dependencies can have different lifecycles and be equipped with special features makes the application development process more flexible and allows features such as performance optimizations to be easily integrated.

Rozetler

shields.io Gibi bir yerden rozetler ekleyin.

MIT License GPLv3 License AGPL License

Helper.Tool.ObjectCreator

This library is for backend .Net software developers.

Purpose of Use and Benefits

When used in large and modular .NET Core or .NET 8+ projects, this structure reduces code duplication, simplifies maintenance, and ensures orderly management of dependencies. The fact that dependencies can have different lifecycles and be equipped with special features makes the application development process more flexible and allows features such as performance optimizations to be easily integrated.

Kullanılan Teknolojiler

SDK: Microsoft.NET.Sdk

TargetFramework: Microsoft.NET.Sdk

Library: Microsoft.Extensions.DependencyInjection.Abstractions

Özellikler

  • Dependency Injection
  • Customized Dependency Management
  • Defining Dependencies with Custom Properties
  • Service Definition and Usage
  • Modular and Easy to Use
  • Integration of Custom Functionalities

Dependency Injection

Thanks to the IServiceCollection and extensionmethods in the code, dependencies are managed.You can determine the life cycles of services with methods such as AddScoped, AddTransient and AddSingleton.

Customized Dependency Management

With the InjectableAttribute class, dependencies, lifecycles and special properties of services and components can be defined. For example, using InjectableAttribute, services such as Lifetime, caching, lazy loading, start and end times can be defined.

Defining Dependencies with Custom Properties

It supports special features like caching and laziness loading thanks to interfaces like ICacheable and ILazyLoad.

Service Definition and Usage

The ServiceCollectionExtensions class enables easy identification of services with various lifecycles. The DependencyHelper class manages and adds dependencies by scanning specially marked classes (for example, classes marked using InjectableAttribute).

Modular and Easy to Use

ScanForAttributeInjection methods allow dependencies to be automatically added by scanning specific types and mounted units (assemblies). In this way, it reduces code repetition and offers a modular structure.

Integration of Custom Functionalities

For example, the CacheableService and LazyLoadService classes specifically support functionality such as caching and lazy loading.

Usage & Examples

Dependency Injection

Adding Scoped Cacheable Service

AddScopedCacheable method is used to add services as Scoped and make them cacheable.

services.AddScopedCacheable<IMyService, MyService>();
Adding Transient LazyLoad Service

The AddTransientLazyLoad method is used to add services as Transients and perform lazy loading.

services.AddTransientLazyLoad<IMyService, MyService>();
Adding Singleton Cacheable Service

The AddSingletonCacheable method is used to add services as Singletons and make them cacheable.

services.AddSingletonCacheable<IMyService, MyService>();
Adding Services by Scanning via Assembly

The ScanForAttributeInjection method is used to automatically add dependencies by scanning classes and marked properties in the assembly.

services.ScanForAttributeInjection(Assembly.GetExecutingAssembly());
Adding Caching and Lazy Loading with Injectable Attribute

Adding caching and lazy loading by flagging services using the Injectable Attribute.

[ObjectCreate(Cache = true, CacheType = CacheType.L2)]
public class MyService : IMyService
{
    // Service Codes
}
Adding Custom Properties with InjectableAttribute

Determining the behavior of services by adding custom properties using InjectableAttribute.

[ObjectCreate(Lifetime = ServiceLifetime.Singleton, Cache = true, CacheType = CacheType.L3)]
public class MySingletonService : IMySingletonService
{
   // Service Codes
}
Registering Services Manually

Adding services manually using the RegisterServices method in the DependencyHelper class.

var services = new ServiceCollection();
services.AddScoped<IMyService, MyService>();
DependencyHelper.RegisterServices(services, Assembly.GetExecutingAssembly());
IObjectCreatorFactory

In the C# code structure given as a base, the IObjectCreatorFactory interface is used to control how a particular service will be created during the dependency injection process. The purpose of IObjectCreatorFactory is to integrate factory pattern logic into dependency injection. In this way, you can flexibly determine how services will be created, which dependencies will be provided, and which lifecycle will be used.

  • IObjectCreatorFactory is an interface that provides service creation functionality in the Dependency Injection process.
  • This interface contains a method called Create, which creates and returns a service instance by taking an IServiceProvider parameter
public interface IObjectCreatorFactory
{
    object Create(IServiceProvider serviceProvider);
}

Using IObjectCreatorFactory
  • IObjectCreatorFactory is often used in conjunction with InjectableAttribute and is used to determine how a service will be created.
  • The Create method enables the creation of the service, thus allowing you to implement customized service creation logic.
public class MyFactory : IObjectCreatorFactory
{
    public object Create(IServiceProvider serviceProvider)
    {
        // Özel servis oluşturma mantığı
        return new MyService();
    }
}
Using IObjectCreatorFactory with InjectableAttribute
  • InjectableAttribute uses IObjectCreatorFactory to determine how a service is created.
  • It is determined how the service will be created by providing an object of type IObjectCreatorFactory through the Factory property.
[ObjectCreate(Factory = typeof(MyFactory))]
public class MyService : IMyService
{
    // Servis kodları
}
Advantages
  • You can make your code cleaner and more organized by moving the service creation logic to a separate class using IObjectCreatorFactory.
  • You can better manage dependencies and apply custom service creation logic based on specific situations.
  • It can also be useful for testability because you can change the service creation logic based on an interface. By using the IObjectCreatorFactory interface in this way, you can more flexibly manage the service creation functionality during the dependency injection process and improve the overall performance and maintainability of your application.

Cache Functionality and Features with InjectableAttribute

Cache Functionality:

The InjectableAttribute provides functionality for caching services, allowing them to be stored in memory for faster access and reduced load times.

Cache Properties:

Cache (boolean): Enables or disables caching for the service. If set to true, the service will be cached. CacheType (enum): Specifies the type of cache to use, such as L1, L2, or L3 caching. ExpirationTimeInMinutes (integer): Sets the expiration time for cached items in minutes. After this time, the cached item will be invalidated and refreshed. StartTimeHour, StartTimeMinute, EndTimeHour, EndTimeMinute (integer): Defines a time window during which cached items are valid. Outside this window, the cache is refreshed.

Cache

[ObjectCreate(Cache = true, CacheType = CacheType.L2)]
public class ProductService : IProductService
{

    public void DoSomething()
    {
        // Servis tarafından sunulan bir işlev
    }
}

[ObjectCreate(
    Lifetime = ServiceLifetime.Singleton,
    Cache = true,
    CacheType = CacheType.L1,
    ExpirationTimeInMinutes = 60, // 60 minute
    StartTimeHour = 0,
    StartTimeMinute = 0,
    EndTimeHour = 0,
    EndTimeMinute = 0
)]
public class ProductService : IProductService
{
    public ProductService()
    {
    }

    public void AddProduct(Product product)
    {
       
    }

    public Product GetProduct(int productId)
    {
        return null;
    }
}
[ObjectCreate(
    Lifetime = ServiceLifetime.Singleton,
    Cache = true,
    CacheType = CacheType.L2,
    StartTimeHour = 14,
    StartTimeMinute = 0,
    EndTimeHour = 18,
    EndTimeMinute = 0
)]
public class ProductService : IProductService
{
    public ProductService()
    {
    }

    public void AddProduct(Product product)
    {
       
    }

    public Product GetProduct(int productId)
    {
        return null;
    }
}

LazyLoad

[ObjectCreate(LazyLoad = true)]
public class ProductService : IProductService
{
    public ProductService()
    {
        // Servis başlatma kodları
    }

    public void DoSomething()
    {
        // Servis işlevleri
    }
}

Register services

var services = new ServiceCollection();

  • 1 Yöntem: Servisi doğrudan eklemek & Way 1: Adding the service directly
services.AddSingleton<IMyService, MyService>();
  • 2 Yöntem: IServiceCollection metodları ile eklemek & Way 2: Adding the service directly
services.AddScopedCacheable<IMyService, MyService>();
services.AddTransientLazyLoad<IMyService, MyService>();
services.AddSingletonCacheable<IMyService, MyService>();
  • 3 Yöntem: DependencyHelper kullanarak eklemek & Way 3: Adding the service directly
DependencyHelper.RegisterServices(services, Assembly.GetExecutingAssembly());
  • 4.01 Yöntem: Attirubute Scanning & Way 4.01:
// Önce bir IServiceCollection nesnesi oluşturulur
var services = new ServiceCollection();

// Daha sonra DependencyHelper sınıfı kullanılarak tarama yapılır ve kayıtlar otomatik olarak eklenir
services.ScanForAttributeInjection(Assembly.GetExecutingAssembly());

// Şimdi IServiceCollection nesnesi üzerinde tanımlı servisleri kullanabilirsiniz
var serviceProvider = services.BuildServiceProvider();
var myService = serviceProvider.GetService<IMyService>();
  • 4.02 Yöntem: Attirubute Scanning & Way 4.02:
services.ScanForAttributeInjection(
    typeof(MyNamespace.MyClass).Assembly, // MyClass sınıfının bulunduğu assembly
    typeof(OtherNamespace.OtherClass).Assembly // OtherClass sınıfının bulunduğu assembly
);

Asp.Net Core WebAPI

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Reflection;

namespace MyWebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            // Dependency Injection için servislerin tarama işlemi
           
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            services.ScanForAttributeInjection(Assembly.GetExecutingAssembly());
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            


            // Web API servislerini eklemek
            services.AddControllers();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

Created This Library : Özgür ATILGAN

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 110 3/23/2024
1.0.0 68 3/23/2024

This is a advanced tool