我有一個.net核心api,它在不同的數據庫中使用不同的dbcontexts。包裝帶多個dbcontexts的作用域類
我已經寫了包裝所有dbcontexts類:
public class BoundedContext : IDisposable
{
public EcommerceDbContext EcommerceContext { get; }
public SecurityDbContext SecurityContext { get; }
public CRMDbContext CrmContext { get; }
public BoundedContext(string EcommerceConnectionString,
string SecurityConnectionString,
string CRMConnectionString)
{
EcommerceContext = new EcommerceDbContext(EcommerceConnectionString);
SecurityContext = new SecurityDbContext(SecurityConnectionString);
CrmContext = new CRMDbContext(CRMConnectionString);
}
public void SaveChanges()
{
if (SecurityContext != null)
SecurityContext.SaveChanges();
if (CrmContext != null)
CrmContext.SaveChanges();
if (EcommerceContext != null)
EcommerceContext.SaveChanges();
}
public void Dispose()
{
if (SecurityContext != null)
SecurityContext.Dispose();
if (CrmContext != null)
CrmContext.Dispose();
if (EcommerceContext != null)
EcommerceContext.Dispose();
}
}
在我已經將它定義爲一個範圍的情況下啓動類:
services.AddScoped((_) => new BoundedContext(Configuration["Data:Ecommerce:ConnectionString"],
Configuration["Data:Security:ConnectionString"],
Configuration["Data:CRM:ConnectionString"]));
控制器動作被調用一個靜態類路過一人或幾個「命令」,所以這個類是負責執行它並承諾修改
namespace Test.Business.Services
{
public static class CommandService
{
static BoundedContext _context;
public static void Process(BoundedContext context, IEnumerable<ICommand> commands)
{
_context = context;
//actions
foreach (var command in commands)
{
command.Execute(_context);
}
foreach (var command in commands)
{
if (command is IBulkInsertCommand)
{
(command as IBulkInsertCommand).BulkInsert();
}
}
//commit
_context.SaveChanges();
//post actions
foreach (var command in commands)
{
if (command is IPostCommitCommand)
{
(command as IPostCommitCommand).PostCommitAction();
_context.SaveChanges();
}
}
}
}
}
我有一個.net核心網站,用swagger生成的sdk調用這個api。網絡的控制器有一個過濾器,以獲得登錄用戶的屬性:
public override void OnActionExecuting(ActionExecutingContext context)
{
if (User.Identity.IsAuthenticated)
{
if (_currentUser == null)
{
_currentUser = ApiHandler.GetCurrentUser(_applicationId, _accessToken);
}
return _currentUser;
}
return null;
}
和動作的一個樣本:
// GET: /<controller>/
[HttpGet("{All}")]
public async Task<IActionResult> Index([FromRoute]string All)
{
GetNotificationResponse result = await ControllerHandler.GetNotifications(All, _accessToken());
return PartialView("~/Views/Notifications/v1/NotificationsList.cshtml",result);
}
我們稱一個jQuery AJAX調用這個動作。問題是,有時我們在「OnActionExecuting」中收到System.ObjectDisposedException,但我不知道爲什麼,因爲管理dbcontexts的類是使用scoped選項注入的。
你認爲這個架構很糟糕還是我錯過了什麼?