JasinCole
Well-known member
- Joined
- Feb 16, 2023
- Messages
- 66
- Programming Experience
- 1-3
Hi, I'm new here and recently found my way back to C# for a project I am working on.
While I can fudge my way through programming in lot's of languages I've never been proficient or effective in any language. Time to change that...
Given the below code, which works as designed, obvious issues of redundancy I am looking for some advise on how I can reduce the redudant code as this class grows.
I don't understand how I should handle the DbContext that both functions need. I read that the DbContext should be short lived so it would seem logicial to make that local to each function. But then if I created a third private function to return the employee record (that both functions need) and pass that back to the functions I'd still need a local DbContext for the Entry operations. Can the DbContext be a class field in this instance? What determines whether something is long or short lived?
This is where my lack of hard core fundamentals lets me down and I want to learn the right way and the thought process behind it.
While I can fudge my way through programming in lot's of languages I've never been proficient or effective in any language. Time to change that...
Given the below code, which works as designed, obvious issues of redundancy I am looking for some advise on how I can reduce the redudant code as this class grows.
I don't understand how I should handle the DbContext that both functions need. I read that the DbContext should be short lived so it would seem logicial to make that local to each function. But then if I created a third private function to return the employee record (that both functions need) and pass that back to the functions I'd still need a local DbContext for the Entry operations. Can the DbContext be a class field in this instance? What determines whether something is long or short lived?
This is where my lack of hard core fundamentals lets me down and I want to learn the right way and the thought process behind it.
Employee Record:
using DatabaseAccess.Entities;
using DatabaseAccess.Context;
using Microsoft.EntityFrameworkCore;
namespace DatabaseAccess;
public class EmployeeRecords
{
private readonly string? _connectionString;
public EmployeeRecords(string? connectionString)
{
_connectionString = connectionString;
}
public Employ GetRecord(int employeeNum)
{
using var ctx = new SageDbContext(_connectionString);
var employee = ctx.Employs
.FirstOrDefault(employee => employee.Recnum == employeeNum) ?? new Employ();
ctx.Entry(employee)
.Collection(e => e.Payrecs!)
.Load();
return employee;
}
public Employ GetRecord(int employeeNum, List<DateTime> payPeriods)
{
using var ctx = new SageDbContext(_connectionString);
var employee = ctx.Employs
.FirstOrDefault(employee => employee.Recnum == employeeNum) ?? new Employ();
ctx.Entry(employee)
.Collection(e => e.Payrecs!)
.Query()
.Where(payrec => payPeriods.Contains((DateTime)payrec.Payprd!))
.Load();
return employee;
}
}