Dependency
using Castle.Core.Configuration;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Resources;
namespace Castle.MicroKernel.Registration
{
public sealed class Dependency
{
private readonly object item;
internal Dependency(object item)
{
this.item = item;
}
public static Parameter OnConfigValue(string dependencyName, string valueAsString)
{
return Parameter.ForKey(dependencyName).Eq(valueAsString);
}
public static Parameter OnConfigValue(string dependencyName, IConfiguration value)
{
return Parameter.ForKey(dependencyName).Eq(value);
}
public static Parameter OnAppSettingsValue(string dependencyName, string settingName)
{
string value = ConfigurationManager.get_AppSettings().Get(settingName);
return Parameter.ForKey(dependencyName).Eq(value);
}
public static Parameter OnAppSettingsValue(string name)
{
return OnAppSettingsValue(name, name);
}
public static ServiceOverride OnComponent(string dependencyName, string componentName)
{
return Property.ForKey(dependencyName).Is(componentName);
}
public static ServiceOverride OnComponent(Type dependencyType, string componentName)
{
return Property.ForKey(dependencyType).Is(componentName);
}
public static ServiceOverride OnComponent(string dependencyName, Type componentType)
{
return Property.ForKey(dependencyName).Is(componentType);
}
public static ServiceOverride OnComponent(Type dependencyType, Type componentType)
{
return Property.ForKey(dependencyType).Is(componentType);
}
public static ServiceOverride OnComponent<TDependencyType, TComponentType>()
{
return Property.ForKey<TDependencyType>().Is<TComponentType>();
}
public static ServiceOverride OnComponentCollection(string collectionDependencyName, params string[] componentNames)
{
return ServiceOverride.ForKey(collectionDependencyName).Eq(componentNames);
}
public static ServiceOverride OnComponentCollection(Type collectionDependencyType, params string[] componentNames)
{
return ServiceOverride.ForKey(collectionDependencyType).Eq(componentNames);
}
public static ServiceOverride OnComponentCollection<TCollectionDependencyType>(params string[] componentNames) where TCollectionDependencyType : IEnumerable
{
return ServiceOverride.ForKey(typeof(TCollectionDependencyType)).Eq(componentNames);
}
public static ServiceOverride OnComponentCollection(string collectionDependencyName, params Type[] componentTypes)
{
return ServiceOverride.ForKey(collectionDependencyName).Eq(componentTypes);
}
public static ServiceOverride OnComponentCollection(Type collectionDependencyType, params Type[] componentTypes)
{
return ServiceOverride.ForKey(collectionDependencyType).Eq(componentTypes);
}
public static ServiceOverride OnComponentCollection<TCollectionDependencyType>(params Type[] componentTypes) where TCollectionDependencyType : IEnumerable
{
return ServiceOverride.ForKey(typeof(TCollectionDependencyType)).Eq(componentTypes);
}
public static Property OnValue(string dependencyName, object value)
{
return Property.ForKey(dependencyName).Eq(value);
}
public static Property OnValue(Type dependencyType, object value)
{
return Property.ForKey(dependencyType).Eq(value);
}
public static Property OnValue<TDependencyType>(object value)
{
return Property.ForKey<TDependencyType>().Eq(value);
}
internal bool Accept<TItem>(ICollection<TItem> items) where TItem : class
{
TItem val = item as TItem;
if (val != null) {
items.Add(val);
return true;
}
return false;
}
public static Property OnResource<TResources>(string dependencyName, string resourceName)
{
PropertyInfo property = typeof(TResources).GetProperty("ResourceManager", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, typeof(ResourceManager), Type.EmptyTypes, null);
if (property == (PropertyInfo)null)
throw new ArgumentException($"""{typeof(TResources)}""");
ResourceManager resourceManager;
try {
resourceManager = (ResourceManager)property.GetValue(null, null);
} catch (Exception innerException) {
throw new ArgumentException(string.Format("Could not read property {1} on type {0}", typeof(TResources), property), innerException);
}
return OnResource(dependencyName, resourceManager, resourceName);
}
public static Property OnResource(string dependencyName, ResourceManager resourceManager, string resourceName)
{
return Property.ForKey(dependencyName).Eq(resourceManager.GetObject(resourceName));
}
}
}