Skip to content

Commit

Permalink
DI related changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey M committed Apr 26, 2020
1 parent a79a374 commit 1c77bc5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 59 deletions.
43 changes: 11 additions & 32 deletions DPackRx.Tests/Package/FeatureCommandFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;

using Moq;
using NUnit.Framework;

using DPackRx.Features;
using DPackRx.Package;
using DPackRx.Services;

namespace DPackRx.Tests.Package
{
Expand All @@ -19,10 +17,8 @@ public class FeatureCommandFactoryTests
{
#region Fields

private Mock<IMenuCommandService> _menuCommandServiceMock;
private Mock<ILog> _logMock;
private Mock<IMessageService> _messageServiceMock;
private Mock<IUtilsService> _utilsServiceMock;
private Mock<IServiceProvider> _serviceProviderMock;
private Mock<IFeatureCommand> _featureCommandMock;

#endregion

Expand Down Expand Up @@ -64,25 +60,18 @@ public bool IsValidContext(int commandId)
[SetUp]
public void Setup()
{
_menuCommandServiceMock = new Mock<IMenuCommandService>();
_menuCommandServiceMock.Setup(m => m.AddCommand(It.IsAny<MenuCommand>())).Verifiable();
_featureCommandMock = new Mock<IFeatureCommand>();
_featureCommandMock.Setup(c => c.Initialize(It.IsAny<IFeature>(), It.IsAny<int>())).Verifiable();

_logMock = new Mock<ILog>();
_logMock.Setup(l => l.LogMessage(It.IsAny<string>(), It.IsAny<string>())).Verifiable();

_messageServiceMock = new Mock<IMessageService>();
_messageServiceMock.Setup(m => m.ShowError(It.IsAny<string>(), true)).Verifiable();

_utilsServiceMock = new Mock<IUtilsService>();
_serviceProviderMock = new Mock<IServiceProvider>();
_serviceProviderMock.Setup(s => s.GetService(typeof(IFeatureCommand))).Returns(_featureCommandMock.Object).Verifiable();
}

[TearDown]
public void TearDown()
{
_menuCommandServiceMock = null;
_logMock = null;
_messageServiceMock = null;
_utilsServiceMock = null;
_serviceProviderMock = null;
_featureCommandMock = null;
}

#endregion
Expand All @@ -94,15 +83,7 @@ public void TearDown()
/// </summary>
private IFeatureCommandFactory GetService()
{
return new FeatureCommandFactory(_logMock.Object, _menuCommandServiceMock.Object, _messageServiceMock.Object, _utilsServiceMock.Object);
}

/// <summary>
/// Returns command instance.
/// </summary>
private IFeatureCommand GetCommand()
{
return new FeatureCommand(_logMock.Object, _menuCommandServiceMock.Object, _messageServiceMock.Object, _utilsServiceMock.Object);
return new FeatureCommandFactory(_serviceProviderMock.Object);
}

#endregion
Expand All @@ -118,10 +99,8 @@ public void CreateCommand()
var command = service.CreateCommand(feature, 123);

Assert.That(command, Is.Not.Null);
Assert.That(command.CommandId, Is.EqualTo(123));
Assert.That(command.Feature, Is.EqualTo(KnownFeature.Miscellaneous));
Assert.That(command.Initialized, Is.True);
_menuCommandServiceMock.Verify(m => m.AddCommand(It.IsNotNull<MenuCommand>()));
_serviceProviderMock.Verify(s => s.GetService(typeof(IFeatureCommand)));
_featureCommandMock.Verify(c => c.Initialize(feature, 123));
}

#endregion
Expand Down
6 changes: 3 additions & 3 deletions DPackRx/Extensions/ServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static T GetService<T>(this IServiceProvider serviceProvider, bool throwO
if (throwOnError)
throw new ApplicationException($"Service of {typeof(T)} type is not available.");
else
return default(T);
return default;
}

return (T)service;
Expand All @@ -47,7 +47,7 @@ public static T GetService<T, TS>(this IServiceProvider serviceProvider, bool th
if (throwOnError)
throw new ApplicationException($"Service of {typeof(TS)} type is not available.");
else
return default(T);
return default;
}

return (T)service;
Expand All @@ -68,7 +68,7 @@ public static async Task<T> GetServiceAsync<T>(this IAsyncServiceProvider servic
if (throwOnError)
throw new ApplicationException($"Service of {typeof(T)} type is not available.");
else
return default(T);
return default;
}

return (T)service;
Expand Down
2 changes: 2 additions & 0 deletions DPackRx/Features/IFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ public enum KnownFeature // TODO: finish other features
[Description("Surround With")]
SurroundWith,

//[Description("Solution Statistics")]
//SolutionStats,

//[Description("Solution Backup")]
//SolutionBackup,
}

Expand Down
12 changes: 0 additions & 12 deletions DPackRx/Package/FeatureCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ public class FeatureCommand : IFeatureCommand

public FeatureCommand(ILog log, IMenuCommandService menuCommandService, IMessageService messageService, IUtilsService utilsService)
{
if (log == null)
throw new ArgumentNullException(nameof(log));

if (menuCommandService == null)
throw new ArgumentNullException(nameof(menuCommandService));

if (messageService == null)
throw new ArgumentNullException(nameof(messageService));

if (utilsService == null)
throw new ArgumentNullException(nameof(utilsService));

_log = log;
_menuCommandService = menuCommandService;
_messageService = messageService;
Expand Down
18 changes: 6 additions & 12 deletions DPackRx/Package/FeatureCommandFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.ComponentModel.Design;
using System;

using DPackRx.Extensions;
using DPackRx.Features;
using DPackRx.Services;

namespace DPackRx.Package
{
Expand All @@ -12,19 +12,13 @@ public class FeatureCommandFactory : IFeatureCommandFactory
{
#region Fields

private readonly ILog _log;
private readonly IMenuCommandService _menuCommandService;
private readonly IMessageService _messageService;
private readonly IUtilsService _utilsService;
private readonly IServiceProvider _serviceProvider;

#endregion

public FeatureCommandFactory(ILog log, IMenuCommandService menuCommandService, IMessageService messageService, IUtilsService utilsService)
public FeatureCommandFactory(IServiceProvider serviceProvider)
{
_log = log;
_menuCommandService = menuCommandService;
_messageService = messageService;
_utilsService = utilsService;
_serviceProvider = serviceProvider;
}

#region ICommandFactory Members
Expand All @@ -34,7 +28,7 @@ public FeatureCommandFactory(ILog log, IMenuCommandService menuCommandService, I
/// </summary>
public IFeatureCommand CreateCommand(IFeature feature, int commandId)
{
var command = new FeatureCommand(_log, _menuCommandService, _messageService, _utilsService);
var command = _serviceProvider.GetService<IFeatureCommand>();
command.Initialize(feature, commandId);
return command;
}
Expand Down
1 change: 1 addition & 0 deletions DPackRx/Services/FeatureFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ private void Initialize()
_log.LogMessage($"Failed to initialize feature {feature}", ex);
errors.Add(GetFeatureName(feature));
}

_features.TryAdd(feature, featureInstance);
_log.LogMessage($"Loaded {feature} feature");
}
Expand Down

0 comments on commit 1c77bc5

Please sign in to comment.