static void Main(string[] args) { // Create a new container var container = new SimpleInjector.Container(); // Configure the container (Register) container.Register <LogConfiguration>(); container.Register <IAccount, AccountController>(); container.Collection.Register <ILogger>(typeof(ConsoleLogger), typeof(CompositeLogger)); container.Collection.Append <ILogger, FileLoger>(); var assemblies = new[] { typeof(ICommand <>).Assembly }; //container.Register(typeof(ICommand<>), assemblies); container.Collection.Register(assemblies); container.Register <IHandler <AHandler>, AHandler>(); container.Register <IHandler <BHandler>, BHandler>(); container.RegisterInitializer <IHandler>(hdl => hdl.ExecuteAsynchronously = true); container.Register <Transient>(Lifestyle.Transient); //container.Register<Scoped>(Lifestyle.Scoped); container.Register <Singleton1>(Lifestyle.Singleton); container.RegisterInstance(new Singleton2()); container.RegisterSingleton <Singleton3>(); // Solve: Cycling dependency container.RegisterInstance <IServiceProvider>(container); container.Register <ServiceFactory>(); container.Register <AService>(); container.Register <BService>(); // Verify the container (optional) container.Verify(); Console.WriteLine("Verified ====>"); // Retrieve instances from the container (Resolve) DemoRegisterInitializer(container); DemoChangeValue <Transient>(container); //DemoChangeValue<Scoped>(container); DemoChangeValue <Singleton1>(container); DemoChangeValue <Singleton2>(container); DemoChangeValue <Singleton3>(container); var depenOnB = container.GetInstance <AService>(); var depenOnA = container.GetInstance <BService>(); }
/// <summary> /// DIコンテナへ生成インスタンスの登録を行います /// </summary> /// <param name="container"></param> public void SetDiContainer(SimpleInjector.Container container) { this.mContainer = container; container.RegisterInstance <IApplicationContext> (this); container.Register <ICategoryRepository, CategoryRepository> (); container.Register <IContentRepository, ContentRepository> (); container.Register <IEventLogRepository, EventLogRepository> (); container.Register <IFileMappingInfoRepository, FileMappingInfoRepository> (); container.Register <ILabelRepository, LabelRepository> (); container.Register <IWorkspaceRepository, WorkspaceRepository> (); container.Register <IAppAppMetaInfoRepository, AppAppMetaInfoRepository> (); container.Register <IThumbnailAppMetaInfoRepository, ThumbnailAppMetaInfoRepository> (); container.Register <IThumbnailRepository, ThumbnailRepository> (); container.Register <ApiResponseBuilder> (); container.Register <IAppDbContext, AppDbContext> (Lifestyle.Scoped); container.Register <IThumbnailDbContext, ThumbnailDbContext> (Lifestyle.Scoped); container.Register <IThumbnailBuilder, ThumbnailBuilder> (); container.Register <IFileUpdateRunner, FileUpdateRunner> (); // サービス登録 container.Register <IVirtualFileSystemService, VirtualFileSystemServiceImpl> (); // メッセージング機能 container.Register <IMessagingScopeContext, MessagingScopeContext> (Lifestyle.Scoped); var messagingManager = new MessagingManager(container); container.RegisterInstance <MessagingManager> (messagingManager); container.RegisterInstance <IMessagingManager> (messagingManager); // 拡張機能 var extentionManager = new ExtentionManager(container); //extentionManager.AddPlugin (typeof (InitializeBuildExtention)); // 開発中は常に拡張機能を読み込む //extentionManager.AddPlugin (typeof (WebScribeExtention)); // 開発中は常に拡張機能を読み込む container.RegisterInstance <ExtentionManager> (extentionManager); extentionManager.InitializePlugin(ExtentionDirectoryPath); extentionManager.CompletePlugin(); // VFS機能 var virtualFileUpdateWatchService = new VirtualFileUpdateWatchService(container); container.RegisterInstance <VirtualFileUpdateWatchService> (virtualFileUpdateWatchService); }
// Configure your AppHost with the necessary configuration and dependencies your App needs public override void Configure(Funq.Container container) { base.SetConfig(new HostConfig { DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false), HandlerFactoryPath = "api" }); // plugins Plugins.Add(new TemplatePagesFeature()); Plugins.Add(new ValidationFeature()); SimpleContainer = new Container(); //SimpleContainer.Options.DefaultScopedLifestyle = ScopedLifestyle.Flowing; container.Adapter = new SimpleInjectorIocAdapter(SimpleContainer); InitializeCache(); InitializeDbConnectionFactory(); InitializeMessaging(); SimpleContainer.Register(() => Container.Adapter.Resolve <IMessageService>().MessageFactory); SimpleContainer.Register <IMessageProducer>(() => Container.Adapter.Resolve <IMessageFactory>().CreateMessageProducer(), Lifestyle.Transient); SimpleContainer.Register <IMessageQueueClient>(() => Container.Adapter.Resolve <IMessageFactory>().CreateMessageQueueClient(), Lifestyle.Transient); InitializeIntegrations(); // repositories SimpleContainer.Register <ILinkRepository, LinkRepository>(); SimpleContainer.Register <ITagRepository, TagRepository>(); // validators SimpleContainer.Register <ITagValidator, TagValidator>(); SimpleContainer.Collection.Register(typeof(IValidator <>), this.ServiceAssemblies); SimpleContainer.Collection.Register(typeof(ICreateLinkScreenshotHandler), this.ServiceAssemblies); //SimpleContainer.RegisterDecorator(...); // https://cuttingedge.it/blogs/steven/pivot/entry.php?id=93 // events SimpleContainer.RegisterInstance <IEventBus>(new DomainEventBus(SimpleContainer)); SimpleContainer.Collection.Register(typeof(IEventHandler <>), this.ServiceAssemblies); // Done container registration! SimpleContainer.Verify(VerificationOption.VerifyOnly); OnConfigEnding(); // Messaging Service var mqHost = Container.Adapter.Resolve <IMessageService>(); mqHost.RegisterHandler <CreateLinkScreenshotRequest>(base.ExecuteMessage); mqHost.Start(); }
public static void RegisterMvcIntegratedFilterProvider(this Container container) { if (container == null) { throw new ArgumentNullException(nameof(container)); } RequiresFilterProviderNotRegistered(container); var singletonFilterProvider = new SimpleInjectorFilterAttributeFilterProvider(container); container.RegisterInstance <IFilterProvider>(singletonFilterProvider); var providers = FilterProviders.Providers.OfType <FilterAttributeFilterProvider>().ToList(); providers.ForEach(provider => FilterProviders.Providers.Remove(provider)); FilterProviders.Providers.Add(singletonFilterProvider); }
public static SimpleInjector.Container GetInstance() { if (_container == null) { _container = new SimpleInjector.Container(); _container.RegisterInstance(new DataBaseService()); _container.Options.AllowOverridingRegistrations = true; _container.Options.ResolveUnregisteredConcreteTypes = true; _container.Register <IPartnerRepository, PartnerRepository>(Lifestyle.Singleton); _container.Register <IPartnerService, PartnerService>(Lifestyle.Singleton); _container.Register <ReportService>(Lifestyle.Singleton); _container.Verify(); } return(_container); }
public static void Register(HttpConfiguration config) { var container = new SimpleInjector.Container(); container.Options.DefaultScopedLifestyle = new SimpleInjector.Lifestyles.AsyncScopedLifestyle(); container.Register <IAccountService, AccountService>(Lifestyle.Transient); container.RegisterInstance <ILogger>(LogManager.GetCurrentClassLogger()); container.RegisterWebApiControllers(config); container.Verify(); GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container); config.Filters.Add(new ExceptionFilter()); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }
static void Main(string[] argv) { Channel logChannel = null; Logger log = null; Thread logFlushing = null; Semaphore cacheSemaphore = new Semaphore(0, 1); Semaphore itemSemaphore = new Semaphore(0, 1); try { // create the dependency injector container Container dependencies = new Container(); // register basic dependencies first dependencies.Register <Logger>(Lifestyle.Singleton); dependencies.Register <DatabaseConnection>(Lifestyle.Singleton); dependencies.Register <ClientManager>(Lifestyle.Singleton); dependencies.Register <NodeContainer>(Lifestyle.Singleton); dependencies.Register <CacheStorage>(Lifestyle.Singleton); dependencies.Register <ItemManager>(Lifestyle.Singleton); dependencies.Register <MetaInventoryManager>(Lifestyle.Singleton); dependencies.Register <AttributeManager>(Lifestyle.Singleton); dependencies.Register <TypeManager>(Lifestyle.Singleton); dependencies.Register <CategoryManager>(Lifestyle.Singleton); dependencies.Register <GroupManager>(Lifestyle.Singleton); dependencies.Register <StationManager>(Lifestyle.Singleton); dependencies.Register <ItemFactory>(Lifestyle.Singleton); dependencies.Register <TimerManager>(Lifestyle.Singleton); dependencies.Register <SystemManager>(Lifestyle.Singleton); dependencies.Register <ServiceManager>(Lifestyle.Singleton); dependencies.Register <BoundServiceManager>(Lifestyle.Singleton); dependencies.Register <ClusterConnection>(Lifestyle.Singleton); dependencies.Register <CharacterManager>(Lifestyle.Singleton); dependencies.Register <NotificationManager>(Lifestyle.Singleton); dependencies.Register <MachoNet>(Lifestyle.Singleton); dependencies.Register <ExpressionManager>(Lifestyle.Singleton); dependencies.Register <WalletManager>(Lifestyle.Singleton); dependencies.Register <MailManager>(Lifestyle.Singleton); // register the database accessors dependencies dependencies.Register <AccountDB>(Lifestyle.Singleton); dependencies.Register <AgentDB>(Lifestyle.Singleton); dependencies.Register <BookmarkDB>(Lifestyle.Singleton); dependencies.Register <CertificatesDB>(Lifestyle.Singleton); dependencies.Register <CharacterDB>(Lifestyle.Singleton); dependencies.Register <ChatDB>(Lifestyle.Singleton); dependencies.Register <ConfigDB>(Lifestyle.Singleton); dependencies.Register <ContractDB>(Lifestyle.Singleton); dependencies.Register <CorporationDB>(Lifestyle.Singleton); dependencies.Register <GeneralDB>(Lifestyle.Singleton); dependencies.Register <ItemDB>(Lifestyle.Singleton); dependencies.Register <MarketDB>(Lifestyle.Singleton); dependencies.Register <MessagesDB>(Lifestyle.Singleton); dependencies.Register <SkillDB>(Lifestyle.Singleton); dependencies.Register <StandingDB>(Lifestyle.Singleton); dependencies.Register <StationDB>(Lifestyle.Singleton); dependencies.Register <LookupDB>(Lifestyle.Singleton); dependencies.Register <InsuranceDB>(Lifestyle.Singleton); dependencies.Register <SolarSystemDB>(Lifestyle.Singleton); dependencies.Register <DogmaDB>(Lifestyle.Singleton); dependencies.Register <RepairDB>(Lifestyle.Singleton); dependencies.Register <ReprocessingDB>(Lifestyle.Singleton); dependencies.Register <RAMDB>(Lifestyle.Singleton); dependencies.Register <FactoryDB>(Lifestyle.Singleton); dependencies.Register <WalletDB>(Lifestyle.Singleton); // register all the services dependencies.Register <account>(Lifestyle.Singleton); dependencies.Register <machoNet>(Lifestyle.Singleton); dependencies.Register <objectCaching>(Lifestyle.Singleton); dependencies.Register <alert>(Lifestyle.Singleton); dependencies.Register <authentication>(Lifestyle.Singleton); dependencies.Register <character>(Lifestyle.Singleton); dependencies.Register <userSvc>(Lifestyle.Singleton); dependencies.Register <charmgr>(Lifestyle.Singleton); dependencies.Register <config>(Lifestyle.Singleton); dependencies.Register <dogmaIM>(Lifestyle.Singleton); dependencies.Register <invbroker>(Lifestyle.Singleton); dependencies.Register <warRegistry>(Lifestyle.Singleton); dependencies.Register <station>(Lifestyle.Singleton); dependencies.Register <map>(Lifestyle.Singleton); dependencies.Register <skillMgr>(Lifestyle.Singleton); dependencies.Register <contractMgr>(Lifestyle.Singleton); dependencies.Register <corpStationMgr>(Lifestyle.Singleton); dependencies.Register <bookmark>(Lifestyle.Singleton); dependencies.Register <LSC>(Lifestyle.Singleton); dependencies.Register <onlineStatus>(Lifestyle.Singleton); dependencies.Register <billMgr>(Lifestyle.Singleton); dependencies.Register <facWarMgr>(Lifestyle.Singleton); dependencies.Register <corporationSvc>(Lifestyle.Singleton); dependencies.Register <clientStatsMgr>(Lifestyle.Singleton); dependencies.Register <voiceMgr>(Lifestyle.Singleton); dependencies.Register <standing2>(Lifestyle.Singleton); dependencies.Register <tutorialSvc>(Lifestyle.Singleton); dependencies.Register <agentMgr>(Lifestyle.Singleton); dependencies.Register <corpRegistry>(Lifestyle.Singleton); dependencies.Register <marketProxy>(Lifestyle.Singleton); dependencies.Register <stationSvc>(Lifestyle.Singleton); dependencies.Register <certificateMgr>(Lifestyle.Singleton); dependencies.Register <jumpCloneSvc>(Lifestyle.Singleton); dependencies.Register <LPSvc>(Lifestyle.Singleton); dependencies.Register <lookupSvc>(Lifestyle.Singleton); dependencies.Register <insuranceSvc>(Lifestyle.Singleton); dependencies.Register <slash>(Lifestyle.Singleton); dependencies.Register <ship>(Lifestyle.Singleton); dependencies.Register <corpmgr>(Lifestyle.Singleton); dependencies.Register <repairSvc>(Lifestyle.Singleton); dependencies.Register <reprocessingSvc>(Lifestyle.Singleton); dependencies.Register <ramProxy>(Lifestyle.Singleton); dependencies.Register <factory>(Lifestyle.Singleton); dependencies.RegisterInstance(General.LoadFromFile("configuration.conf", dependencies)); // disable auto-verification on the container as it triggers creation of instances before they're needed dependencies.Options.EnableAutoVerification = false; General configuration = dependencies.GetInstance <General>(); log = dependencies.GetInstance <Logger>(); logChannel = log.CreateLogChannel("main"); // add log streams log.AddLogStream(new ConsoleLogStream()); if (configuration.LogLite.Enabled == true) { log.AddLogStream(new LogLiteStream("Node", log, configuration.LogLite)); } if (configuration.FileLog.Enabled == true) { log.AddLogStream(new FileLogStream(configuration.FileLog)); } // run a thread for log flushing logFlushing = new Thread(() => { try { while (true) { log.Flush(); Thread.Sleep(1); } } catch { // ignored } }); logFlushing.Start(); logChannel.Info("Initializing EVESharp Node"); logChannel.Fatal("Initializing EVESharp Node"); logChannel.Error("Initializing EVESharp Node"); logChannel.Warning("Initializing EVESharp Node"); logChannel.Debug("Initializing EVESharp Node"); logChannel.Trace("Initializing EVESharp Node"); // connect to the database dependencies.GetInstance <DatabaseConnection>(); // sDatabase.Query("SET global max_allowed_packet=1073741824"); // create the node container dependencies.GetInstance <NodeContainer>(); logChannel.Info("Initializing timer manager"); dependencies.GetInstance <TimerManager>().Start(); logChannel.Debug("Done"); // do some parallel initialization, cache priming and static item loading can be performed in parallel // this makes the changes quicker new Thread(() => { logChannel.Info("Initializing cache"); CacheStorage cacheStorage = dependencies.GetInstance <CacheStorage>(); // prime bulk data cacheStorage.Load( CacheStorage.LoginCacheTable, CacheStorage.LoginCacheQueries, CacheStorage.LoginCacheTypes ); // prime character creation cache cacheStorage.Load( CacheStorage.CreateCharacterCacheTable, CacheStorage.CreateCharacterCacheQueries, CacheStorage.CreateCharacterCacheTypes ); // prime character appearance cache cacheStorage.Load( CacheStorage.CharacterAppearanceCacheTable, CacheStorage.CharacterAppearanceCacheQueries, CacheStorage.CharacterAppearanceCacheTypes ); logChannel.Info("Cache Initialized"); cacheSemaphore.Release(1); }).Start(); new Thread(() => { logChannel.Info("Initializing item factory"); dependencies.GetInstance <ItemFactory>().Init(); logChannel.Debug("Item Factory Initialized"); itemSemaphore.Release(1); }).Start(); // wait for both semaphores cacheSemaphore.WaitOne(); itemSemaphore.WaitOne(); logChannel.Info("Initializing solar system manager"); dependencies.GetInstance <SystemManager>(); logChannel.Debug("Done"); dependencies.GetInstance <MachoNet>().ConnectToProxy(); logChannel.Trace("Node startup done"); while (true) { Thread.Sleep(1); } } catch (Exception e) { if (log is null || logChannel is null) { Console.WriteLine(e.ToString()); }