Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnet214 committed Sep 21, 2024
1 parent 288e45a commit a187dfe
Show file tree
Hide file tree
Showing 40 changed files with 679 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/Jamesnet.Core/BaseResourceLoader.cs
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;
Expand Down
4 changes: 4 additions & 0 deletions src/Jamesnet.Core/Container.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Jamesnet.Core;

public class Container : IContainer
Expand Down
4 changes: 3 additions & 1 deletion src/Jamesnet.Core/ContainerProvider.cs
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
{
Expand Down
3 changes: 3 additions & 0 deletions src/Jamesnet.Core/DefaultViewModelInitializer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Linq;

namespace Jamesnet.Core;

public class DefaultViewModelInitializer : IViewModelInitializer
Expand Down
2 changes: 2 additions & 0 deletions src/Jamesnet.Core/IContainer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace Jamesnet.Core;

public interface IContainer
Expand Down
4 changes: 3 additions & 1 deletion src/Jamesnet.Core/IViewModelMapper.cs
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
{
Expand Down
4 changes: 3 additions & 1 deletion src/Jamesnet.Core/InjectAttribute.cs
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
{
}
6 changes: 3 additions & 3 deletions src/Jamesnet.Core/Jamesnet.Core.csproj
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>
3 changes: 3 additions & 0 deletions src/Jamesnet.Core/LayerManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Collections.Generic;

namespace Jamesnet.Core;

public class LayerManager : ILayerManager
Expand Down
1 change: 1 addition & 0 deletions src/Jamesnet.Core/RelayCommand.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Windows.Input;

namespace Jamesnet.Core;
Expand Down
2 changes: 2 additions & 0 deletions src/Jamesnet.Core/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

Expand Down
5 changes: 4 additions & 1 deletion src/Jamesnet.Core/ViewModelMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Jamesnet.Core;
using System;
using System.Collections.Generic;

namespace Jamesnet.Core;

public class ViewModelMapper : IViewModelMapper
{
Expand Down
4 changes: 4 additions & 0 deletions src/Jamesnet.Core/YamlConverter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace Jamesnet.Core
Expand Down
4 changes: 3 additions & 1 deletion src/Jamesnet.Core/YamlData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Jamesnet.Core;
using System.Collections.Generic;

namespace Jamesnet.Core;

public class YamlData : List<YamlItem>
{
Expand Down
5 changes: 4 additions & 1 deletion src/Jamesnet.Core/YamlExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Jamesnet.Core;
using System;
using System.Collections.Generic;

namespace Jamesnet.Core;

public static class YamlExtensions
{
Expand Down
3 changes: 3 additions & 0 deletions src/Jamesnet.Core/YamlItem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using System.Collections.Generic;

namespace Jamesnet.Core;

public class YamlItem : Dictionary<string, string>
Expand Down
37 changes: 37 additions & 0 deletions src/Jamesnet.Core2/AppBootstrapper.cs
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();
}
43 changes: 43 additions & 0 deletions src/Jamesnet.Core2/BaseResourceLoader.cs
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;
}
}
100 changes: 100 additions & 0 deletions src/Jamesnet.Core2/Container.cs
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);
}
}
20 changes: 20 additions & 0 deletions src/Jamesnet.Core2/ContainerProvider.cs
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;
}
}
46 changes: 46 additions & 0 deletions src/Jamesnet.Core2/DefaultViewModelInitializer.cs
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);
}
}

}
18 changes: 18 additions & 0 deletions src/Jamesnet.Core2/IContainer.cs
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);
}
6 changes: 6 additions & 0 deletions src/Jamesnet.Core2/ILayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Jamesnet.Core;

public interface ILayer
{
object Content { get; set; }
}
Loading

0 comments on commit a187dfe

Please sign in to comment.