/// <summary> /// Initializes a new instance of the <see cref="Inventory"/> class. /// </summary> /// <param name="capacity">Capacity of this inventory.</param> /// <param name="inventoryServices">Needed services of this inventory.</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> is too low.</exception> public Inventory(int capacity, AggregatedInventoryServices inventoryServices) { if (capacity < MinimalInventoryCapacity) { throw new ArgumentOutOfRangeException(nameof(capacity), $"The capacity has to be {MinimalInventoryCapacity} or higher"); } this.itemRegistry = inventoryServices.ItemRegistry; this.items = new ConcurrentDictionary <Guid, IItem>(); this.RuntimeId = Guid.NewGuid(); this.UsedCapacity = 0; this.Capacity = capacity; }
/// <summary> /// Initializes a new instance of the <see cref="ItemFactory"/> class. /// </summary> /// <param name="registry">Needed registry-service reference.</param> /// <param name="serviceProvider">Needed serviceprovider reference.</param> public ItemFactory(IItemRegistry registry, IServiceProvider serviceProvider) { this.registry = registry; this.serviceProvider = serviceProvider; }
/// <summary> /// Initializes a new instance of the <see cref="AggregatedInventoryServices"/> class. /// </summary> /// <param name="itemRegistry">Non-null instance of <see cref="IItemRegistry"/>.</param> public AggregatedInventoryServices(IItemRegistry itemRegistry) { this.ItemRegistry = itemRegistry; }
/// <summary> /// Reads the given <paramref name="itemRegistry"/> and registers all <see cref="ItemMeta"/> instances to the <see cref="IServiceCollection"/>. /// </summary> /// <param name="serviceCollection"><see cref="IServiceCollection"/> to register all <see cref="ItemMeta"/> to.</param> /// <param name="itemRegistry"><see cref="IItemRegistry"/> that will be read.</param> /// <returns><see cref="IServiceCollection"/> that was passed by <paramref name="serviceCollection"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="serviceCollection"/> or <paramref name="itemRegistry"/> is null.</exception> /// <exception cref="InvalidItemRegistryException"><paramref name="itemRegistry"/> is not valid. See exception for more details.</exception> public static IServiceCollection AddItemTypes(this IServiceCollection serviceCollection, IItemRegistry itemRegistry) { if (serviceCollection == null) { throw new ArgumentNullException(nameof(serviceCollection)); } if (itemRegistry == null) { throw new ArgumentNullException(nameof(itemRegistry)); } serviceCollection.AddTransient(x => itemRegistry); var metaCollection = itemRegistry.GetItemMeta(); if (metaCollection == null) { throw new InvalidItemRegistryException($"The method {itemRegistry.GetType()}.{nameof(IItemRegistry.GetItemMeta)} returns a null enumerable instance!"); } if (metaCollection.Contains(null !)) { throw new InvalidItemRegistryException($"The method {itemRegistry.GetType()}.{nameof(IItemRegistry.GetItemMeta)} contains a null value inside the collection"); } var uniqueTypes = metaCollection.Select(x => x.Type).Distinct(); foreach (var itemType in uniqueTypes) { serviceCollection.AddTransient(itemType, x => ActivatorUtilities.CreateFactory(itemType, new[] { typeof(ItemMeta) })); } return(serviceCollection); }