diff --git a/Telegrator/Attributes/Components/UpdateHandlerAttributeBase.cs b/Telegrator/Attributes/Components/UpdateHandlerAttributeBase.cs index bdfd8e6..ed49421 100644 --- a/Telegrator/Attributes/Components/UpdateHandlerAttributeBase.cs +++ b/Telegrator/Attributes/Components/UpdateHandlerAttributeBase.cs @@ -36,7 +36,7 @@ namespace Telegrator.Attributes.Components public int Priority { get; set; } /// - /// Gets or sets a value indicating whether to form a fallback report for debugging purposes. + /// Gets or sets a value indicating whether to form a fallback report. /// public bool FormReport { get; set; } diff --git a/Telegrator/MadiatorCore/Descriptors/HandlerDescriptor.cs b/Telegrator/MadiatorCore/Descriptors/HandlerDescriptor.cs index 63b1321..c2cf494 100644 --- a/Telegrator/MadiatorCore/Descriptors/HandlerDescriptor.cs +++ b/Telegrator/MadiatorCore/Descriptors/HandlerDescriptor.cs @@ -20,12 +20,12 @@ namespace Telegrator.MadiatorCore.Descriptors /// Keyed handler descriptor (uses a service key). /// Keyed, - + /// /// Implicit handler descriptor. /// Implicit, - + /// /// Singleton handler descriptor (single instance). /// @@ -74,7 +74,7 @@ namespace Telegrator.MadiatorCore.Descriptors } /// - /// Gets or sets a value indicating whether to form a fallback report for debugging purposes. + /// Gets or sets a value indicating whether to form a fallback report. /// public bool FormReport { @@ -129,8 +129,7 @@ namespace Telegrator.MadiatorCore.Descriptors } /// - /// Gets or sets the display string for this handler descriptor. - /// Used for debugging and logging purposes. + /// Display string for the handler (for debugging or logging). /// public string? DisplayString { @@ -139,8 +138,7 @@ namespace Telegrator.MadiatorCore.Descriptors } /// - /// Gets or sets the lazy initialization action for this handler. - /// Called when the handler instance needs to be initialized. + /// Gets or sets a function for 'lazy' handlers initialization /// public Action? LazyInitialization { @@ -149,60 +147,56 @@ namespace Telegrator.MadiatorCore.Descriptors } /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class with the specified descriptor type and handler type. + /// Automatically inspects the handler type to extract attributes, filters, and configuration. /// - /// The type of the descriptor. - /// The type of the handler. - /// Whether to skip inspection of the handler type. + /// The type of the descriptor + /// The type of the handler to describe + /// + /// Thrown when the handler type is not compatible with the expected handler type public HandlerDescriptor(DescriptorType descriptorType, Type handlerType, bool dontInspect = false) { Type = descriptorType; HandlerType = handlerType; - Indexer = new DescriptorIndexer(0, 0, 0); + Filters = new DescriptorFiltersSet(null, null, null); - if (!dontInspect) - { - UpdateHandlerAttributeBase? pollingHandlerAttribute = HandlerInspector.GetPollingHandlerAttribute(handlerType); - if (pollingHandlerAttribute != null) - { - UpdateType = pollingHandlerAttribute.Type; - Indexer = pollingHandlerAttribute.GetIndexer(); - } + if (dontInspect) + return; - IFilter[]? filters = HandlerInspector.GetFilterAttributes(handlerType, UpdateType).ToArray(); - IFilter? stateKeepFilter = HandlerInspector.GetStateKeeperAttribute(handlerType); - DescriptorAspectsSet? aspects = HandlerInspector.GetAspects(handlerType); + UpdateHandlerAttributeBase handlerAttribute = HandlerInspector.GetHandlerAttribute(handlerType); + if (handlerAttribute.ExpectingHandlerType != null && !handlerAttribute.ExpectingHandlerType.Contains(handlerType.BaseType)) + throw new ArgumentException(string.Format("This handler attribute cannot be attached to this class. Attribute can be attached on next handlers : {0}", string.Join(", ", handlerAttribute.ExpectingHandlerType.AsEnumerable()))); - if (filters.Length > 0 || stateKeepFilter != null) - { - Filters = new DescriptorFiltersSet(filters ?? [], stateKeepFilter); - } + StateKeeperAttributeBase? stateKeeperAttribute = HandlerInspector.GetStateKeeperAttribute(handlerType); + IFilter[] filters = HandlerInspector.GetFilterAttributes(handlerType, handlerAttribute.Type).ToArray(); - if (aspects != null) - { - Aspects = aspects; - } - } + UpdateType = handlerAttribute.Type; + Indexer = handlerAttribute.GetIndexer(); + FormReport = handlerAttribute.FormReport; + Filters = new DescriptorFiltersSet(handlerAttribute, stateKeeperAttribute, filters); + Aspects = HandlerInspector.GetAspects(handlerType); + DisplayString = HandlerInspector.GetDisplayName(handlerType); } /// - /// Initializes a new instance of the class for keyed handlers. + /// Initializes a new instance of the class as a keyed handler with the specified service key. /// - /// The type of the handler. - /// The service key for the handler. + /// The type of the handler to describe + /// The service key for dependency injection + /// Thrown when is null public HandlerDescriptor(Type handlerType, object serviceKey) : this(DescriptorType.Keyed, handlerType) { - ServiceKey = serviceKey; + ServiceKey = serviceKey ?? throw new ArgumentNullException(nameof(serviceKey)); } /// - /// Initializes a new instance of the class with complete configuration. + /// Initializes a new instance of the class with all basic properties. /// - /// The type of the descriptor. - /// The type of the handler. - /// The update type handled by this handler. - /// The indexer for handler concurrency and priority. - /// The set of filters associated with this handler. + /// The type of the descriptor + /// The type of the handler + /// The type of update this handler processes + /// The indexer for handler concurrency and priority + /// The set of filters associated with this handler public HandlerDescriptor(DescriptorType type, Type handlerType, UpdateType updateType, DescriptorIndexer indexer, DescriptorFiltersSet filters) { Type = type; @@ -213,15 +207,16 @@ namespace Telegrator.MadiatorCore.Descriptors } /// - /// Initializes a new instance of the class for singleton handlers. + /// Initializes a new instance of the class with singleton instance support. /// - /// The type of the descriptor. - /// The type of the handler. - /// The update type handled by this handler. - /// The indexer for handler concurrency and priority. - /// The set of filters associated with this handler. - /// The service key for the handler. - /// The singleton instance of the handler. + /// The type of the descriptor + /// The type of the handler + /// The type of update this handler processes + /// The indexer for handler concurrency and priority + /// The set of filters associated with this handler + /// The service key for dependency injection + /// The singleton instance of the handler + /// Thrown when or is null public HandlerDescriptor(DescriptorType type, Type handlerType, UpdateType updateType, DescriptorIndexer indexer, DescriptorFiltersSet filters, object serviceKey, UpdateHandlerBase singletonInstance) { Type = type; @@ -229,19 +224,20 @@ namespace Telegrator.MadiatorCore.Descriptors UpdateType = updateType; Indexer = indexer; Filters = filters; - ServiceKey = serviceKey; - SingletonInstance = singletonInstance; + ServiceKey = serviceKey ?? throw new ArgumentNullException(nameof(serviceKey)); + SingletonInstance = singletonInstance ?? throw new ArgumentNullException(nameof(singletonInstance)); } /// - /// Initializes a new instance of the class with instance factory. + /// Initializes a new instance of the class with instance factory support. /// - /// The type of the descriptor. - /// The type of the handler. - /// The update type handled by this handler. - /// The indexer for handler concurrency and priority. - /// The set of filters associated with this handler. - /// The factory for creating handler instances. + /// The type of the descriptor + /// The type of the handler + /// The type of update this handler processes + /// The indexer for handler concurrency and priority + /// The set of filters associated with this handler + /// Factory for creating handler instances + /// Thrown when is null public HandlerDescriptor(DescriptorType type, Type handlerType, UpdateType updateType, DescriptorIndexer indexer, DescriptorFiltersSet filters, Func instanceFactory) { Type = type; @@ -249,19 +245,20 @@ namespace Telegrator.MadiatorCore.Descriptors UpdateType = updateType; Indexer = indexer; Filters = filters; - InstanceFactory = instanceFactory; + InstanceFactory = instanceFactory ?? throw new ArgumentNullException(nameof(instanceFactory)); } /// - /// Initializes a new instance of the class with service key and instance factory. + /// Initializes a new instance of the class with service key and instance factory support. /// - /// The type of the descriptor. - /// The type of the handler. - /// The update type handled by this handler. - /// The indexer for handler concurrency and priority. - /// The set of filters associated with this handler. - /// The service key for the handler. - /// The factory for creating handler instances. + /// The type of the descriptor + /// The type of the handler + /// The type of update this handler processes + /// The indexer for handler concurrency and priority + /// The set of filters associated with this handler + /// The service key for dependency injection + /// Factory for creating handler instances + /// Thrown when or is null public HandlerDescriptor(DescriptorType type, Type handlerType, UpdateType updateType, DescriptorIndexer indexer, DescriptorFiltersSet filters, object serviceKey, Func instanceFactory) { Type = type; @@ -269,243 +266,210 @@ namespace Telegrator.MadiatorCore.Descriptors UpdateType = updateType; Indexer = indexer; Filters = filters; - ServiceKey = serviceKey; - InstanceFactory = instanceFactory; + ServiceKey = serviceKey ?? throw new ArgumentNullException(nameof(serviceKey)); + InstanceFactory = instanceFactory ?? throw new ArgumentNullException(nameof(instanceFactory)); } /// - /// Initializes a new instance of the class with polling handler attribute. + /// Initializes a new instance of the class with polling handler attribute and filters. /// - /// The type of the descriptor. - /// The type of the handler. - /// The polling handler attribute. - /// The array of filters associated with this handler. - /// The state keeper filter. + /// The type of the descriptor + /// The type of the handler + /// The polling handler attribute containing configuration + /// Optional array of filters to apply + /// Optional state keeping filter public HandlerDescriptor(DescriptorType type, Type handlerType, UpdateHandlerAttributeBase pollingHandlerAttribute, IFilter[]? filters, IFilter? stateKeepFilter) { Type = type; HandlerType = handlerType; UpdateType = pollingHandlerAttribute.Type; Indexer = pollingHandlerAttribute.GetIndexer(); - FormReport = pollingHandlerAttribute.FormReport; - - if (filters != null || stateKeepFilter != null) - { - Filters = new DescriptorFiltersSet(filters ?? [], stateKeepFilter); - } + Filters = new DescriptorFiltersSet(pollingHandlerAttribute, stateKeepFilter, filters); } /// - /// Initializes a new instance of the class for singleton handlers with polling handler attribute. + /// Initializes a new instance of the class with polling handler attribute, filters, and singleton instance. /// - /// The type of the descriptor. - /// The type of the handler. - /// The polling handler attribute. - /// The array of filters associated with this handler. - /// The state keeper filter. - /// The service key for the handler. - /// The singleton instance of the handler. + /// The type of the descriptor + /// The type of the handler + /// The polling handler attribute containing configuration + /// Optional array of filters to apply + /// Optional state keeping filter + /// The service key for dependency injection + /// The singleton instance of the handler + /// Thrown when or is null public HandlerDescriptor(DescriptorType type, Type handlerType, UpdateHandlerAttributeBase pollingHandlerAttribute, IFilter[]? filters, IFilter? stateKeepFilter, object serviceKey, UpdateHandlerBase singletonInstance) { Type = type; HandlerType = handlerType; UpdateType = pollingHandlerAttribute.Type; Indexer = pollingHandlerAttribute.GetIndexer(); - FormReport = pollingHandlerAttribute.FormReport; - ServiceKey = serviceKey; - SingletonInstance = singletonInstance; - - if (filters != null || stateKeepFilter != null) - { - Filters = new DescriptorFiltersSet(filters ?? [], stateKeepFilter); - } + Filters = new DescriptorFiltersSet(pollingHandlerAttribute, stateKeepFilter, filters); + ServiceKey = serviceKey ?? throw new ArgumentNullException(nameof(serviceKey)); + SingletonInstance = singletonInstance ?? throw new ArgumentNullException(nameof(singletonInstance)); } /// - /// Initializes a new instance of the class with instance factory and polling handler attribute. + /// Initializes a new instance of the class with polling handler attribute, filters, and instance factory. /// - /// The type of the descriptor. - /// The type of the handler. - /// The polling handler attribute. - /// The array of filters associated with this handler. - /// The state keeper filter. - /// The factory for creating handler instances. + /// The type of the descriptor + /// The type of the handler + /// The polling handler attribute containing configuration + /// Optional array of filters to apply + /// Optional state keeping filter + /// Factory for creating handler instances + /// Thrown when is null public HandlerDescriptor(DescriptorType type, Type handlerType, UpdateHandlerAttributeBase pollingHandlerAttribute, IFilter[]? filters, IFilter? stateKeepFilter, Func instanceFactory) { Type = type; HandlerType = handlerType; UpdateType = pollingHandlerAttribute.Type; Indexer = pollingHandlerAttribute.GetIndexer(); - FormReport = pollingHandlerAttribute.FormReport; - InstanceFactory = instanceFactory; - - if (filters != null || stateKeepFilter != null) - { - Filters = new DescriptorFiltersSet(filters ?? [], stateKeepFilter); - } + Filters = new DescriptorFiltersSet(pollingHandlerAttribute, stateKeepFilter, filters); + InstanceFactory = instanceFactory ?? throw new ArgumentNullException(nameof(instanceFactory)); } /// - /// Initializes a new instance of the class with service key, instance factory and polling handler attribute. + /// Initializes a new instance of the class with polling handler attribute, filters, service key, and instance factory. /// - /// The type of the descriptor. - /// The type of the handler. - /// The polling handler attribute. - /// The array of filters associated with this handler. - /// The state keeper filter. - /// The service key for the handler. - /// The factory for creating handler instances. + /// The type of the descriptor + /// The type of the handler + /// The polling handler attribute containing configuration + /// Optional array of filters to apply + /// Optional state keeping filter + /// The service key for dependency injection + /// Factory for creating handler instances + /// Thrown when or is null public HandlerDescriptor(DescriptorType type, Type handlerType, UpdateHandlerAttributeBase pollingHandlerAttribute, IFilter[]? filters, IFilter? stateKeepFilter, object serviceKey, Func instanceFactory) { Type = type; HandlerType = handlerType; UpdateType = pollingHandlerAttribute.Type; Indexer = pollingHandlerAttribute.GetIndexer(); - FormReport = pollingHandlerAttribute.FormReport; - ServiceKey = serviceKey; - InstanceFactory = instanceFactory; - - if (filters != null || stateKeepFilter != null) - { - Filters = new DescriptorFiltersSet(filters ?? [], stateKeepFilter); - } + Filters = new DescriptorFiltersSet(pollingHandlerAttribute, stateKeepFilter, filters); + ServiceKey = serviceKey ?? throw new ArgumentNullException(nameof(serviceKey)); + InstanceFactory = instanceFactory ?? throw new ArgumentNullException(nameof(instanceFactory)); } /// - /// Initializes a new instance of the class with complete configuration. + /// Initializes a new instance of the class with validation filter support. /// - /// The type of the descriptor. - /// The type of the handler. - /// The update type handled by this handler. - /// The indexer for handler concurrency and priority. - /// The validation filter. - /// The array of filters associated with this handler. - /// The state keeper filter. + /// The type of the descriptor + /// The type of the handler + /// The type of update this handler processes + /// The indexer for handler concurrency and priority + /// Optional validation filter + /// Optional array of filters to apply + /// Optional state keeping filter public HandlerDescriptor(DescriptorType type, Type handlerType, UpdateType updateType, DescriptorIndexer indexer, IFilter? validateFilter, IFilter[]? filters, IFilter? stateKeepFilter) { Type = type; HandlerType = handlerType; UpdateType = updateType; Indexer = indexer; - - if (validateFilter != null || filters != null || stateKeepFilter != null) - { - Filters = new DescriptorFiltersSet(filters ?? [], stateKeepFilter, validateFilter); - } + Filters = new DescriptorFiltersSet(validateFilter, stateKeepFilter, filters); } /// - /// Initializes a new instance of the class for singleton handlers with complete configuration. + /// Initializes a new instance of the class with validation filter and singleton instance support. /// - /// The type of the descriptor. - /// The type of the handler. - /// The update type handled by this handler. - /// The indexer for handler concurrency and priority. - /// The validation filter. - /// The array of filters associated with this handler. - /// The state keeper filter. - /// The service key for the handler. - /// The singleton instance of the handler. + /// The type of the descriptor + /// The type of the handler + /// The type of update this handler processes + /// The indexer for handler concurrency and priority + /// Optional validation filter + /// Optional array of filters to apply + /// Optional state keeping filter + /// The service key for dependency injection + /// The singleton instance of the handler + /// Thrown when or is null public HandlerDescriptor(DescriptorType type, Type handlerType, UpdateType updateType, DescriptorIndexer indexer, IFilter? validateFilter, IFilter[]? filters, IFilter? stateKeepFilter, object serviceKey, UpdateHandlerBase singletonInstance) { Type = type; HandlerType = handlerType; UpdateType = updateType; Indexer = indexer; - ServiceKey = serviceKey; - SingletonInstance = singletonInstance; - - if (validateFilter != null || filters != null || stateKeepFilter != null) - { - Filters = new DescriptorFiltersSet(filters ?? [], stateKeepFilter, validateFilter); - } + Filters = new DescriptorFiltersSet(validateFilter, stateKeepFilter, filters); + ServiceKey = serviceKey ?? throw new ArgumentNullException(nameof(serviceKey)); + SingletonInstance = singletonInstance ?? throw new ArgumentNullException(nameof(singletonInstance)); } /// - /// Initializes a new instance of the class with instance factory and complete configuration. + /// Initializes a new instance of the class with validation filter and instance factory support. /// - /// The type of the descriptor. - /// The type of the handler. - /// The update type handled by this handler. - /// The indexer for handler concurrency and priority. - /// The validation filter. - /// The array of filters associated with this handler. - /// The state keeper filter. - /// The factory for creating handler instances. + /// The type of the descriptor + /// The type of the handler + /// The type of update this handler processes + /// The indexer for handler concurrency and priority + /// Optional validation filter + /// Optional array of filters to apply + /// Optional state keeping filter + /// Factory for creating handler instances + /// Thrown when is null public HandlerDescriptor(DescriptorType type, Type handlerType, UpdateType updateType, DescriptorIndexer indexer, IFilter? validateFilter, IFilter[]? filters, IFilter? stateKeepFilter, Func instanceFactory) { Type = type; HandlerType = handlerType; UpdateType = updateType; Indexer = indexer; - InstanceFactory = instanceFactory; - - if (validateFilter != null || filters != null || stateKeepFilter != null) - { - Filters = new DescriptorFiltersSet(filters ?? [], stateKeepFilter, validateFilter); - } + Filters = new DescriptorFiltersSet(validateFilter, stateKeepFilter, filters); + InstanceFactory = instanceFactory ?? throw new ArgumentNullException(nameof(instanceFactory)); } /// - /// Initializes a new instance of the class with service key, instance factory and complete configuration. + /// Initializes a new instance of the class with validation filter, service key, and instance factory support. /// - /// The type of the descriptor. - /// The type of the handler. - /// The update type handled by this handler. - /// The indexer for handler concurrency and priority. - /// The validation filter. - /// The array of filters associated with this handler. - /// The state keeper filter. - /// The service key for the handler. - /// The factory for creating handler instances. + /// The type of the descriptor + /// The type of the handler + /// The type of update this handler processes + /// The indexer for handler concurrency and priority + /// Optional validation filter + /// Optional array of filters to apply + /// Optional state keeping filter + /// The service key for dependency injection + /// Factory for creating handler instances + /// Thrown when or is null public HandlerDescriptor(DescriptorType type, Type handlerType, UpdateType updateType, DescriptorIndexer indexer, IFilter? validateFilter, IFilter[]? filters, IFilter? stateKeepFilter, object serviceKey, Func instanceFactory) { Type = type; HandlerType = handlerType; UpdateType = updateType; Indexer = indexer; - ServiceKey = serviceKey; - InstanceFactory = instanceFactory; - - if (validateFilter != null || filters != null || stateKeepFilter != null) - { - Filters = new DescriptorFiltersSet(filters ?? [], stateKeepFilter, validateFilter); - } + Filters = new DescriptorFiltersSet(validateFilter, stateKeepFilter, filters); + ServiceKey = serviceKey ?? throw new ArgumentNullException(nameof(serviceKey)); + InstanceFactory = instanceFactory ?? throw new ArgumentNullException(nameof(instanceFactory)); } /// - /// Sets the singleton instance for this handler descriptor. + /// Sets singleton instance of this descriptor + /// Throws exception if instance already set /// - /// The singleton instance to set. + /// + /// public void SetInstance(UpdateHandlerBase instance) { - if (Type != DescriptorType.Singleton) - throw new InvalidOperationException("Cannot set instance for non-singleton descriptor"); + if (SingletonInstance != null) + throw new Exception(); SingletonInstance = instance; } /// - /// Attempts to set the singleton instance for this handler descriptor. + /// Tries to set singleton instance of this descriptor /// - /// The singleton instance to set. - /// True if the instance was set successfully; otherwise, false. + /// + /// public bool TrySetInstance(UpdateHandlerBase instance) { - if (Type != DescriptorType.Singleton) + if (SingletonInstance != null) return false; SingletonInstance = instance; return true; } - /// - /// Returns a string representation of this handler descriptor. - /// - /// A string representation of the handler descriptor. + /// public override string ToString() - { - return DisplayString ?? $"{Type} {HandlerType.Name}"; - } + => DisplayString ?? HandlerType.Name; } -} +} \ No newline at end of file