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

Generic Type Model #49

Open
aa-development-portal opened this issue Feb 2, 2022 · 1 comment
Open

Generic Type Model #49

aa-development-portal opened this issue Feb 2, 2022 · 1 comment

Comments

@aa-development-portal
Copy link

How can I make this function generic :

return Context.LoadStoredProc("dbo.ListAll")
.AddParam("limit", 300L)
.AddParam("limitOut", out IOutParam limitOut)
.Exec(r => rows = r.ToListAsync());

@shawty
Copy link

shawty commented Mar 10, 2022

I'm not the maintainer of this library, just a user of it, but I had a similar challenge tonight and thought I'd reveal how I got it to work in a generic fashion.

I have a very standard EF core data context

public class PbxDataContext : DbContext
{
  public PbxDataContext(DbContextOptions<PbxDataContext> options) : base(options)
  {}

  // Standard table entity models
  public DbSet<CallerIdDirectory>? CallerDirectory { get; set; }
}

Nothing too complicated.

Then as a public method to my DC I added the following function

  public List<T> ExecuteTableFn<T>(string fnName) where T : class, new()
  {
    List<T> rows = new List<T>();

    this.LoadStoredProc(fnName)
      .Exec(r =>
          rows = r.ToList<T>()
      );

    return rows;
  }

NOTE the : class, new() on the end of the method signature? this is needed in order to match the signature of ToList.

Once this is added , you can use it quite simply as follows

    results.CallCountsByYear = 
      _dc.ExecuteTableFn<YearlyCallCount>("pbx_unknowncallcountsbyyear");

Put your own concrete type in the type parameter, and the name of the fn to call in the quote marks.

I HAVE NOT yet added sending any parameters in, as I don't actually need any for what I'm doing, but it shouldn't take you too much to figure out.

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