Datamodel - Simplified some things

This commit is contained in:
Robert committed 2020-06-26 13:00:43 +02:00
1 parent e184d8b724
commit 1bc8d65237
10 files changed
+111 -116

No files matched your search

@@ -0,0 +1,11 @@
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using Stylet;
namespace Artemis.UI.DataModelVisualization
{
public abstract class DataModelVisualizationViewModel : PropertyChangedBase
{
public DataModelPropertyAttribute PropertyDescription { get; protected set; }
public DataModelViewModel Parent { get; protected set; }
}
}
@@ -0,0 +1,23 @@
using System.Reflection;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using FastMember;
namespace Artemis.UI.DataModelVisualization
{
public class DataModelPropertyViewModel : DataModelVisualizationViewModel
{
private readonly ObjectAccessor _accessor;
public DataModelPropertyViewModel(PropertyInfo propertyInfo, DataModelPropertyAttribute propertyDescription, DataModelViewModel parent)
{
_accessor = ObjectAccessor.Create(parent.Model);
PropertyInfo = propertyInfo;
Parent = parent;
PropertyDescription = propertyDescription;
}
public PropertyInfo PropertyInfo { get; }
public object Value => _accessor[PropertyInfo.Name];
}
}
@@ -1,54 +1,57 @@
using System;
using System.Linq;
using Artemis.Core.Attributes;
using Artemis.Core.Plugins.Abstract.DataModels;
using Artemis.UI.Exceptions;
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
using Humanizer;
using Stylet;
namespace Artemis.UI.DataModelVisualization
{
public class DataModelViewModel : PropertyChangedBase
public class DataModelViewModel : DataModelVisualizationViewModel
{
public DataModelViewModel(DataModel dataModel)
public DataModelViewModel()
{
if (!DataModel.Initialized)
throw new ArtemisUIException("Cannot create view model for data model that is not yet initialized");
DataModel = dataModel;
Children = new BindableCollection<DataModelVisualizationViewModel>();
}
public DataModelViewModel(DataModel parent, DataModel dataModel)
public DataModelViewModel(object model, DataModelPropertyAttribute propertyDescription, DataModelViewModel parent)
{
if (!DataModel.Initialized)
throw new ArtemisUIException("Cannot create view model for data model that is not yet initialized");
Model = model;
PropertyDescription = propertyDescription;
Parent = parent;
DataModel = dataModel;
Children = new BindableCollection<DataModelVisualizationViewModel>();
PopulateProperties();
}
public DataModel DataModel { get; }
public DataModel Parent { get; set; }
public object Model { get; }
public BindableCollection<DataModelVisualizationViewModel> Children { get; set; }
private void PopulateProperties()
public void PopulateProperties()
{
foreach (var propertyInfo in DataModel.GetType().GetProperties())
Children.Clear();
foreach (var propertyInfo in Model.GetType().GetProperties())
{
var dataModelPropertyAttribute = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute));
if (dataModelPropertyAttribute == null)
// Skip properties decorated with DataModelIgnore
if (Attribute.IsDefined(propertyInfo, typeof(DataModelIgnoreAttribute)))
continue;
// For child data models create another data model view model
if (typeof(DataModel).IsAssignableFrom(propertyInfo.PropertyType))
var dataModelPropertyAttribute = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute));
// If no DataModelProperty attribute was provided, pull one out of our ass
if (dataModelPropertyAttribute == null)
dataModelPropertyAttribute = new DataModelPropertyAttribute {Name = propertyInfo.Name.Humanize()};
// For value types create a child view model if the value type is not null
if (propertyInfo.PropertyType.IsValueType)
{
var value = propertyInfo.GetValue(Model);
if (value == null)
continue;
Children.Add(new DataModelViewModel(value, dataModelPropertyAttribute, this));
}
// For primitives, create a property view model
// For primitives, create a property view model, it may be null that is fine
else if (propertyInfo.PropertyType.IsPrimitive)
{
}
// For anything else check if it has any child primitives and if so create a property container view model
else if (propertyInfo.PropertyType.GetProperties().Any(p => p.PropertyType.IsPrimitive))
{
Children.Add(new DataModelPropertyViewModel(propertyInfo, dataModelPropertyAttribute, this));
}
}
}
@@ -0,0 +1,30 @@
using Artemis.Core.Services.Interfaces;
using Artemis.UI.DataModelVisualization;
using Artemis.UI.Services.Interfaces;
namespace Artemis.UI.Services
{
public class DataModelVisualizationService : IDataModelVisualizationService
{
private readonly IDataModelService _dataModelService;
public DataModelVisualizationService(IDataModelService dataModelService)
{
_dataModelService = dataModelService;
}
public DataModelViewModel GetMainDataModelVisualization()
{
var viewModel = new DataModelViewModel();
foreach (var dataModelExpansion in _dataModelService.DataModelExpansions)
viewModel.Children.Add(new DataModelViewModel(dataModelExpansion, dataModelExpansion.DataModelDescription, viewModel));
return viewModel;
}
}
public interface IDataModelVisualizationService : IArtemisUIService
{
public DataModelViewModel GetMainDataModelVisualization();
}
}