Skip to content

Commit

Permalink
Set up IoC registration to provide Default settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Vogel612 committed Apr 27, 2019
1 parent 09c8bc3 commit 61fd569
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
7 changes: 1 addition & 6 deletions Rubberduck.Main/Extension.cs
Expand Up @@ -149,12 +149,7 @@ private void InitializeAddIn()
}

var pathProvider = PersistencePathProvider.Instance;
var configLoader = new XmlPersistenceService<GeneralSettings>(pathProvider)
{
FilePath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Rubberduck", "rubberduck.config")
};
var configLoader = new XmlPersistenceService<GeneralSettings>(pathProvider);
var configProvider = new GeneralConfigProvider(configLoader);

_initialSettings = configProvider.Read();
Expand Down
23 changes: 23 additions & 0 deletions Rubberduck.Main/Root/FixedGenericAppender.cs
@@ -0,0 +1,23 @@
using Castle.Core;
using Castle.MicroKernel.Context;
using Castle.MicroKernel.Handlers;
using System;
using System.Linq;

namespace Rubberduck.Root
{
class FixedGenericAppender : IGenericImplementationMatchingStrategy
{
private readonly Type[] closingGenerics;

public FixedGenericAppender(Type[] closingGenerics)
{
this.closingGenerics = closingGenerics;
}

public Type[] GetGenericArguments(ComponentModel model, CreationContext context)
{
return context.GenericArguments.Union(closingGenerics).ToArray();
}
}
}
5 changes: 5 additions & 0 deletions Rubberduck.Main/Root/RubberduckIoCInstaller.cs
Expand Up @@ -244,6 +244,11 @@ private void RegisterConfiguration(IWindsorContainer container, Assembly[] assem
.Where(t => Attribute.IsDefined(t, typeof(ExperimentalAttribute))));
}

container.Register(Component.For(typeof(IDefaultSettings<>))
.ImplementedBy(typeof(DefaultSettings<,>), new FixedGenericAppender(new[] { typeof(Properties.Settings) }))
.IsFallback()
.LifestyleTransient());

var provider = new ExperimentalTypesProvider(experimentalTypes);
container.Register(Component.For(typeof(IExperimentalTypesProvider))
.DependsOn(Dependency.OnComponent<ViewModelBase, GeneralSettingsViewModel>())
Expand Down
3 changes: 1 addition & 2 deletions RubberduckTests/Settings/GeneralSettingsTests.cs
Expand Up @@ -168,13 +168,12 @@ public void AutoSavePeriodIsSetInCtor()
public void UserSettingsLoadedUsingDefaultWhenMissingFile()
{
var pathProviderMock = new Mock<IPersistencePathProvider>();
pathProviderMock.Setup(x => x.DataRootPath).Returns("C:\\rubberduck\\");
pathProviderMock.Setup(x => x.DataRootPath).Returns(@"C:\some\non\existent\path\rubberduck");
pathProviderMock.Setup(x => x.DataFolderPath(It.IsAny<string>())).Returns<string>(x => x);
// For this test, we need to use the actual object. Fortunately, the path is virtual, so we
// can override that property and force it to use an non-existent path to prove that settings
// will be still created using defaults without the file present.
var persisterMock = new Mock<XmlPersistenceService<GeneralSettings>>(pathProviderMock.Object);
persisterMock.Setup(x => x.FilePath).Returns("C:\\some\\non\\existent\\path\\rubberduck");
persisterMock.CallBase = true;
var configProvider = new GeneralConfigProvider(persisterMock.Object);

Expand Down

0 comments on commit 61fd569

Please sign in to comment.