Asp.NetCore 中每个数据库访问的模拟

问题描述

在解释问题之前先了解一些信息。 我们是一个数据库进行通信的应用程序(在 asp.netcore 中)。

据我所知,asp.netcore 的文档说不要模仿,但有一个可行的解决方法

在我们的案例中,它仅部分起作用,正如您在此处看到的 (Impersonation Middleware in an Asp.Net Core Intranet app for Windows-Identity)。在这种情况下,模拟从 asp.netCore 项目的启动开始。

现在我希望模拟每个数据库访问。意味着当我调用“更新”方法时,我只想模拟这部分,如果我保存新索引,我只想模拟保存过程。 我目前的方法是使用适配器。适配器在我的网络应用程序中,我需要在我的 DataAccessLayer 项目中进行模拟(首先是实现可以进行单元测试的代码)。

适配器如下所示:

public void Impersonate(Action action)
{
    if (action is null) throw new ArgumentNullException(nameof(action));

    var winIdent = _context?.User?.Identity as WindowsIdentity;

    // If we run the code as localhost we don't need the WindowsIdentity here.
    if (winIdent is null)
    {
        action();
    }
    else
    {
        WindowsIdentity.RunImpersonated(winIdent.Accesstoken,action);
    }
}

在 DataAccessLayer 中,它看起来像这样:

_impersonation.Impersonate(() =>
{
    // Code which runs impersonated.
});  

乍一看一切正常,但也许有更好的方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)