patterncsharpMinor
Service class with db context
Viewed 0 times
withserviceclasscontext
Problem
I have implemented service which working with db context. I am using entity framework and I do not need repositories because logic is quite simple.
I want to keep simple logic but clean approach.
One problem I have is when I want to call service method 2 times in same service instance. I will get context dispose exception.
this is not problem in my case because for now I do not have case like that.
But maybe you have idea for better solution.
my code:
```
using Common.Configurations;
using Common.Container;
using Common.Factory;
using Common.Helpers;
using Common.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using RemoteManager.EntityFramework;
using Common.Entities;
namespace RemoteManagerApi.Services
{
public class MachineService : IMachineService
{
private readonly IOptions applicationConfiguration;
private readonly ILogger logger;
private readonly IMachineCleaner machineCleaner;
private readonly IFileContainer fileContainer;
private readonly RemoteManagerContext context;
public MachineService(RemoteManagerContext context, IFileContainerFactory fileContainerFactory, IOptions applicationConfiguration, IMachineCleaner machineCleaner, ILogger logger)
{
this.fileContainer = fileContainerFactory.CreateContainer();
this.applicationConfiguration = applicationConfiguration;
this.logger = logger;
this.machineCleaner = machineCleaner;
this.context = context;
}
public async Task Add(MachineConfiguration machine)
{
try
{
await this.fileContainer.CreateDirectory(machine.SessionFolder);
logger.LogInformation($"Start {nameof(MachineService)}:{nameof(this.Add)}");
using (var db = this.context)
{
var res
I want to keep simple logic but clean approach.
One problem I have is when I want to call service method 2 times in same service instance. I will get context dispose exception.
this is not problem in my case because for now I do not have case like that.
But maybe you have idea for better solution.
my code:
```
using Common.Configurations;
using Common.Container;
using Common.Factory;
using Common.Helpers;
using Common.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using RemoteManager.EntityFramework;
using Common.Entities;
namespace RemoteManagerApi.Services
{
public class MachineService : IMachineService
{
private readonly IOptions applicationConfiguration;
private readonly ILogger logger;
private readonly IMachineCleaner machineCleaner;
private readonly IFileContainer fileContainer;
private readonly RemoteManagerContext context;
public MachineService(RemoteManagerContext context, IFileContainerFactory fileContainerFactory, IOptions applicationConfiguration, IMachineCleaner machineCleaner, ILogger logger)
{
this.fileContainer = fileContainerFactory.CreateContainer();
this.applicationConfiguration = applicationConfiguration;
this.logger = logger;
this.machineCleaner = machineCleaner;
this.context = context;
}
public async Task Add(MachineConfiguration machine)
{
try
{
await this.fileContainer.CreateDirectory(machine.SessionFolder);
logger.LogInformation($"Start {nameof(MachineService)}:{nameof(this.Add)}");
using (var db = this.context)
{
var res
Solution
You're injecting a disposable resource (
But your code is wrapping
Remove the
context) - this means someone else is creating it (which is excellent BTW), and that someone else should therefore be responsible for disposing the context.But your code is wrapping
context accesses in a using block, which is going to call Dispose on that context when execution leaves the using scope.Remove the
using scopes - it's not this class' job to dispose the context. If you're using an IoC container for dependency injection, it's the container that's owning the instances, and therefore it's the container's responsibility to dispose them. If you're not using an IoC container, then whoever is newing up the RemoteManagerContext is the owner that needs to dispose it.Context
StackExchange Code Review Q#146659, answer score: 7
Revisions (0)
No revisions yet.