Пример #1
0
        public override async Task ExecuteCommandAsync(VsProject result)
        {
            try
            {
                var registrationSourceCode = await RegisterServices.GetRegistrationClassAsync(result);

                if (registrationSourceCode == null)
                {
                    throw new CodeFactoryException("Could load or create the dependency injection code.");
                }

                if (!registrationSourceCode.IsLoaded)
                {
                    throw new CodeFactoryException("Could load or create the dependency injection code.");
                }

                var registrationClasses = await DependencyInjectionManagement.LoadInstanceProjectClassesForRegistrationAsync(result);

                var manager = registrationSourceCode.LoadNamespaceManager(result.DefaultNamespace);

                var injectionMethod = DependencyInjectionManagement.BuildInjectionMethod(registrationClasses, false,
                                                                                         true, RegisterServices.TransientClassRegistrationMethodName,
                                                                                         RegisterServices.ServiceCollectionParameterName, manager);

                if (injectionMethod == null)
                {
                    throw new CodeFactoryException("Could not generated the automated dependency injection method");
                }

                var registrationClass =
                    registrationSourceCode.Classes.FirstOrDefault(c =>
                                                                  c.Name == RegisterServices.RegistrationClassName);

                if (registrationClass == null)
                {
                    throw new CodeFactoryException("Could not load the dependency injection class");
                }

                var autoRegistrationMethod = registrationClass.Methods.FirstOrDefault(m =>
                                                                                      m.Name == RegisterServices.TransientClassRegistrationMethodName);

                if (autoRegistrationMethod != null)
                {
                    await autoRegistrationMethod.ReplaceAsync(injectionMethod);
                }
                else
                {
                    await registrationClass.AddToEndAsync(injectionMethod);
                }
            }
            catch (Exception unhandledError)
            {
                _logger.Error($"The following unhandled error occurred while executing the solution explorer project command {commandTitle}. ",
                              unhandledError);
            }
        }
Пример #2
0
#pragma warning disable CS1998
        #region Overrides of VsCommandBase<VsProject>

        /// <summary>
        /// Validation logic that will determine if this command should be enabled for execution.
        /// </summary>
        /// <param name="result">The target model data that will be used to determine if this command should be enabled.</param>
        /// <returns>Boolean flag that will tell code factory to enable this command or disable it.</returns>
        public override async Task <bool> EnableCommandAsync(VsProject result)
        {
            //Result that determines if the the command is enabled and visible in the context menu for execution.
            bool isEnabled = false;

            try
            {
                //Confirming the project supports dependency injection
                isEnabled = await DependencyInjectionManagement.HasMicrosoftExtensionDependencyInjectionLibrariesAsync(result);
            }
            catch (Exception unhandledError)
            {
                _logger.Error($"The following unhandled error occured while checking if the solution explorer project command {commandTitle} is enabled. ",
                              unhandledError);
                isEnabled = false;
            }

            return(isEnabled);
        }