Skip to content

Commit

Permalink
Updates all
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnet214 committed Sep 20, 2024
1 parent 6c9e4d3 commit 3c3dcf0
Show file tree
Hide file tree
Showing 2,048 changed files with 23,024 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Jamesnet.Core/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.Core/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.Core/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.Core/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.Core/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.Core/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.Core/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; }
}
10 changes: 10 additions & 0 deletions src/Jamesnet.Core/ILayerManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Jamesnet.Core;

public interface ILayerManager
{
void Register(string layerName, ILayer layer);
void Add(string layerName, IView view);
void Show(string layerName, IView view);
void Hide(string layerName);
void Mapping(string layerName, IView view);
}
6 changes: 6 additions & 0 deletions src/Jamesnet.Core/IView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Jamesnet.Core;

public interface IView
{
object DataContext { get; set; }
}
6 changes: 6 additions & 0 deletions src/Jamesnet.Core/IViewLoadable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Jamesnet.Core;

public interface IViewLoadable
{
void Loaded();
}
6 changes: 6 additions & 0 deletions src/Jamesnet.Core/IViewModelInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Jamesnet.Core;

public interface IViewModelInitializer
{
void InitializeViewModel(IView view);
}
7 changes: 7 additions & 0 deletions src/Jamesnet.Core/IViewModelMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Jamesnet.Core;

public interface IViewModelMapper
{
void Register<TView, TViewModel>() where TView : IView where TViewModel : class;
Type GetViewModelType(Type viewType);
}
4 changes: 4 additions & 0 deletions src/Jamesnet.Core/InjectAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class InjectAttribute : Attribute
{
}
9 changes: 9 additions & 0 deletions src/Jamesnet.Core/Jamesnet.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
Loading

0 comments on commit 3c3dcf0

Please sign in to comment.