Skip to content

Commit

Permalink
Provides a "Ignore" attribute to ignore properties when mapping the P…
Browse files Browse the repository at this point in the history
…OCO class to the entity (#49)

* Provides a "Ignore" attribute to ignore properties when mapping the POCO to the entity.
Adds Guid to the list of "primitive" types

* Adds UnitTests for the new IgnoreAttribute.

---------

Co-authored-by: Vincent Pellichero <vincent.pellichero@oniryx.be>
  • Loading branch information
vpellichero and Vincent Pellichero committed Jan 22, 2024
1 parent 04e67ad commit d894f21
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 18 deletions.
12 changes: 12 additions & 0 deletions Src/Core/Annotations/IgnoreAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Apps72.Dev.Data.Annotations
{
/// <summary>
/// Specifies that the property shouldn't be part of the database mapping.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class IgnoreAttribute : Attribute
{
}
}
14 changes: 6 additions & 8 deletions Src/Core/Convertor/DataReaderConvertor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Apps72.Dev.Data.Annotations;
using Apps72.Dev.Data.Schema;
using System;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using Apps72.Dev.Data.Annotations;
using Apps72.Dev.Data.Schema;

namespace Apps72.Dev.Data.Convertor
{
Expand All @@ -17,16 +17,15 @@ internal static ColumnsAndRows<T> ToType<T>(DbDataReader reader)
// No data
if (!hasRow)
{
return new ColumnsAndRows<T>
{
return new ColumnsAndRows<T> {
Columns = new DataColumn[0],
Rows = new T[0]
};
}

// Class Properties
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanWrite);
.Where(p => p.CanWrite && p.GetCustomAttribute<IgnoreAttribute>() is null);

// DataTable Columns
var names = Enumerable.Range(0, reader.FieldCount)
Expand Down Expand Up @@ -71,8 +70,7 @@ internal static ColumnsAndRows<T> ToType<T>(DbDataReader reader)
} while (reader.Read());

// Return
return new ColumnsAndRows<T>()
{
return new ColumnsAndRows<T>() {
Columns = columns.Keys,
Rows = rows
};
Expand Down
3 changes: 2 additions & 1 deletion Src/Core/Convertor/TypeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public static bool IsPrimitive(Type type)
type == typeof(UIntPtr) || type == typeof(Nullable<UIntPtr>) ||
type == typeof(Char) || type == typeof(Nullable<Char>) ||
type == typeof(Double) || type == typeof(Nullable<Double>) ||
type == typeof(Single) || type == typeof(Nullable<Single>);
type == typeof(Single) || type == typeof(Nullable<Single>) ||
type == typeof(Guid) || type == typeof(Nullable<Guid>);
}

/// <summary>
Expand Down
19 changes: 10 additions & 9 deletions Src/Core/Schema/DataParameter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Apps72.Dev.Data.Convertor;
using System;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using Apps72.Dev.Data.Annotations;
using Apps72.Dev.Data.Convertor;

namespace Apps72.Dev.Data.Schema
{
Expand Down Expand Up @@ -59,6 +60,9 @@ public static void AddValues<T>(DbCommand command, T values)
List<DbParameter> parameters = new List<DbParameter>();
foreach (PropertyInfo property in typeof(T).GetProperties())
{
if (property.GetCustomAttribute<IgnoreAttribute>() != null)
continue;

if (TypeExtension.IsPrimitive(property.PropertyType))
{
// Data type
Expand All @@ -76,15 +80,12 @@ public static void AddValues<T>(DbCommand command, T values)
parameter.Value = DBNull.Value;

// Parameter name
parameter.ParameterName = property.Name;

string attribute = Apps72.Dev.Data.Annotations.ColumnAttribute.GetColumnAttributeName(property);
if (string.IsNullOrEmpty(attribute))
{
parameter.ParameterName = property.Name;
}
else
{
if (!string.IsNullOrEmpty(attribute))
parameter.ParameterName = attribute;
}


parameters.Add(parameter);
}
Expand Down
84 changes: 84 additions & 0 deletions Test/Core/Annotations/IgnoreAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Data.SqlClient;
using Apps72.Dev.Data;
using Data.Core.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Core.Tests.Annotations
{
/*
To run most of these tests, you must have the SCOTT database (scott.sql)
and you need to configure your connection string (configuration.cs)
*/

[TestClass]
public class IgnoreAttributeTests
{
#region INITIALIZATION

private SqlConnection _connection;

[TestInitialize]
public void Initialization()
{
_connection = new SqlConnection(Configuration.CONNECTION_STRING);
_connection.Open();
}

[TestCleanup]
public void Finalization()
{
if (_connection != null)
{
_connection.Close();
_connection.Dispose();
}
}

#endregion{

[TestMethod]
public void ToParameters_WhenIgnoreAttribute_DontAddParameter()
{
// Arrange
var data = new DEPTWithIgnoredProperty() {
DeptNo = 70,
Loc = "BRUSSELS"
};
var cmd = new DatabaseCommand(_connection);

// Act
cmd.AddParameter(data);

// Assert
Assert.AreEqual(2, cmd.Parameters.Count);
Assert.AreEqual("@DeptNo", cmd.Parameters[0].ParameterName);
Assert.AreEqual("@Loc", cmd.Parameters[1].ParameterName);
}

[TestMethod]
public void DatabaseRead_WhenIgnoreAttribute_IgnoreProperty()
{
using (var cmd = new DatabaseCommand(_connection))
{
cmd.Log = Console.WriteLine;
cmd.CommandText = "SELECT * FROM DEPT WHERE DEPTNO = 10";
var result = cmd.ExecuteRow<DEPTWithIgnoredProperty>();

Assert.AreEqual(10, result.DeptNo);
Assert.AreEqual("IgnoredValue", result.DName);
Assert.AreEqual("NEW YORK", result.Loc);
}
}
}

internal class DEPTWithIgnoredProperty
{
public virtual int DeptNo { get; set; }

[Apps72.Dev.Data.Annotations.Ignore]
public virtual string DName { get; set; } = "IgnoredValue";

public virtual string Loc { get; set; }
}
}

0 comments on commit d894f21

Please sign in to comment.