• Avalanche
  • Avalanche.Core
  • Lexical

    Show / Hide Table of Contents
    • Avalanche.Localization
      • Introduction
      • Localization
      • LocalizationFile
      • LocalizationFiles
      • LocalizationFileSystem
      • LocalizationFileFormat
      • LocalizationLine
      • LocalizationLines
      • TemplateFormat
      • CultureProvider
      • FallbackCultureProvider
      • ResourceManager
      • LocalizationError
      • Microsoft.Extensions
        • Introduction
        • DependencyInjection
        • FileProvider
        • Logging
        • ITextLocalizer
        • IFileLocalizer
        • Localization
        • AspNetCore
      • Pluralization
        • Introduction
        • Multiple plural parameters
        • Custom PluralRules
        • Invariant Culture
        • Unit Prefix
        • IPluralRule
        • IPluralNumber
        • IPluralRules
        • CLDRs
        • Unicode.CLDR41
        • Unicode.CLDR40
      • Articles
        • Alphabet localization
        • Benchmarks
        • Caching
        • Class Library
        • Demo
        • Diagnostics
        • Embedded resources
        • Emplacement
        • File localization
        • Text localization
    • Avalanche.Message
      • Introduction
      • IMessage
      • IMessageProvider
      • IMessageDescription
      • IMessageDescriptions
      • MessageLevel
      • Message printing
      • Messages and Exceptions
      • Logging
      • Validation
      • Localization
    • Avalanche.StatusCode
      • Introduction
      • HResult
        • Introduction
        • HResult.Facilities
        • BasicMessages
        • RpcMessages
        • DispatchMessages
        • ItfMessages
        • Win32Messages
        • ClrMessages
      • System
        • Introduction
        • AccessControlMessages
        • AggregateMessages
        • AppDomainMessages
        • ArgumentMessages
        • ArgumentNullMessages
        • ArgumentOutOfRangeMessages
        • ArithmeticMessages
        • ArrayMessages
        • AssemblyMessages
        • BadImageFormatMessages
        • CodeContractMessages
        • CodePageMessages
        • CollectionsMessages
        • CompilerServiceMessages
        • CryptographyMessages
        • CultureMessages
        • DiagnosticsMessages
        • EventSourceMessages
        • ExecutionEngineMessages
        • FormatMessages
        • HostProtectionMessages
        • IOMessages
        • IndexOutOfRangeMessages
        • InteropServiceMessages
        • InvalidCastMessages
        • InvalidOperationMessages
        • IsolatedStorageMessages
        • LazyMessages
        • MarshalerMessages
        • MemoryMessages
        • MiscellaneousMessages
        • NotImplementedMessages
        • NotSupportedMessages
        • ObjectDisposedMessages
        • OperationCanceledMessages
        • OverflowMessages
        • PlatformMessages
        • PolicyMessages
        • PrincipalMessages
        • ProgramMessages
        • ReferenceMessages
        • ReflectionMessages
        • RegionMessages
        • RemotingMessages
        • ResourcesMessages
        • SecurityMessages
        • SerializationMessages
        • StackMessages
        • TaskMessages
        • TextMessages
        • ThreadingMessages
        • TimeZoneMessages
        • TypeMessages
        • XmlMessages
    • Avalanche.Template
      • Introduction
      • TemplateFormats
      • ITemplatePrintable
      • ITemplateFormatPrintable
      • ITemplateText
      • ITemplateBreakdown
      • ITemplateFormat
      • ITemplateFormats
      • Extract Arguments
      • Emplacement
    • Avalanche.Tokenizer
      • Introduction
      • IToken
      • ITokenizer
      • Tokenizers
    • Avalanche.Service
      • Introduction
      • Service
        • IService
        • Construction
        • Query
        • CancellationToken
        • CachePolicy
        • Cast
        • Scope
        • Dispose
      • Handler
        • Introduction
        • IHandler
        • Cancel
        • Recursion
        • Cyclicity
        • Delegates
        • Casting
        • Invokable
        • Handlers
      • Request
        • Introduction
        • IRequest
        • [Request]
        • [ContextParameter]
        • Print Tree
      • Dependency Injection
        • Introduction
        • ServiceRequest<T>
        • Handler
        • CachePolicy
        • CancellationToken
        • QueryLogger
      • Examples
        • Expression Example
        • Mapper Example
      • ServiceMessages
    • Avalanche.Writer
      • Introduction
      • Writer
      • Referer
      • TypeCast
      • DefaultConstructor
      • DelegateWriter
      • WriterPipe
      • ConstantWriter
      • PassthroughWriter
      • Context
      • WriterMessages
    • Avalanche.Accessor
      • Introduction
      • AccessorServices
      • List
        • ListAccessor
        • ListConstructor
        • ListDeconstructor
        • ListDefaultConstructor
        • ListGet
        • ListSet
        • ListAdd
        • ListInsert
        • ListClear
        • ListCount
        • ListIndexOf
        • ListRemoveAt
        • ListReferer
      • Map
        • MapAccessor
        • MapConstructor
        • MapDeconstructor
        • MapDefaultConstructor
        • MapSet
        • MapGet
        • MapRemove
        • MapClear
        • MapCount
        • MapReferer
      • Record
        • RecordAccessor
        • RecordConstructor
        • RecordDeconstructor
        • RecordDefaultConstructor
      • Field
        • FieldAccessor
        • FieldReader
        • FieldWriter
        • FieldReferer
      • Dependency Injection
        • Introduction
      • AccessorMessages
    • Avalanche.Emit
      • Introduction
      • TypeBuilder
      • ConstructorBuilder
      • MethodBuilder
      • PropertyBuilder
      • FieldBuilder
      • Emit
      • Utilities
    • Avalanche.DataType
      • Introduction
      • IDataTypeBase
      • IDataType
      • IRecordType
      • IFieldType
      • IUnionType
      • IListType
      • IMapType
      • IValueType
      • IEnumerationType
      • IIntegerType
      • IRealType
      • IStringType
      • IAnyType
      • DataTypeRequest
      • PrintTree
      • DataTypeMessages
    • Avalanche.FileSystem
      • Introduction
      • Abstractions
        • IFileSystem
          • IFileSystemBrowse
          • IFileSystemCreateDirectory
          • IFileSystemDelete
          • IFileSystemFileAttribute
          • IFileSystemMount
          • IFileSystemMove
          • IFileSystemObserve
          • IFileSystemOpen
        • IEvent
        • IEntry
        • IOption
        • IToken
      • FileSystem
      • VirtualFileSystem
      • MemoryFileSystem
      • EmbeddedFileSystem
      • HttpFileSystem
      • Decoration
      • IFileProvider
      • Events
      • Utilities
        • Dispose
        • File Scanner
        • Visit Tree
        • File Operation
    • Avalanche.Identity
      • Introduction
      • Identity
      • IdentityParts
      • IdentityInterner
      • IdentityComparer
      • Print Tree
      • IdentityAccessors
        • Introduction
        • TypeName
    • Avalanche.Utilities
      • Introduction
      • Collections
        • Tuples
        • StructList
        • ArrayList
        • BijectionMap
        • LocakableDictionary
        • LockableList
        • MapList
        • Pipe
        • RingQueue
        • EnumerableExtensions
        • TupleUtilities
        • ArrayUtilities
      • Comparers
        • IGraphComparer
        • IGraphComparable
        • AlphaNumericComparer
        • EnumerableComparer
        • EnumerableGraphComparer
        • ReferenceComparer
        • KeyValuePairComparer
        • DefaultComparerProvider
        • RecordComparer
      • Cloners
        • ICloner
        • IGraphCloner
        • IGraphCloneable
        • EnumerableCloner
        • FieldCloner
        • PassthroughCloner
        • RecordCloner
        • ClonerProvider
      • Dispose
        • IDisposeAttachable
        • IDisposeBelatable
      • Provider
        • Introduction
        • ProviderBase
        • Delegate
        • Concatenation
        • Cache
        • ResultCapture
        • AsReadOnly
        • AsService
      • Record
        • IRecordDescription
        • IFieldDescription
        • IConstructorDescription
        • IConstructionDescription
        • IParameterDescription
        • IRecordProviders
        • RecordDelegates
          • RecordCreate
          • RecordClone
          • RecordCopy
          • IRecordDelegates
        • FieldDelegates
          • FieldRead
          • FieldWrite
          • RecreateWith
          • IFieldDelegates
      • String
        • IEscaper
        • UnicodeString
      • Miscellaneous
        • IIdGenerator
        • Permutation
        • IReadOnly
        • IUserDataContainer
        • Void
    • Avalanche.Core
      • License
      • Order

    Introduction

    Class, struct and record can be used as a request. Request name should be a verb that describes the requested action.

    /// <summary>Request to print record-like object as json-string</summary>
    public record PrintAsJson(Object record);
    

    Requests must be immutable, or used as such.

    record PrintAsJson_
    {
        public readonly object? Record;
    }
    

    If request type is class or struct then Hash-equals must be implemented explicitly.

        /// <summary>Request to print record-like object as json-string</summary>
        public class PrintAsJson
        {
            /// <summary>Record to print</summary>
            public readonly object record;
    
            /// <summary>Create request to print <paramref name="record"/></summary>
            public PrintAsJson(object record) => this.record = record;
    
            /// <summary>Compare to <paramref name="obj"/></summary>
            public override bool Equals(object? obj) => obj is PrintAsJson casted ? casted.record.Equals(this.record) : false;
            /// <summary>Request specific hashcode</summary>
            public override int GetHashCode() => record.GetHashCode() ^ 213;
        }
    }
    

    Hash-equals comparable struct can be used as request. Small stucts are very applicable requests, because they can be created without heap-allocation.

    /// <summary>Request to print record-like object as json-string</summary>
    public struct PrintAsJson
    {
        /// <summary>Record to print</summary>
        public readonly object record;
    
        /// <summary>Create request to print <paramref name="record"/></summary>
        public PrintAsJson(object record) => this.record = record;
    
        /// <summary>Compare to <paramref name="obj"/></summary>
        public override bool Equals(object? obj) => obj is PrintAsJson casted ? casted.record.Equals(this.record) : false;
        /// <summary>Request specific hashcode</summary>
        public override int GetHashCode() => record.GetHashCode() ^ 213;
    }
    

    Any value-type can be used for requesting. They are, however, usually suitable only for narrow-purposed services as the type doesn't indicate the requested verb. The verb must be known from the context.

    // Handler that doubles value
    Action<IQuery<int, int>> h = q => q.Response.SetValue(q.Request * 2);
    // Create service
    IService<int, int> doubleService = Services.Create<int, int>(h);
    // Double value
    int x = doubleService.GetRequired(10);
    

    [IgnoreDataMember] indicates that field or property shouls not be considered as field of request.

    /// <summary>Request to print record-like object as json-string</summary>
    public record PrintAsJson : IRequestNotToBeCached
    {
        /// <summary></summary>
        public object? Record { get; init; }
        /// <summary>Print info</summary>
        [IgnoreDataMember]
        public String Info => $"Prints {Record} as json.";
    }
    

    [DataMember] instructs explicit order and name.

    public record MyRequest : IRequestNotToBeCached
    {
        [DataMember(Name = "FieldName", Order = 1)]
        public String? SomeField { get; init; }
    
        [DataMember(Name = "FieldName2", Order = 2)]
        public object? AnotherField { get; init; }
    }
    

    readonly struct are very efficient request types as they are pointer referenced in sync service calls.

    /// <summary>Request to print record-like object as json-string</summary>
    public readonly record struct PrintAsJson(Object record) : IRequestNotToBeDisposed;
    

    Referable requests should be sealed for most cases. One, it is easier to implement handler when request doesn't have polymorphism. And two, if the request class is used in remote service, its serialization is more efficient.

    /// <summary>Request to print record-like object as json-string</summary>
    public sealed record PrintAsJson(Object record) : IRequestNotToBeDisposed;
    

    Full Example

    Full example
    #pragma warning disable CS0649
    using System;
    using System.Runtime.Serialization;
    using Avalanche.Service;
    
    public class request_index
    {
        public static void Run()
        {
            {
                // <05>
                // Handler that doubles value
                Action<IQuery<int, int>> h = q => q.Response.SetValue(q.Request * 2);
                // Create service
                IService<int, int> doubleService = Services.Create<int, int>(h);
                // Double value
                int x = doubleService.GetRequired(10);
                // </05>
            }
        }
    
        static class _01
        {
            // <01>
            /// <summary>Request to print record-like object as json-string</summary>
            public record PrintAsJson(Object record);
            // </01>
        }
    
    
        // <02>
        record PrintAsJson_
        {
            public readonly object? Record;
        }
        // </02>
    
        static class _03
        {
            // <03>
            /// <summary>Request to print record-like object as json-string</summary>
            public class PrintAsJson
            {
                /// <summary>Record to print</summary>
                public readonly object record;
    
                /// <summary>Create request to print <paramref name="record"/></summary>
                public PrintAsJson(object record) => this.record = record;
    
                /// <summary>Compare to <paramref name="obj"/></summary>
                public override bool Equals(object? obj) => obj is PrintAsJson casted ? casted.record.Equals(this.record) : false;
                /// <summary>Request specific hashcode</summary>
                public override int GetHashCode() => record.GetHashCode() ^ 213;
            }
        }
        // </03>
    
        static class _04
        {
            // <04>
            /// <summary>Request to print record-like object as json-string</summary>
            public struct PrintAsJson
            {
                /// <summary>Record to print</summary>
                public readonly object record;
    
                /// <summary>Create request to print <paramref name="record"/></summary>
                public PrintAsJson(object record) => this.record = record;
    
                /// <summary>Compare to <paramref name="obj"/></summary>
                public override bool Equals(object? obj) => obj is PrintAsJson casted ? casted.record.Equals(this.record) : false;
                /// <summary>Request specific hashcode</summary>
                public override int GetHashCode() => record.GetHashCode() ^ 213;
            }
            // </04>
        }
    
    
        static class _06
        {
            // <06>
            /// <summary>Request to print record-like object as json-string</summary>
            public record PrintAsJson : IRequestNotToBeCached
            {
                /// <summary></summary>
                public object? Record { get; init; }
                /// <summary>Print info</summary>
                [IgnoreDataMember]
                public String Info => $"Prints {Record} as json.";
            }
            // </06>
        }
    
        static class _07
        {
            // <07>
            public record MyRequest : IRequestNotToBeCached
            {
                [DataMember(Name = "FieldName", Order = 1)]
                public String? SomeField { get; init; }
    
                [DataMember(Name = "FieldName2", Order = 2)]
                public object? AnotherField { get; init; }
            }
            // </07>
        }
    
        static class _08
        {
            // <08>
            /// <summary>Request to print record-like object as json-string</summary>
            public readonly record struct PrintAsJson(Object record) : IRequestNotToBeDisposed;
            // </08>
        }
    
        static class _09
        {
            // <09>
            /// <summary>Request to print record-like object as json-string</summary>
            public sealed record PrintAsJson(Object record) : IRequestNotToBeDisposed;
            // </09>
        }
    
    
    }
    #pragma warning restore CS0649
    
    Back to top Copyright © 2022 Toni Kalajainen, contact@avalanche.fi