-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provides a "Ignore" attribute to ignore properties when mapping the P…
…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
1 parent
04e67ad
commit d894f21
Showing
5 changed files
with
114 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |