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

Make Get<T> available on the interface #579

Open
intellimedhu opened this issue Jul 18, 2024 · 2 comments
Open

Make Get<T> available on the interface #579

intellimedhu opened this issue Jul 18, 2024 · 2 comments

Comments

@intellimedhu
Copy link

It would be helpful if this method were available on the ISession interface:

public IEnumerable<T> Get<T>(IList<Document> documents, string collection) where T : class

@sebastienros
Copy link
Owner

Do you have some usage examples?
Is the idea that you could load documents, then want to get the corresponding instances afterwards?

@intellimedhu
Copy link
Author

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)])
    };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants