yessql
yessql copied to clipboard
Make Get<T> available on the interface
It would be helpful if this method were available on the ISession interface:
https://github.com/sebastienros/yessql/blob/85bbc03ac8822407225b3d593c6b34569e1b6e26/src/YesSql.Core/Session.cs#L525
Do you have some usage examples? Is the idea that you could load documents, then want to get the corresponding instances afterwards?
In our application, we use a mix of yessql and stored procedures for queries. Our goal is to minimize the number of database calls. Therefore, when we want to retrieve various types of data, we don't always execute yessql queries. For example, it may happen that a stored procedure returns multiple datasets, one of which contains records stored in the Document table (which we want to convert into specific instances), while another dataset might be an int list that we read in a "traditional way".
The SP contains something like that:
-- 1st dataset:
select * from Document where ...
-- 2nd dataset
select Col1 from Table1 where ...
Reading the results in C#:
public async Task GetDataAsync()
{
...
var transaction = await _session.BeginTransactionAsync();
using var command = transaction!.Connection!.CreateCommand();
command.Transaction = transaction;
command.CommandText = "stored_procedure_name";
command.CommandType = CommandType.StoredProcedure;
using var dataReader = await command.ExecuteReaderAsync()
// 1st set
var contents = await ReadObjectsAsync<CustomContent>(dataReader);
// 2nd set
var intList = new List<int>();
await dataReader.NextResultAsync();
while (await dataReader.ReadAsync())
{
intList.Add(Convert.ToInt32(dataReader["Col1"]));
}
...
}
private async Task<IEnumerable<T>> ReadObjectsAsync<T>(DbDataReader dataReader, string collection = null) where T : class
{
var typeName = _typeService[typeof(T)];
var documents = new List<Document>();
while (await dataReader.ReadAsync())
{
var document = ReadDocument(dataReader, typeName);
if (document != default)
{
documents.Add(document);
}
}
if (documents.Count != 0)
{
// At this point, the requested interface method would be useful
var tObjects = (_session as Session).Get<T>(documents, collection);
return tObjects;
}
return [];
}
private Document ReadDocument(DbDataReader dataReader, string typeName)
{
var type = dataReader[nameof(Document.Type)].ToString();
if (type != typeName)
{
return default;
}
return new Document()
{
Id = Convert.ToInt64(dataReader[nameof(Document.Id)]),
Type = type,
Content = dataReader[nameof(Document.Content)].ToString(),
Version = Convert.ToInt64(dataReader[nameof(Document.Version)])
};
}