Configuración
El patrón de opciones es una técnica común en aplicaciones .NET que permite centralizar la configuración en un solo lugar, facilitando la gestión y validación de las opciones de configuración. En CodeDesignPlus.Net.Event.Store
, utilizamos este patrón para ofrecer opciones de configuración estructuradas y fácilmente accesibles.
EventStoreOptions
La clase EventStoreOptions
se utiliza para configurar y validar las opciones de Event Store en una aplicación .NET. Esta clase hereda de EventSourcingOptions
e implementa la interfaz IValidatableObject
namespace CodeDesignPlus.Net.EventStore.Abstractions.Options;
/// <summary>/// Options to setting of the EventStore/// </summary>public class EventStoreOptions : EventSourcingOptions, IValidatableObject{ /// <summary> /// Name of the setions used in the appsettings /// </summary> public static new readonly string Section = "EventStore";
/// <summary> /// Gets or sets the collection of EventStore servers (nodes) to which the application can connect. /// Each server is represented by a key-value pair, where the key is a unique identifier or name for the server, /// and the value contains the server's connection details. /// </summary> /// <value> /// A dictionary of servers where the key is a unique identifier or name for the server, /// and the value contains the server's connection details. /// </value> public Dictionary<string, Server> Servers { get; set; } = [];
/// <summary> /// Determines whether the specified object is valid. /// </summary> /// <param name="validationContext">The validation context.</param> /// <returns>A collection that holds failed-validation information.</returns> public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { var validations = new List<ValidationResult>();
if (Servers.Count == 0) validations.Add(new ValidationResult("The collection of EventStore servers (nodes) to which the application can connect is required.", [nameof(this.Servers)]));
foreach (var server in this.Servers.Select(x => x.Value)) Validator.TryValidateObject(server, new ValidationContext(server), validations, true);
return validations; }}
Propiedades
- Section: Nombre de la sección usada en el archivo de configuración appsettings. Es una propiedad estática y de solo lectura.
public static readonly string Section = "EventStore";
- Servers: Colección de servidores de Event Store (nodos) a los que la aplicación puede conectarse. Cada servidor está representado por un par clave-valor, donde la clave es un identificador único o nombre para el servidor, y el valor contiene los detalles de conexión del servidor.
public Dictionary<string, Server> Servers { get; set; } = new Dictionary<string, Server>();
Server
La clase Server
representa los detalles necesarios para conectarse a un servidor o nodo de EventStore.
namespace CodeDesignPlus.Net.EventStore.Abstractions;
/// <summary>/// Represents the details required to connect to an EventStore server or node./// </summary>public class Server{ /// <summary> /// Gets or sets the connection string as a URI that points to the EventStore server or node. /// This is typically used to define the address and port of the EventStore instance. /// </summary> /// <value> /// The URI representing the connection string to the EventStore server. /// </value> [Required] public Uri ConnectionString { get; set; }
/// <summary> /// Gets or sets the user name to use when connecting to the EventStore server. /// </summary> /// <value> /// The user name for the EventStore server connection. /// </value> [Required] public string User { get; set; }
/// <summary> /// Gets or sets the password to use when connecting to the EventStore server. /// </summary> /// <value> /// The password for the EventStore server connection. /// </value> [Required] public string Password { get; set; }}
Propiedades
- ConnectionString: URI que apunta al servidor o nodo de EventStore, típicamente utilizado para definir la dirección y el puerto de la instancia de EventStore.
public Uri ConnectionString { get; set; }
- User: Nombre de usuario para conectarse al servidor de EventStore.
public string User { get; set; }
- Password: Contraseña para conectarse al servidor de EventStore.
public string Password { get; set; }
Ejemplo de configuración
Para configurar las opciones de EventSourcing, agregue la siguiente sección a su archivo appsettings.json
.
{ "EventStore": { "Servers": { "Core": { "ConnectionString": "tcp://localhost:1113", "User": "admin", "Password": "12345678" } }, "SnapshotSuffix": "snapshot", "FrequencySnapshot": 20 }}