-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
288e45a
commit a187dfe
Showing
40 changed files
with
679 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace Jamesnet.Core; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
namespace Jamesnet.Core; | ||
using System; | ||
|
||
namespace Jamesnet.Core; | ||
|
||
public static class ContainerProvider | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
using System; | ||
|
||
namespace Jamesnet.Core; | ||
|
||
public interface IContainer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
namespace Jamesnet.Core; | ||
using System; | ||
|
||
namespace Jamesnet.Core; | ||
|
||
public interface IViewModelMapper | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] | ||
using System; | ||
|
||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] | ||
public class InjectAttribute : Attribute | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<LangVersion>10</LangVersion> | ||
<Nullable>disable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
using System; | ||
using System.Windows.Input; | ||
|
||
namespace Jamesnet.Core; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
namespace Jamesnet.Core; | ||
|
||
public abstract class AppBootstrapper | ||
{ | ||
protected readonly IContainer Container; | ||
protected readonly ILayerManager Layer; | ||
protected readonly IViewModelMapper ViewModelMapper; | ||
|
||
protected AppBootstrapper() | ||
{ | ||
Container = new Container(); | ||
Layer = new LayerManager(); | ||
ViewModelMapper = new ViewModelMapper(); | ||
ContainerProvider.SetContainer(Container); | ||
ConfigureContainer(); | ||
} | ||
|
||
protected virtual void ConfigureContainer() | ||
{ | ||
Container.RegisterInstance<IContainer>(Container); | ||
Container.RegisterInstance<ILayerManager>(Layer); | ||
Container.RegisterInstance<IViewModelMapper>(ViewModelMapper); | ||
Container.RegisterSingleton<IViewModelInitializer, DefaultViewModelInitializer>(); | ||
} | ||
|
||
protected abstract void RegisterViewModels(); | ||
protected abstract void RegisterDependencies(); | ||
|
||
public void Run() | ||
{ | ||
RegisterViewModels(); | ||
RegisterDependencies(); | ||
OnStartup(); | ||
} | ||
|
||
protected abstract void OnStartup(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Reflection; | ||
|
||
namespace Jamesnet.Core; | ||
|
||
public abstract class BaseResourceLoader<TItem, TResult> | ||
{ | ||
protected abstract string AssemblyName { get; } | ||
protected abstract string ResourcePath { get; } | ||
protected abstract IEnumerable<TItem> ConvertToItems(YamlData rawData); | ||
protected abstract TResult OrganizeItems(IEnumerable<TItem> items); | ||
|
||
public TResult LoadAndOrganize() | ||
{ | ||
Assembly assembly = Assembly.Load(AssemblyName); | ||
YamlData rawData = LoadYamlData(assembly, ResourcePath); | ||
IEnumerable<TItem> items = ConvertToItems(rawData); | ||
return OrganizeItems(items); | ||
} | ||
|
||
private YamlData LoadYamlData(Assembly assembly, string resourcePath) | ||
{ | ||
YamlData yamlData = new YamlData(); | ||
|
||
object result = YamlConverter.ParseResource(assembly, resourcePath); | ||
IEnumerable<object> data = result as IEnumerable<object>; | ||
|
||
if (data == null) | ||
{ | ||
throw new InvalidOperationException("YamlConverter.ParseResource did not return an IEnumerable<object>"); | ||
} | ||
|
||
foreach (object item in data) | ||
{ | ||
IDictionary<string, string> dict = item as IDictionary<string, string>; | ||
if (dict != null) | ||
{ | ||
yamlData.Add(new YamlItem(dict)); | ||
} | ||
} | ||
|
||
return yamlData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
namespace Jamesnet.Core; | ||
|
||
public class Container : IContainer | ||
{ | ||
private readonly Dictionary<(Type, string), Func<object>> _registrations = new Dictionary<(Type, string), Func<object>>(); | ||
|
||
public void Register<TInterface, TImplementation>() where TImplementation : TInterface | ||
{ | ||
Register<TInterface, TImplementation>(null); | ||
} | ||
|
||
public void Register<TInterface, TImplementation>(string name) where TImplementation : TInterface | ||
{ | ||
_registrations[(typeof(TInterface), name)] = () => CreateInstance(typeof(TImplementation)); | ||
} | ||
|
||
public void RegisterSingleton<TInterface, TImplementation>() where TImplementation : TInterface | ||
{ | ||
RegisterSingleton<TInterface, TImplementation>(null); | ||
} | ||
|
||
public void RegisterSingleton<TInterface, TImplementation>(string name) where TImplementation : TInterface | ||
{ | ||
var lazy = new Lazy<object>(() => CreateInstance(typeof(TImplementation))); | ||
_registrations[(typeof(TInterface), name)] = () => lazy.Value; | ||
} | ||
|
||
public void RegisterSingleton<TImplementation>(string name) | ||
{ | ||
var lazy = new Lazy<object>(() => CreateInstance(typeof(TImplementation))); | ||
_registrations[(typeof(TImplementation), name)] = () => lazy.Value; | ||
} | ||
|
||
public void RegisterInstance<TInterface>(TInterface instance) | ||
{ | ||
RegisterInstance(instance, null); | ||
} | ||
|
||
public void RegisterInstance<TInterface>(TInterface instance, string name) | ||
{ | ||
_registrations[(typeof(TInterface), name)] = () => instance; | ||
} | ||
|
||
public T Resolve<T>() | ||
{ | ||
return Resolve<T>(null); | ||
} | ||
|
||
public T Resolve<T>(string name) | ||
{ | ||
return (T)Resolve(typeof(T), name); | ||
} | ||
|
||
public object Resolve(Type type) | ||
{ | ||
return Resolve(type, null); | ||
} | ||
|
||
public object Resolve(Type type, string name) | ||
{ | ||
if (_registrations.TryGetValue((type, name), out var creator)) | ||
{ | ||
return creator(); | ||
} | ||
if (!type.IsAbstract && !type.IsInterface) | ||
{ | ||
return CreateInstance(type); | ||
} | ||
throw new InvalidOperationException($"Type {type} has not been registered."); | ||
} | ||
|
||
public bool TryResolve<T>(out T result) | ||
{ | ||
return TryResolve<T>(null, out result); | ||
} | ||
|
||
public bool TryResolve<T>(string name, out T result) | ||
{ | ||
if (_registrations.TryGetValue((typeof(T), name), out var creator)) | ||
{ | ||
result = (T)creator(); | ||
return true; | ||
} | ||
if (!typeof(T).IsAbstract && !typeof(T).IsInterface) | ||
{ | ||
result = (T)CreateInstance(typeof(T)); | ||
return true; | ||
} | ||
result = default; | ||
return false; | ||
} | ||
|
||
private object CreateInstance(Type type) | ||
{ | ||
var constructors = type.GetConstructors(); | ||
var constructor = constructors.FirstOrDefault(c => c.GetParameters().Length > 0) ?? constructors.First(); | ||
var parameters = constructor.GetParameters().Select(p => Resolve(p.ParameterType)).ToArray(); | ||
return constructor.Invoke(parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Jamesnet.Core; | ||
|
||
public static class ContainerProvider | ||
{ | ||
private static IContainer _container; | ||
|
||
public static void SetContainer(IContainer container) | ||
{ | ||
_container = container; | ||
} | ||
|
||
public static IContainer GetContainer() | ||
{ | ||
if (_container == null) | ||
{ | ||
throw new InvalidOperationException("IContainer has not been set. Make sure to call ContainerProvider.SetContainer in your App class."); | ||
} | ||
return _container; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace Jamesnet.Core; | ||
|
||
public class DefaultViewModelInitializer : IViewModelInitializer | ||
{ | ||
private readonly IContainer _container; | ||
private readonly IViewModelMapper _viewModelMapper; | ||
|
||
public DefaultViewModelInitializer(IContainer container, IViewModelMapper viewModelMapper) | ||
{ | ||
_container = container; | ||
_viewModelMapper = viewModelMapper; | ||
} | ||
|
||
public void InitializeViewModel(IView view) | ||
{ | ||
var viewType = view.GetType(); | ||
var viewModelType = _viewModelMapper.GetViewModelType(viewType); | ||
|
||
if (viewModelType != null) | ||
{ | ||
var viewModel = CreateViewModel(viewModelType); | ||
view.DataContext = viewModel; | ||
} | ||
} | ||
|
||
private object CreateViewModel(Type viewModelType) | ||
{ | ||
try | ||
{ | ||
var constructor = viewModelType.GetConstructors() | ||
.OrderByDescending(c => c.GetParameters().Length) | ||
.First(); | ||
|
||
var parameters = constructor.GetParameters() | ||
.Select(p => _container.Resolve(p.ParameterType)) | ||
.ToArray(); | ||
|
||
return constructor.Invoke(parameters); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new InvalidOperationException($"Failed to create ViewModel of type {viewModelType.Name}.", ex); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace Jamesnet.Core; | ||
|
||
public interface IContainer | ||
{ | ||
void Register<TInterface, TImplementation>() where TImplementation : TInterface; | ||
void Register<TInterface, TImplementation>(string name) where TImplementation : TInterface; | ||
void RegisterSingleton<TInterface, TImplementation>() where TImplementation : TInterface; | ||
void RegisterSingleton<TInterface, TImplementation>(string name) where TImplementation : TInterface; | ||
void RegisterSingleton<TImplementation>(string name); | ||
void RegisterInstance<TInterface>(TInterface instance); | ||
void RegisterInstance<TInterface>(TInterface instance, string name); | ||
T Resolve<T>(); | ||
T Resolve<T>(string name); | ||
object Resolve(Type type); | ||
object Resolve(Type type, string name); | ||
bool TryResolve<T>(out T result); | ||
bool TryResolve<T>(string name, out T result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Jamesnet.Core; | ||
|
||
public interface ILayer | ||
{ | ||
object Content { get; set; } | ||
} |
Oops, something went wrong.