| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="MetadataRegistry.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Concurrent; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Metadata; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides a central registry for managing metadata associated with types. This static class allows for the configurat |
| | | 15 | | /// </summary> |
| | | 16 | | public static class MetadataRegistry |
| | | 17 | | { |
| | 12 | 18 | | private static readonly ConcurrentDictionary<Type, TypeMetadata> Cache = []; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Configures metadata for a specific type. This method takes a generic type parameter T, which represents the type |
| | | 22 | | /// </summary> |
| | | 23 | | /// <typeparam name="T">The type for which metadata is being configured.</typeparam> |
| | | 24 | | /// <returns>A <see cref="MetadataBuilder{T}"/> that can be used to further configure the metadata for the specified |
| | | 25 | | public static MetadataBuilder<T> For<T>() |
| | | 26 | | { |
| | 30 | 27 | | var metadata = Get(typeof(T)); |
| | 30 | 28 | | return new(metadata); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Retrieves the metadata associated with a given type. This method takes a Type object as a parameter and returns |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="type">The type for which metadata is being retrieved.</param> |
| | | 35 | | /// <returns>The <see cref="TypeMetadata"/> associated with the specified type.</returns> |
| | | 36 | | /// <exception cref="KeyNotFoundException">Thrown if there is no metadata associated with the specified type in the |
| | | 37 | | public static TypeMetadata Get(Type type) |
| | | 38 | | { |
| | 12246 | 39 | | ArgumentNullException.ThrowIfNull(type); |
| | | 40 | | |
| | 12246 | 41 | | GeneratedMetadataBootstrapInvoker.Ensure(type); |
| | | 42 | | |
| | 12246 | 43 | | return GetOrCreate(type); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Retrieves the metadata associated with a given type, or creates a new instance if it does not exist. This method |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="type">The type for which metadata is being retrieved or created.</param> |
| | | 50 | | /// <returns>The <see cref="TypeMetadata"/> associated with the specified type.</returns> |
| | 12246 | 51 | | private static TypeMetadata GetOrCreate(Type type) => Cache.GetOrAdd(type, static _ => new()); |
| | | 52 | | } |
| | | 53 | | |