Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A small change that makes it possible to pass ObjectReader as SqlDbType.Structured parameter to a stored procedure call #97

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions FastMember/ObjectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ public class ObjectReader : DbDataReader
private readonly string[] memberNames;
private readonly Type[] effectiveTypes;
private readonly BitArray allowNull;
private readonly string keyPropertyName;

/// <summary>
/// Creates a new ObjectReader instance for reading the supplied data
/// </summary>
/// <param name="source">The sequence of objects to represent</param>
/// <param name="keyPropertyName">Current object property name to set IsKey column value for schema table</param>
/// <param name="members">The members that should be exposed to the reader</param>
public static ObjectReader Create<TSource>(IEnumerable<TSource> source, string keyPropertyName, params string[] members)
{
return new ObjectReader(typeof(TSource), source, keyPropertyName, members);
}

/// <summary>
/// Creates a new ObjectReader instance for reading the supplied data
Expand All @@ -26,20 +38,21 @@ public class ObjectReader : DbDataReader
/// <param name="members">The members that should be exposed to the reader</param>
public static ObjectReader Create<T>(IEnumerable<T> source, params string[] members)
{
return new ObjectReader(typeof(T), source, members);
return new ObjectReader(typeof(T), source, members: members);
}

/// <summary>
/// Creates a new ObjectReader instance for reading the supplied data
/// </summary>
/// <param name="type">The expected Type of the information to be read</param>
/// <param name="source">The sequence of objects to represent</param>
/// <param name="keyPropertyName">Current object property name to set IsKey column value for schema table</param>
/// <param name="members">The members that should be exposed to the reader</param>
public ObjectReader(Type type, IEnumerable source, params string[] members)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a hard break; we can't do that; whatever API changes must be binary compatible; I'm less concerned about build compatible, but that would be nice too.

Adding IsKey itself: fine

Copy link
Author

@Gagarin23 Gagarin23 Mar 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UPD Key property can be selected via expression
image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I create another constructor for backwards compatibility
image

public ObjectReader(Type type, IEnumerable source, string keyPropertyName = "", params string[] members)
{
if (source == null) throw new ArgumentOutOfRangeException("source");

if (source == null) throw new ArgumentOutOfRangeException(nameof(source));

this.keyPropertyName = keyPropertyName;

bool allMembers = members == null || members.Length == 0;

Expand Down Expand Up @@ -119,17 +132,21 @@ public override DataTable GetSchemaTable()
{"ColumnName", typeof(string)},
{"DataType", typeof(Type)},
{"ColumnSize", typeof(int)},
{"AllowDBNull", typeof(bool)}
{"AllowDBNull", typeof(bool)},
{"IsKey", typeof(bool)}
}
};
object[] rowData = new object[5];
for (int i = 0; i < memberNames.Length; i++)
{
var memberName = memberNames[i];

rowData[0] = i;
rowData[1] = memberNames[i];
rowData[1] = memberName;
rowData[2] = effectiveTypes == null ? typeof(object) : effectiveTypes[i];
rowData[3] = -1;
rowData[4] = allowNull == null ? true : allowNull[i];
rowData[5] = memberName == keyPropertyName;
table.Rows.Add(rowData);
}
return table;
Expand All @@ -147,6 +164,7 @@ public override bool HasRows
}
}
private bool active = true;

public override bool NextResult()
{
active = false;
Expand Down