Skip to content

Commit

Permalink
entity mappings moved
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonStorm committed Jan 23, 2022
1 parent 32da50b commit 8a9a54e
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Dapper.FastCrud.Tests/CrudSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ public void WhenIPartiallyUpdateAllTheInsertedEmployeeEntities()
{
// prepare a new mapping
var defaultMapping = OrmConfiguration.GetDefaultEntityMapping<Employee>();
Assert.IsTrue(defaultMapping.IsFrozen);
Assert.IsTrue(defaultMapping.Registration.IsFrozen);

try
{
Expand Down
4 changes: 2 additions & 2 deletions Dapper.FastCrud/Formatters/SqlEntityFormattableParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ internal class SqlEntityFormattableParameter<TEntity>:SqlParameterFormatter
/// </summary>
public SqlEntityFormattableParameter(
SqlParameterElementType elementType,
string parameterValue,
EntityMapping entityMappingOverride)
string? parameterValue,
EntityMapping? entityMappingOverride)
: base(elementType, parameterValue, typeof(TEntity), entityMappingOverride)
{
}
Expand Down
111 changes: 77 additions & 34 deletions Dapper.FastCrud/Mappings/PropertyMapping(TEntityType).cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq.Expressions;

/// <summary>
Expand All @@ -27,6 +28,11 @@ internal PropertyMapping(PropertyMapping propertyRegistration)
/// </summary>
internal PropertyMapping Registration => _propertyRegistration;

/// <summary>
/// Gets the property name.
/// </summary>
public string PropertyName => _propertyRegistration.PropertyName;

/// <summary>
/// Marks the property as primary key.
/// </summary>
Expand Down Expand Up @@ -109,48 +115,18 @@ public PropertyMapping<TEntityType> RefreshOnUpdates(bool refreshOnUpdate = true
/// <summary>
/// The property will be included in insert operations.
/// </summary>
public PropertyMapping<TEntityType> IncludeInInserts()
public PropertyMapping<TEntityType> IncludeInInserts(bool includeInInserts = true)
{
_propertyRegistration.IsExcludedFromInserts = false;
return this;
}

/// <summary>
/// The property will be excluded from update operations.
/// </summary>
public PropertyMapping<TEntityType> ExcludeFromInserts()
{
_propertyRegistration.IsExcludedFromInserts = true;
_propertyRegistration.IsExcludedFromInserts = !includeInInserts;
return this;
}

/// <summary>
/// The property will be included in update operations.
/// </summary>
public PropertyMapping<TEntityType> IncludeInUpdates()
{
_propertyRegistration.IsExcludedFromUpdates = false;
return this;
}

/// <summary>
/// The property will be excluded from update operations.
/// </summary>
public PropertyMapping<TEntityType> ExcludeFromUpdates()
public PropertyMapping<TEntityType> IncludeInUpdates(bool includeInUpdates = true)
{
_propertyRegistration.IsExcludedFromUpdates = true;
return this;
}

/// <summary>
/// Sets up a foreign key relationship with another entity.
/// </summary>
/// <typeparam name="TParentEntityType">Foreign entity type.</typeparam>
/// <param name="referencingEntityPropertyName">The name of the property on the current entity that would hold the referenced entity when instructed to do so in a JOIN statement.</param>
[Obsolete(message: "Use the typed overload instead", error: false)]
public PropertyMapping<TEntityType> SetChildParentRelationship<TParentEntityType>(string referencingEntityPropertyName)
{
_propertyRegistration.ChildParentRelationship = new PropertyMappingRelationship(typeof(TParentEntityType), referencingEntityPropertyName, null);
_propertyRegistration.IsExcludedFromUpdates = !includeInUpdates;
return this;
}

Expand Down Expand Up @@ -183,5 +159,72 @@ public PropertyMapping<TEntityType> RemoveChildParentRelationship()
_propertyRegistration.ChildParentRelationship = null;
return this;
}

/// <summary>
/// Removes the current property mapping.
/// </summary>
public void Remove()
{
_propertyRegistration.EntityMapping.RemoveProperty(_propertyRegistration.PropertyName);
}

#region Obsolete methods and properties
[Obsolete("This method will be removed. Use IncludeInInserts(false) instead", error: false)]
public PropertyMapping<TEntityType> ExcludeFromInserts()
{
_propertyRegistration.IsExcludedFromInserts = true;
return this;
}

[Obsolete("This method will be removed. Use IncludeInUpdates(false) instead", error: false)]
public PropertyMapping<TEntityType> ExcludeFromUpdates()
{
_propertyRegistration.IsExcludedFromUpdates = true;
return this;
}

[Obsolete(message: "This method will be removed. Use the typed overload instead", error: false)]
public PropertyMapping<TEntityType> SetChildParentRelationship<TParentEntityType>(string referencingEntityPropertyName)
{
_propertyRegistration.ChildParentRelationship = new PropertyMappingRelationship(typeof(TParentEntityType), referencingEntityPropertyName, null);
return this;
}

[Obsolete(message:"This property will be removed. Use SetPrimaryKey instead.", error:false)]
public bool IsPrimaryKey
{
get => _propertyRegistration.IsPrimaryKey;
set => _propertyRegistration.IsPrimaryKey = value;
}

[Obsolete(message: "This property will be removed. Use RefreshOnInserts instead.", error: false)]
public bool IsRefreshedOnInserts
{
get => _propertyRegistration.IsRefreshedOnInserts;
set => _propertyRegistration.IsRefreshedOnInserts = value;
}

[Obsolete(message: "This property will be removed. Use RefreshOnUpdates instead.", error: false)]
public bool IsRefreshedOnUpdates
{
get => _propertyRegistration.IsRefreshedOnUpdates;
set => _propertyRegistration.IsRefreshedOnUpdates = value;
}

[Obsolete(message: "This property will be removed. Use IncludeInInserts(false) instead.", error: false)]
public bool IsExcludedFromInserts
{
get => _propertyRegistration.IsExcludedFromInserts;
set => _propertyRegistration.IsExcludedFromInserts = value;
}

[Obsolete(message: "This property will be removed. Use IncludeInUpdates(false) instead.", error: false)]
public bool IsExcludedFromUpdates
{
get => _propertyRegistration.IsExcludedFromUpdates;
set => _propertyRegistration.IsExcludedFromUpdates = value;
}

#endregion
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,6 @@ public int ColumnOrder
}
}

