Exemple #1
0
        private async Task ConfigureServicesAsync()
        {
            var environment = await GetServiceAsync(typeof(DTE)) as DTE2;

            var documentWindowMgr = await GetServiceAsync(typeof(IVsUIShellDocumentWindowMgr)) as IVsUIShellDocumentWindowMgr;

            var commandService = await GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;

            _container = new Container();
            _container.Register <PackageAccessor>(() => new PackageAccessor(this)).AsSingleton();
            _container.Register <IMenuCommandService>(() => commandService).AsSingleton();
            _container.Register <DTE2>(() => environment).AsSingleton();
            _container.Register <IVsUIShellDocumentWindowMgr>(() => documentWindowMgr).AsSingleton();
            _container.Register <IIDE, IDE>().AsSingleton();

            _container.Register <ISessionManager, SessionManager>().AsSingleton();
            _container.Register <IJsonSerializer, JsonSerializer>().AsSingleton();
            _container.Register <ISettingsStore, FileSettingsStore>().AsSingleton();
            _container.Register <ILogger, DefaultLogger>().AsSingleton();

            _container.Register <SessionManagerToolWindow>().AsSingleton();
            _container.Register <SessionManagerToolWindowControl>().AsSingleton();
            _container.Register <SessionManagerToolWindowState>().AsSingleton();

            // Commands
            var commands = new[]
            {
                typeof(SessionManagerToolWindowCommand),
                typeof(SaveCurrentSessionCommand),
                typeof(OpenSessionCommand),
                typeof(RestoreSessionCommand),
                typeof(CloseSessionDocumentsCommand),
                typeof(DeleteSessionCommand),
            };

            // Register each command
            foreach (var commandType in commands)
            {
                _container.Register(commandType, commandType).AsSingleton();
            }

            // Register collection of commands
            _container.Register <IEnumerable <ISessionManagerCommand> >(() =>
            {
                return(commands.Select(c => _serviceProvider.GetService(c) as ISessionManagerCommand).ToArray());
            });

            //_container.Register<SessionManagerToolWindowCommand>().AsSingleton();

            _serviceProvider = _container.CreateScope();
            _container.Register <IServiceProvider>(() => _serviceProvider).AsSingleton();
        }
Exemple #2
0
 /// <summary>
 /// 返回指定接口的实现(类)
 /// </summary>
 /// <typeparam name="T">Interface type</typeparam>
 /// <param name="scope">This scope instance</param>
 /// <returns>Object implementing the interface</returns>
 public static T Resolve <T>(this Container.IScope scope)
 {
     return((T)scope.GetService(typeof(T)));
 }