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

Reduce Code duplication in AccountDataService even further #6

Open
duhowise opened this issue May 16, 2020 · 2 comments
Open

Reduce Code duplication in AccountDataService even further #6

duhowise opened this issue May 16, 2020 · 2 comments

Comments

@duhowise
Copy link

AccountDataService could inherit from NonQueryDataService thereby removing the need to write out or have to explicitly delegate functionality to NonQueryDataService... here's what I mean:

public class AccountDataService : NonQueryDataService<Account>, IDataService<Account>
{
    private readonly SimpleTraderDbContextFactory _contextFactory;

    public AccountDataService(SimpleTraderDbContextFactory contextFactory) : base(contextFactory)
    {
        _contextFactory = contextFactory;
    }

    public async Task<IEnumerable<Account>> GetAll()
    {
        await using var context = _contextFactory.CreateDbContext(new[] {""});
        var list = await context.Accounts.Include(x => x.AssetTransactions).Include(x => x.AccountHolder)
            .ToListAsync();
        return list;
    }

    public async Task<Account> Get(int id)
    {
        await using var context = _contextFactory.CreateDbContext(new[] {""});
        var entity = await context.Accounts.Include(x => x.AssetTransactions).Include(x => x.AccountHolder)
            .FirstOrDefaultAsync(x => x.Id == id);
        return entity;
    }
}
@SingletonSean
Copy link
Owner

SingletonSean commented May 16, 2020

Looking back, I think this is cleaner. I try to limit using inheritance due to the coupling created between the base and derived classes, but I think this is definitely a situation where inheritance is preferable.

I'm not sure I will go back and change this since the current code is acceptable, but I think future DataServices will simply inherit from NonQueryDataService. Thank you your input duhowise, including the code snippet example! I love code snippets.

@Twodio
Copy link

Twodio commented Apr 29, 2021

@duhowise or instead of all these inheritance, using virtual methods on the GenericDataService and override them on the AccoundDataService would just get the job done, and remove the need for the NonQueryDataService.

danzuep added a commit to danzuep/SimpleTrader that referenced this issue Oct 5, 2023
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

3 participants