public void ResourceConfigurationBuilder_ConfigureRelations_ShouldMapRelations() { IResourceConfigurationBuilder builder = new ResourceConfigurationBuilder <Resource>("resource") .BelongsTo <Parent>(x => x.Parent) .HasMany <Child>(x => x.Children); var config = builder.Build(); Assert.That(config.Mapping.GetAttributeNames().Count, Is.EqualTo(3)); var names = config.Mapping.GetRelationNames().ToList(); Assert.That(names.Count, Is.EqualTo(2)); var testResource = new Resource(); var testId = Guid.NewGuid(); config.Mapping.SetRelationValue(testResource, "Parent", testId); Assert.That(testResource.Parent, Is.EqualTo(testId)); var testIds = new[] { Guid.NewGuid(), Guid.NewGuid() }; config.Mapping.SetRelationValue(testResource, "Children", testIds); Assert.That(testResource.Children, Is.EqualTo(testIds)); }
public void ResourceConfigurationBuilder_ZeroConfiguration_ShouldMapAllNonIdPropertiesAsAttributes() { IResourceConfigurationBuilder builder = new ResourceConfigurationBuilder <Resource>("resource"); var config = builder.Build(); Assert.That(config.TypeName, Is.EqualTo("resource")); Assert.That(config.ResourceType, Is.EqualTo(typeof(Resource))); var names = config.Mapping.GetAttributeNames().ToList(); Assert.That(names.Count, Is.EqualTo(5)); Assert.That(names, Does.Contain("OtherId")); Assert.That(names, Does.Contain("Name")); Assert.That(names, Does.Contain("Active")); Assert.That(names, Does.Contain("Parent")); Assert.That(names, Does.Contain("Children")); var testResource = new Resource(); var testId = Guid.NewGuid(); config.Mapping.SetId(testResource, testId); Assert.That(testResource.Id, Is.EqualTo(testId)); Assert.That(testResource.Active, Is.False); config.Mapping.SetAttributeValue(testResource, "Active", true); Assert.That(testResource.Active, Is.True); }
private static IReadOnlyJsonRuntimeConfigurationProvider BuildRuntimeConfigurationProvider() { IEnumerable <IReadOnlyJsonResourceConfiguration> resourceConfigurations = ResourceConfigurationBuilder.Build <IReadOnlyJsonPropertyConfiguration, IReadOnlyJsonResourceConfiguration, IReadOnlyJsonModelConfiguration>(typeof(JsonResourceConfigurationBuilder <>)); var runtimeConfigurations = new Dictionary <Type, IReadOnlyJsonRuntimeConfiguration <IRestResource> >(); foreach (IReadOnlyJsonResourceConfiguration configuration in resourceConfigurations) { string json = File.ReadAllText(configuration.ModelConfiguration.JsonFilePath); Type genericListType = typeof(List <>).MakeGenericType(configuration.ResourceType); MethodInfo genericMethod = typeof(JsonConvert) .GetMethod(nameof(JsonConvert.DeserializeObject), 1, new Type[] { typeof(string) }) .MakeGenericMethod(genericListType); // Create the generic list of resource objects object resourceList = genericMethod.Invoke(null, new object[] { json }); var runtimeConfiguration = Activator.CreateInstance( typeof(ReadOnlyJsonRuntimeConfiguration <>).MakeGenericType(configuration.ResourceType), configuration, resourceList) as IReadOnlyJsonRuntimeConfiguration <IRestResource>; runtimeConfigurations.Add(configuration.ResourceType, runtimeConfiguration); } return(new ReadOnlyJsonRuntimeConfigurationProvider(runtimeConfigurations)); }
public void ResourceConfigurationBuilder_IgnoreIdProperty_ShouldThrowException() { IResourceConfigurationBuilder builder = new ResourceConfigurationBuilder <Resource>("resource") .IgnoreProperty(x => x.Id); var ex = Assert.Throws <Exception>(() => builder.Build()); Assert.That(ex.Message, Does.Contain("No id property defined")); }
public void ResourceConfigurationBuilder_IgnoreProperty_ShouldExcludePropertyFromMapping() { IResourceConfigurationBuilder builder = new ResourceConfigurationBuilder <Resource>("resource") .IgnoreProperty(x => x.OtherId); var config = builder.Build(); var names = config.Mapping.GetAttributeNames().ToList(); Assert.That(names.Count, Is.EqualTo(4)); Assert.That(names, Does.Not.Contain("OtherId")); Assert.That(config.Mapping.GetRelationNames().Count(), Is.Zero); }
public void ResourceConfigurationBuilder_ConfigureIdProperty_ShouldMapSelectedProperty() { IResourceConfigurationBuilder builder = new ResourceConfigurationBuilder <Resource>("resource") .WithIdProperty(x => x.OtherId); var config = builder.Build(); Assert.That(config.Mapping.GetAttributeNames(), Does.Not.Contain("OtherId")); var testResource = new Resource(); var testId = Guid.NewGuid(); config.Mapping.SetId(testResource, testId); Assert.That(testResource.OtherId, Is.EqualTo(testId)); }
private static ISqlRuntimeConfigurationProvider BuildRuntimeConfigurationProvider() { IEnumerable <ISqlResourceConfiguration> resourceConfigurations = ResourceConfigurationBuilder.Build <ISqlPropertyConfiguration, ISqlResourceConfiguration, ISqlModelConfiguration>(typeof(SqlResourceConfigurationBuilder <>)); // No need to register these with the DI container, as they are only used during startup ISqlParamaterProvider sqlParamaterProvider = new SqlParameterProvider(); ISqlGenerator generator = new SqlGenerator(sqlParamaterProvider); ISqlExpressionBuilder sqlExpressionBuilder = new SqlExpressionBuilder(sqlParamaterProvider, new SqlClassProvider()); Dictionary <Type, ISqlRuntimeConfiguration <IRestResource> > runtimeConfigurations = new Dictionary <Type, ISqlRuntimeConfiguration <IRestResource> >(); foreach (ISqlResourceConfiguration configuration in resourceConfigurations) { string selectAll = generator.SelectAll(configuration); string selectById = generator.SelectById(configuration); string deleteById = generator.DeleteById(configuration); string updateById = generator.Update(configuration); string insert = generator.Insert(configuration); var createObjectRelationalMapFunc = typeof(ISqlExpressionBuilder) .GetMethod(nameof(ISqlExpressionBuilder.CreateObjectRelationalMap)) .MakeGenericMethod(configuration.ResourceType) .Invoke(sqlExpressionBuilder, new[] { configuration }) as Func <IDatabaseResultReader, IRestResource>; var getPrimaryKeySqlParameterFunc = sqlExpressionBuilder.GetPrimaryKeySqlParameter(configuration.PrimaryIdentifier); var getSqlParametersForCreationFunc = sqlExpressionBuilder.GetSqlParametersForCreation(configuration); var getSqlParametersForUpdatingFunc = sqlExpressionBuilder.GetSqlParametersForUpdating(configuration); var runtimeConfiguration = Activator.CreateInstance( typeof(SqlRuntimeConfiguration <>).MakeGenericType(configuration.ResourceType), configuration, createObjectRelationalMapFunc, getPrimaryKeySqlParameterFunc, getSqlParametersForCreationFunc, getSqlParametersForUpdatingFunc, selectAll, selectById, deleteById, insert, updateById) as ISqlRuntimeConfiguration <IRestResource>; runtimeConfigurations.Add(configuration.ResourceType, runtimeConfiguration); } return(new SqlRuntimeConfigurationProvider(runtimeConfigurations)); }
private static IInMemoryRuntimeConfigurationProvider BuildRuntimeConfigurationProvider() { IEnumerable <IInMemoryResourceConfiguration> resourceConfigurations = ResourceConfigurationBuilder.Build <IInMemoryPropertyConfiguration, IInMemoryResourceConfiguration, IInMemoryModelConfiguration>(typeof(InMemoryResourceConfigurationBuilder <>)); var runtimeConfigurations = new Dictionary <Type, IInMemoryRuntimeConfiguration <IRestResource> >(); foreach (IInMemoryResourceConfiguration configuration in resourceConfigurations) { object resourceList = null; // Read any initial data from the JSON file specified if possible if (configuration.ModelConfiguration.JsonFilePath != null) { try { string json = File.ReadAllText(configuration.ModelConfiguration.JsonFilePath); Type genericListType = typeof(List <>).MakeGenericType(configuration.ResourceType); MethodInfo genericMethod = typeof(JsonConvert) .GetMethod(nameof(JsonConvert.DeserializeObject), 1, new Type[] { typeof(string) }) .MakeGenericMethod(genericListType); // Create the generic list of resource objects resourceList = genericMethod.Invoke(null, new object[] { json }); } catch (Exception ex) { throw new InvalidDataException( $"An error occurred while reading the initial JSON data for {configuration.ResourceType.Name} ({configuration.ModelConfiguration.JsonFilePath}). " + $"Ensure that it is well formed and the correct property types are used. See inner exception for details.", ex); } } var runtimeConfiguration = Activator.CreateInstance( typeof(InMemoryRuntimeConfiguration <>).MakeGenericType(configuration.ResourceType), configuration, resourceList) as IInMemoryRuntimeConfiguration <IRestResource>; runtimeConfigurations.Add(configuration.ResourceType, runtimeConfiguration); } return(new InMemoryRuntimeConfigurationProvider(runtimeConfigurations)); }
private static ICustomRuntimeConfigurationProvider BuildRuntimeConfigurationProvider() { IEnumerable <ICustomResourceConfiguration> resourceConfigurations = ResourceConfigurationBuilder.Build <ICustomPropertyConfiguration, ICustomResourceConfiguration, ICustomModelConfiguration>(typeof(CustomResourceConfigurationBuilder <>)); var runtimeConfigurations = new Dictionary <Type, ICustomRuntimeConfiguration <IRestResource> >(); // Create a runtime configuration for each resource type and pass it to a runtime configuration provider instance to be registered with the DI container foreach (ICustomResourceConfiguration configuration in resourceConfigurations) { var runtimeConfiguration = Activator.CreateInstance( typeof(CustomRuntimeConfiguration <>).MakeGenericType(configuration.ResourceType), configuration) as ICustomRuntimeConfiguration <IRestResource>; runtimeConfigurations.Add(configuration.ResourceType, runtimeConfiguration); } return(new CustomRuntimeConfigurationProvider(runtimeConfigurations)); }