IInitialize 2.0.0

dotnet add package IInitialize --version 2.0.0
                    
NuGet\Install-Package IInitialize -Version 2.0.0
                    
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="IInitialize" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="IInitialize" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="IInitialize" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add IInitialize --version 2.0.0
                    
#r "nuget: IInitialize, 2.0.0"
                    
#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.
#:package IInitialize@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=IInitialize&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=IInitialize&version=2.0.0
                    
Install as a Cake Tool

Description

IInitialize is an interface that defines a contract for initialization processing. Implementing this interface indicates that an object requires an initialization phase.

This interface provides a single event, Initialize, which is raised when the initialization process is complete.

The Initialize event uses the generic InitializeEventHandler<TSender> delegate, where TSender represents the type of the object that raised the event. Event handlers receive an InitializeEventArgs object, which can contain arguments related to the initialization process, such as information about already initialized properties.

By utilizing this interface, other components or services can asynchronously be notified of the initialization completion of objects implementing IInitialize. This can be useful for scenarios such as starting dependent processes or updating the UI after initialization.

Purpose: To organize large classes and support loose coupling.

Key Elements:

  • IInitialize(Of TSender): The interface to be implemented by objects requiring initialization. TSender specifies the type of the object raising the event.
  • InitializeEventHandler(Of TSender) Delegate: Defines the type for the Initialize event's handlers.
  • Initialize Event: An event that is raised when initialization processing is complete.
  • InitializeEventArgs Class: Provides arguments related to the event, such as information about already initialized properties.

Providing this interface as a library allows you to establish a consistent initialization pattern across your applications, improving code reusability and maintainability.

