Skip to content

Commit e037a19

Browse files
committed
Previous commit also fixed that the AfterSaveAction(s) weren't called. This commit adds ILanguagePart to support more modular Language code.
[release]
1 parent 540aee9 commit e037a19

10 files changed

+112
-8
lines changed

Dapplo.Config.Tests/ConfigTests/Interfaces/IIniConfigSubInterfaceTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
namespace Dapplo.Config.Tests.ConfigTests.Interfaces
3030
{
31-
public interface IIniConfigSubInterfaceTest : ISubSection
31+
public interface IIniConfigSubInterfaceTest : IIniSubSection
3232
{
3333
string SubValue { get; set; }
3434

Dapplo.Config.Tests/Dapplo.Config.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
<Compile Include="HelperExtensions.cs" />
191191
<Compile Include="LanguageTests\Interfaces\ILanguageLoaderFailTest.cs" />
192192
<Compile Include="LanguageTests\Interfaces\ILanguageLoaderMyModule.cs" />
193+
<Compile Include="LanguageTests\Interfaces\ILanguageLoaderPartTest.cs" />
193194
<Compile Include="LanguageTests\Interfaces\ILanguageLoaderTest.cs" />
194195
<Compile Include="LanguageTests\LanguageLoaderTest.cs" />
195196
<Compile Include="Properties\AssemblyInfo.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Dapplo - building blocks for desktop applications
2+
// Copyright (C) 2015-2016 Dapplo
3+
//
4+
// For more information see: http://dapplo.net/
5+
// Dapplo repositories are hosted on GitHub: https://github.com/dapplo
6+
//
7+
// This file is part of Dapplo.Config
8+
//
9+
// Dapplo.Config is free software: you can redistribute it and/or modify
10+
// it under the terms of the GNU Lesser General Public License as published by
11+
// the Free Software Foundation, either version 3 of the License, or
12+
// (at your option) any later version.
13+
//
14+
// Dapplo.Config is distributed in the hope that it will be useful,
15+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
// GNU Lesser General Public License for more details.
18+
//
19+
// You should have a copy of the GNU Lesser General Public License
20+
// along with Dapplo.Config. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
21+
22+
#region using
23+
24+
using System.ComponentModel;
25+
using Dapplo.Config.Language;
26+
27+
#endregion
28+
29+
namespace Dapplo.Config.Tests.LanguageTests.Interfaces
30+
{
31+
public interface ILanguageLoaderPartTest : ILanguagePart
32+
{
33+
[DefaultValue(LanguageLoaderTest.Ok)]
34+
string Ok2 { get; }
35+
}
36+
}

Dapplo.Config.Tests/LanguageTests/Interfaces/ILanguageLoaderTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
namespace Dapplo.Config.Tests.LanguageTests.Interfaces
3030
{
3131
[Language("Test")]
32-
public interface ILanguageLoaderTest : ILanguage, INotifyPropertyChanged
32+
public interface ILanguageLoaderTest : ILanguage, INotifyPropertyChanged, ILanguageLoaderPartTest
3333
{
3434
[DefaultValue(LanguageLoaderTest.Ok)]
3535
string Ok { get; }

Dapplo.Config.Tests/LanguageTests/LanguageLoaderTest.cs

+8
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ public async Task TestIndexer()
117117
Assert.True(_languageLoader["test"].Keys().Contains("dapplo"));
118118
}
119119

120+
[Fact]
121+
public async Task TestLanguagePart()
122+
{
123+
var language = await _languageLoader.RegisterAndGetAsync<ILanguageLoaderTest>();
124+
var part = _languageLoader.GetPart<ILanguageLoaderPartTest>();
125+
Assert.Equal("Ok", part.Ok2);
126+
}
127+
120128
[Fact]
121129
public async Task Test_LanguageChanged()
122130
{

Dapplo.Config/Dapplo.Config.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
<Compile Include="Ini\IniSectionEventArgs.cs" />
6666
<Compile Include="Ini\IniValue.cs" />
6767
<Compile Include="Ini\IniConfig.cs" />
68-
<Compile Include="Ini\ISubSection.cs" />
68+
<Compile Include="Ini\IIniSubSection.cs" />
69+
<Compile Include="Language\ILanguagePart.cs" />
6970
<Compile Include="Language\Implementation\ILanguageInternal.cs" />
7071
<Compile Include="Support\TypeDescriptorContext.cs" />
7172
<Compile Include="Language\ILanguage.cs" />

Dapplo.Config/Ini/ISubSection.cs Dapplo.Config/Ini/IIniSubSection.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Dapplo.Config.Ini
2424
/// <summary>
2525
/// Marker interface for sub sections
2626
/// </summary>
27-
public interface ISubSection
27+
public interface IIniSubSection
2828
{
2929
}
3030
}

Dapplo.Config/Ini/IniConfig.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -640,15 +640,21 @@ public T Get<T>() where T : IIniSection
640640
}
641641

642642
/// <summary>
643-
/// Get the IniSection which contains the sub section.
643+
/// Get the IniSection which contains the specified sub section.
644644
/// </summary>
645645
/// <typeparam name="T">Type which extends ISubSection</typeparam>
646646
/// <returns>T</returns>
647-
public T GetSubSection<T>() where T : ISubSection
647+
public T GetSubSection<T>() where T : IIniSubSection
648648
{
649649
var type = typeof(T);
650-
651-
return (T)Sections.FirstOrDefault(s => type.IsInstanceOfType(s));
650+
if (type.IsInstanceOfType(typeof(IIniSection)))
651+
{
652+
throw new ArgumentException("Cannot be of type IIniSection", nameof(T));
653+
}
654+
lock (_iniSections)
655+
{
656+
return (T)_iniSections.Where(s => type.IsInstanceOfType(s.Value)).Select(s => s.Value).FirstOrDefault();
657+
}
652658
}
653659

654660
/// <summary>
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#region Dapplo 2016 - GNU Lesser General Public License
2+
3+
// Dapplo - building blocks for .NET applications
4+
// Copyright (C) 2016 Dapplo
5+
//
6+
// For more information see: http://dapplo.net/
7+
// Dapplo repositories are hosted on GitHub: https://github.com/dapplo
8+
//
9+
// This file is part of Dapplo.Config
10+
//
11+
// Dapplo.Config is free software: you can redistribute it and/or modify
12+
// it under the terms of the GNU Lesser General Public License as published by
13+
// the Free Software Foundation, either version 3 of the License, or
14+
// (at your option) any later version.
15+
//
16+
// Dapplo.Config is distributed in the hope that it will be useful,
17+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
// GNU Lesser General Public License for more details.
20+
//
21+
// You should have a copy of the GNU Lesser General Public License
22+
// along with Dapplo.Config. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
23+
24+
#endregion
25+
26+
namespace Dapplo.Config.Language
27+
{
28+
/// <summary>
29+
/// Marker interface which allows a ILanguage interface to be devided, which allows more modular code.
30+
/// </summary>
31+
public interface ILanguagePart
32+
{
33+
}
34+
}

Dapplo.Config/Language/LanguageLoader.cs

+18
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,24 @@ public T Get<T>() where T : ILanguage
297297
return (T) Get(type);
298298
}
299299

300+
/// <summary>
301+
/// Get the ILanguage which contains the specified ILanguagePart.
302+
/// </summary>
303+
/// <typeparam name="T">Type which extends ISubSection</typeparam>
304+
/// <returns>T</returns>
305+
public T GetPart<T>() where T : ILanguagePart
306+
{
307+
var type = typeof(T);
308+
if (type.IsInstanceOfType(typeof(ILanguage)))
309+
{
310+
throw new ArgumentException("Cannot be of type ILanguage", nameof(T));
311+
}
312+
lock (_languageTypeConfigs)
313+
{
314+
return (T) _languageTypeConfigs.Where(s => type.IsInstanceOfType(s.Value)).Select(l => l.Value).FirstOrDefault();
315+
}
316+
}
317+
300318
/// <summary>
301319
/// Start the intial load, but if none was made yet
302320
/// </summary>

0 commit comments

Comments
 (0)