/// <summary> /// Registers objects to the <see cref="container"/> /// </summary> public static void RegisterTypes() { container = new UnityContainer(); container.RegisterType <IBot, Bot>(new PerThreadLifetimeManager()); container.RegisterSingleton <IConnection, Connection>(); container.RegisterType <ILogger, Logger.Logger>(new PerThreadLifetimeManager()); // DI for discord container.RegisterFactory <DiscordSocketConfig>(i => SocketConfig.GetDefault(), new SingletonLifetimeManager()); container.RegisterFactory <CommandService>(i => CommandConfig.GetDefault(), new SingletonLifetimeManager()); container.RegisterSingleton <DiscordShardedClient>(new InjectionConstructor(typeof(DiscordSocketConfig))); container.RegisterSingleton <IClientLogHandler, ClientLogHandler>(); container.RegisterSingleton <IMiscEventHandler, MiscEventHandler>(); container.RegisterSingleton <IPrefixService, PrefixService>(); container.RegisterSingleton <IBotListUpdater, BotListUpdater>(); container.RegisterSingleton <DiscordBotListsUpdateTimer>(); container.RegisterType <ICommandErrorHandler, CommandErrorHandler>(new PerThreadLifetimeManager()); container.RegisterType <ICommandInputErrorHandler, CommandInputErrorHandler>(new PerThreadLifetimeManager()); container.RegisterType <ICommandHandler, CommandHandler>(new PerThreadLifetimeManager()); container.RegisterType <ISpamFilter, SpamFilter>(new PerThreadLifetimeManager()); // DI for Entity framework container.RegisterType <BotContext>(new PerResolveLifetimeManager()); container.RegisterType <IUnitOfWork, UnitOfWork>(new PerResolveLifetimeManager()); container.RegisterType <IRequestUnitOfWork, RequestUnitOfWork>(new PerResolveLifetimeManager()); container.RegisterType <IServerUnitOfWork, ServerUnitOfWork>(new PerResolveLifetimeManager()); container.RegisterType <IServerRepository, ServerRepository>(new PerResolveLifetimeManager()); container.RegisterType <IUserRepository, UserRepository>(new PerResolveLifetimeManager()); container.RegisterType <IRequestRepository, RequestRepository>(new PerResolveLifetimeManager()); }
public void ConfigDefaultTests() { const LogSeverity expected = LogSeverity.Verbose; var actual = SocketConfig.GetDefault().LogLevel; Assert.Equal(expected, actual); }
public static void RegisterTypes() { _container = new UnityContainer(); _container.RegisterSingleton <IDataStorage, JsonStorage>(); _container.RegisterSingleton <ILogger, Logger>(); _container.RegisterType <DiscordSocketConfig>(new InjectionFactory(i => SocketConfig.GetDefault())); _container.RegisterSingleton <DiscordSocketClient>(new InjectionConstructor(typeof(DiscordSocketConfig))); _container.RegisterSingleton <Discord.Connection>(); }
public void DiscordSocketConfigDefaultTest() { const int expectedMsgCacheSize = 0; const bool expectedAlwaysDownloadUsers = false; var actual = SocketConfig.GetDefault(); Assert.Equal(expectedMsgCacheSize, actual.MessageCacheSize); Assert.Equal(expectedAlwaysDownloadUsers, actual.AlwaysDownloadUsers); }
/*A single location to specify how to resolve dependencies, and set lifecycle the objects*/ public static void RegisterTypes() { _container = new UnityContainer(); //Singleton. Each subsequent time it's called will return the same instance. _container.RegisterSingleton <IDataStorage, JsonStorage>(); _container.RegisterSingleton <ILogger, Logger>(); _container.RegisterType <DiscordSocketConfig>(new InjectionFactory(i => SocketConfig.GetDefault())); _container.RegisterSingleton <DiscordSocketClient>(new InjectionConstructor(typeof(DiscordSocketConfig))); _container.RegisterSingleton <Discord.Connection>(); }
private static void Main() { Unity.RegisterTypes(); Console.WriteLine("Hello, Discord!"); var discordBotConfig = new DiscordBotConfig { Token = "ABC", SocketConfig = SocketConfig.GetDefault() }; }
private static void Main() { Unity.RegisterTypes(); Console.WriteLine("Hello World!"); var config = new BotConfig { Token = "ABC", SocketConfig = SocketConfig.GetDefault() }; var connection = Unity.Resolve <Connection>(); }
public static void RegiterTypes() { _container = new UnityContainer(); //only one instance will be created because of this // ContainerControlledLifetimeManager //_container.RegisterType<IDataStorage, InMemoryStorage>(new ContainerControlledLifetimeManager()); //_container.RegisterType<ILogger, Logger>(new ContainerControlledLifetimeManager()); // _container.RegisterType<Discord.Connection>(new ContainerControlledLifetimeManager()); _container.RegisterSingleton <IDataStorage, JsonStorage>(); _container.RegisterSingleton <ILogger, Logger>(); _container.RegisterType <DiscordSocketConfig>(new InjectionFactory(i => SocketConfig.GetDefault())); _container.RegisterSingleton <DiscordSocketClient>(new InjectionConstructor(typeof(DiscordSocketConfig))); _container.RegisterSingleton <Discord.Connection>(); }
// static void Main(string[] args) { DiscordUnity.RegisterTypes(); Console.WriteLine("Hello Discord!"); // var discordBotConfig = new DiscordBotConfig { Token = "ABC", SocketConfig = SocketConfig.GetDefault() }; var connection = DiscordUnity.Resolve <Connection>(); Console.ReadKey(); }
public static void RegisterTypes() { _container = new UnityContainer(); // Bot Connection _container.RegisterType <DiscordSocketClient>(new InjectionFactory(i => SocketConfig.GetDefault())); _container.RegisterSingleton <DiscordSocketClient>(new InjectionConstructor(typeof(DiscordSocketConfig))); _container.RegisterSingleton <Discord.Connection>(); _container.RegisterSingleton <CommandHandler>(); // Storage _container.RegisterSingleton <IDataStorage, JsonStorage>(); _container.RegisterSingleton <ILogger, Logger>(); // Music _container.RegisterSingleton <AudioService>(); _container.RegisterSingleton <LogService>(); }
public static void RegisterTypes() { _container = new UnityContainer(); _container.RegisterSingleton <IDataStorage, JsonStorage>() .RegisterSingleton <ILogger, Logger>() .RegisterFactory <DiscordSocketConfig>(i => SocketConfig.GetDefault()) .RegisterSingleton <DiscordSocketClient>(new InjectionConstructor(typeof(DiscordSocketConfig))) .RegisterSingleton <Discord.Connection>() .RegisterSingleton <IRecipeScraper, AllRecipeScraper>() .RegisterSingleton <RecipeService>() .RegisterSingleton <ICommandHandler, DiscordCommandHandler>() .RegisterSingleton <IReactionHandler, DiscordReactionHandler>(); //_container.RegisterType<interface, implemtation>(); //_container.RegisterSingleton<interface, implemtation>(); }
public override void Load() { Bind <IDataStorage>().To <JsonStorage>().InSingletonScope(); Bind <ILogger>().To <Logger>().InSingletonScope(); // This *should* work as an injection factory to use the GetDefault version of SocketConfig // for DiscordSocketConfig as default. // Unity version: // _container.RegisterType<DiscordSocketConfig>(new InjectionFactory(i => SocketConfig.GetDefault())); Bind <DiscordSocketConfig>().ToMethod(context => SocketConfig.GetDefault()); // Still figuring this one out. Want to use a specific constructor instead of an empty one for DiscordSocketClient // Unity version: // _container.RegisterSingleton<DiscordSocketClient>(new InjectionConstructor(typeof(DiscordSocketConfig))); // This attempt made based on the following post: // https://github.com/ninject/ninject/issues/120#issuecomment-33205796 Bind <DiscordSocketClient>().ToSelf().InSingletonScope().WithConstructorArgument(typeof(DiscordSocketConfig)); /* Previous attempt * Bind<DiscordSocketClient>(new ConstructorInjector(typeof(DiscordSocketConfig))) * .ToSelf().InSingletonScope(); */ Bind <Discord.Connection>().ToSelf().InSingletonScope(); /* * Format: Bind<Interface>().To<Class>().InSingletonScope(); * Format: Bind<Class>().ToSelf().InSingletonScope(); * InSingletonScope() when there's only one instance of the thing being binded to * * In other classes * using Ninject; * /... * IKernel kernel = new StandardKernel(new ApplicationModule()); * objectType newObject = kernel.Get<objectType>(); */ }
public static void RegisterTypes() { container = new UnityContainer(); container.RegisterType <ILogger, Logger>(new PerThreadLifetimeManager()); container.RegisterFactory <DiscordSocketConfig>(i => SocketConfig.GetDefault(), new SingletonLifetimeManager()); container.RegisterSingleton <DiscordShardedClient>(new InjectionConstructor(typeof(DiscordSocketConfig))); container.RegisterSingleton <IConnection, Connection>(); container.RegisterSingleton <ICommandHandler, CommandHandler>(); container.RegisterSingleton <IExpService, ExpService>(); container.RegisterSingleton <IExpHandler, ExpHandler>(); container.RegisterSingleton <IUserLeftHandler, UserLeftHandler>(); container.RegisterSingleton <EventMessageHandler>(); container.RegisterSingleton <ICat, Cat>(); container.RegisterType <CatContext>(new PerResolveLifetimeManager()); container.RegisterType <IUnitOfWork, UnitOfWork>(new PerResolveLifetimeManager()); container.RegisterType <IServerRepository, ServerRepository>(new PerResolveLifetimeManager()); container.RegisterType <IUserRepository, UserRepository>(new PerResolveLifetimeManager()); container.RegisterType <ILogsRepository, LogsRepository>(new PerResolveLifetimeManager()); container.RegisterType <IDiscordLogger, DiscordLogger>(new PerThreadLifetimeManager()); }
public static void InitializeContainer() { container = new Container(c => { c.For <IPathfinderSettings>().Use <PathfinderSystemConfigurationManagerSettings>(); c.For <ICommandHandler>().Use <DiscordCommandHandler>(); c.For <IMessageHandler>().Use <DiscordMessageHandler>(); c.ForSingletonOf <ILogger>().UseIfNone <Logger>(); c.ForSingletonOf <IDataStorage>().UseIfNone <JsonStorage>(); c.ForSingletonOf <IConnectionService>().UseIfNone <DiscordConnectionService>(); c.ForSingletonOf <DiscordSocketClient>().UseIfNone(new DiscordSocketClient(SocketConfig.GetDefault())); }); }