/// <summary>
/// Removes the current property mapping.
/// </summary>
public void Remove()
{
this.ValidateState();

this.EntityMapping.RemoveProperty(this.PropertyName);
}

internal PropertyMapping Clone(EntityMapping newEntityMapping)
{
var clonedPropertyMapping = new PropertyMapping(newEntityMapping, this.Descriptor)
Expand Down
18 changes: 3 additions & 15 deletions Dapper.FastCrud/Sql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,7 @@ public static IFormattable Identifier(string sqlIdentifier)
/// The resolver can be used as-is in a formattable string expression.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IFormattable Table()
{
return new SqlParameterFormatter(SqlParameterElementType.Table, null, null);
}

/// <summary>
/// Returns a parameter formatter for the SQL table name of the provided entity.
/// If you wish to resolve the table name of the main entity, please use the non-generic overload instead.
/// The resolver can be used as-is in a formattable string expression.
/// </summary>
/// <param name="entityMappingOverride">Overrides the entity mapping used in the query method.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IFormattable Table<TEntity>(EntityMapping<TEntity> entityMappingOverride)
public static IFormattable Table<TEntity>(EntityMapping<TEntity> entityMappingOverride = null)
{
return new SqlEntityFormattableParameter<TEntity>(SqlParameterElementType.Table, null, entityMappingOverride.Registration);
}
Expand All @@ -76,7 +64,7 @@ public static IFormattable Column(string propertyName)
/// <param name="propertyName">Name of the property (e.g. <code>nameof(entity.propname)</code>)</param>
/// <param name="entityMappingOverride">Overrides the entity mapping used in the query method.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IFormattable Column<TEntity>(string propertyName, EntityMapping<TEntity> entityMappingOverride)
public static IFormattable Column<TEntity>(string propertyName, EntityMapping<TEntity> entityMappingOverride = null)
{
Requires.NotNullOrEmpty(propertyName, nameof(propertyName));
return new SqlEntityFormattableParameter<TEntity>(SqlParameterElementType.Column, propertyName, entityMappingOverride.Registration);
Expand All @@ -101,7 +89,7 @@ public static IFormattable TableAndColumn(string propertyName)
/// <param name="propertyName">Name of the property (e.g. <code>nameof(entity.propname)</code>)</param>
/// <param name="entityMappingOverride">Overrides the entity mapping used in the query method.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IFormattable TableAndColumn<TEntity>(string propertyName, EntityMapping<TEntity> entityMappingOverride)
public static IFormattable TableAndColumn<TEntity>(string propertyName, EntityMapping<TEntity> entityMappingOverride = null)
{
Requires.NotNullOrEmpty(propertyName, nameof(propertyName));
return new SqlParameterFormatter(SqlParameterElementType.TableAndColumn, propertyName, entityMappingOverride.Registration);
Expand Down

0 comments on commit 8a9a54e

Please sign in to comment.