(Usage Example for C# Developers):

Here's a basic example of how a C# class might implement and use the IInitialize interface, noting that InitializeEventArgs is nullable:

public class MyService : IInitialize<MyService>
{
    public event InitializeEventHandler<MyService> Initialize;

    public void BeginInitialization()
    {
        // Perform initialization tasks here
        Console.WriteLine("MyService is initializing...");
        OnInitialize(null); // InitializeEventArgs can be null
    }

    protected virtual void OnInitialize(InitializeEventArgs? e)
    {
        Initialize?.Invoke(this, e);
    }
}

public class Consumer
{
    public Consumer(MyService service)
    {
        service.Initialize += (sender, e) =>
        {
            Console.WriteLine("MyService has been initialized!");
            // Perform actions that depend on MyService being initialized
        };
    }
}

Important: When using InitializeEventArgs, always check e != null before casting: InitializeEventArgs args = (InitializeEventArgs)e;

While the Initialize event itself is raised synchronously, asynchronous behavior can be achieved by implementing asynchronous operations within the event handlers using async and await.

Explanation of the Differences Between AlreadyInitializedProperty and InitializationState

In the IInitialize library, InitializeEventArgs is used to provide various information related to the initialization process. Among them, AlreadyInitializedProperty and InitializationState serve different purposes.

AlreadyInitializedProperty

  • Purpose: To provide information about properties that were already initialized before the Initialize event occurred.
  • Information: Typically holds the name and value of a property that has already been initialized.
  • Usage: Can be initialized in the InitializeEventArgs constructor or accessed through the ReadOnly AlreadyInitialized property.
  • Image: Used to indicate information that was set up beforehand as a prerequisite for the initialization process. For example, it might be used to convey information about basic settings or initial data that has already been loaded to event handlers.

InitializationState

  • Purpose: To represent the state of individual initialization steps or components obtained during or after the initialization event (Initialize).
  • Information: Can hold whether the initialization was successful (IsSuccessful), an error message if it failed (Description), the name of the related element (Name), and its value (Value).
  • Usage: By adding instances of InitializationState to the State property (a collection of type PropertyInfoCollection(Of InitializationState)) of InitializeEventArgs, you can record the progress and results of the initialization process.
  • Image: Used to track whether the initialization of a specific component or process was successful or failed, and the details (such as error messages).

Summary

Feature AlreadyInitializedProperty InitializationState
Timing Before the initialization event occurs During or after the initialization event process
Main Information Name and value of properties already initialized Success status, description, name, and value of related elements
Access Method InitializeEventArgs constructor, AlreadyInitialized property InitializeEventArgs's State property (adding to the collection)
Main Use Providing prerequisites or context for initialization Recording the results and details of the initialization process

In this way, AlreadyInitializedProperty conveys information that existed before the start of the initialization, and InitializationState is used to manage information obtained during the initialization process.

Release Notes

Version 1.0.0 Release Notes

  • Introduction of the IInitialize(Of TSender) Interface:

    • Introduced the IInitialize(Of TSender) interface, which defines an event related to initialization processing.
    • Implementing this interface indicates that an object requires an initialization phase.
    • Defines the Initialize event of type InitializeEventHandler(Of TSender).
  • Introduction of the InitializeEventArgs Structure:

    • Introduced the InitializeEventArgs structure, which stores event arguments related to initialization processing.
    • This structure is nullable, so pay attention to the nullable operator.
    • Contains a nested structure AlreadyInitializedProperty to store information about already initialized properties.
    • The AlreadyInitializedProperty structure stores the name and value of an already initialized property and has a constructor that takes these as arguments.
    • The InitializeEventArgs structure has an AlreadyInitialized property that gets the collection of already initialized properties.
    • The InitializeEventArgs structure has constructors that accept either an IEnumerable(Of AlreadyInitializedProperty) or a variable number of AlreadyInitializedProperty objects as arguments.
  • Introduction of the InitializeEventHandler(Of TSender) Delegate:

    • Introduced the InitializeEventHandler(Of TSender) delegate, which represents a handler for events related to initialization processing.
    • Takes the object that raised the event and an InitializeEventArgs object containing arguments related to the event as parameters.

Version 1.0.1 Release Notes

  • The author has been changed.

Version 1.0.2 Release Notes

  • Constructor Added to InitializeEventArgs Structure:
    • Added a new constructor to the InitializeEventArgs structure that accepts a variable number of AlreadyInitializedProperty objects as arguments.
    • This allows for easier creation of InitializeEventArgs instances by individually specifying already initialized properties.

Example:

InitializeEventArgs args = new InitializeEventArgs(
    new AlreadyInitializedProperty("Property1", 1),
    new AlreadyInitializedProperty("Property2", "value")
);

Release Notes - Version 2.0.0

This release introduces the InitializeEventArgs structure for storing event arguments related to initialization processing, along with the associated collection class PropertyInfoCollection, and the InitializationState class for managing the state of initialization.

Main Changes

  • InitializeEventArgs Structure

    • Stores event arguments related to initialization processing.
    • Has a State property (PropertyInfoCollection(Of InitializationState)) to manage the state of the initialization process.
    • Has an AlreadyInitialized property (PropertyInfoCollection(Of AlreadyInitializedProperty)) to store a collection of properties that have already been initialized.
    • Provides constructors that accept either an IEnumerable(Of AlreadyInitializedProperty) or a ParamArray of AlreadyInitializedProperty objects.
  • PropertyInfoCollection(Of T) Class

    • Represents a generic collection of unique elements.
    • Uses HashSet(Of T) internally for fast addition and retrieval of elements.
    • Provides Add method, Contains method, and Count property.
    • Provides a constructor that accepts an IEnumerable(Of T) to initialize the collection.
  • InitializationState Class

    • Stores the state of the initialization process (success/failure, description, etc.).
    • Has IsSuccessful property (indicating whether initialization was successful) and Description property (describing the state).
    • Has Name property and Value property (through implementation of the IPropertyInfo interface).
    • Provides a constructor that accepts Name, Value, IsSuccessful, and Description as arguments.

Breaking Changes

  • AlreadyInitializedProperty Structure
    • The AlreadyInitializedProperty structure, which was previously nested within the InitializeEventArgs structure, has been moved to be a standalone, top-level structure in this release. Code that used the nested AlreadyInitializedProperty in previous versions may require updates.

These changes allow for more structured management of the initialization process state and information about already initialized properties.

Product Compatible and additional computed target framework versions.
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.7.2

    • No dependencies.

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
2.0.0 136 3/28/2025
1.0.3 127 3/28/2025
1.0.2 133 3/28/2025
1.0.1 132 3/27/2025
1.0.0 130 3/27/2025

Initial release of the IInitialize interface and related types.