StructureMap DBServiceRegistry 和 MVC 迷你分析器?

发布于 2024-11-16 21:16:08 字数 1295 浏览 5 评论 0原文

如果我在每个 Repository 类中使​​用此代码,那么我可以进行 SQL 分析,但我想将该代码从每个类移动到 StructureMap 处理数据库的类中。

Repository 类的示例:

public DB CreateNewContext()
    {
        var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
        var profiledConnection = ProfiledDbConnection.Get(sqlConnection);
        return DataContextUtils.CreateDataContext<DB>(profiledConnection);
    }

    public SqlRecipeRepository(DB dataContext)
    {         
        _db = CreateNewContext();
    }

现在我希望 dataContext 变量成为分析版本,因此来自我的 DBServiceRegistry 类。

这是 DBServiceRegistry 类:

public class DBServiceRegistry : Registry
{
    public DBServiceRegistry()
    {
        var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["GetMeCooking.Data.Properties.Settings.server"].ConnectionString);
        var profiledConnection = ProfiledDbConnection.Get(sqlConnection);
        For<DB>().HybridHttpOrThreadLocalScoped().Use(() => DataContextUtils.CreateDataContext<DB>(profiledConnection));

        //Original method just had this:
        //For<DB>().HybridHttpOrThreadLocalScoped().Use(() => new DB());

    }
}

此代码不会导致任何错误,但我没有得到 SQL 分析,我做错了什么?

If I use this code in each Repository class then I get SQL profiling to work but I want to move that code from each class into the class where StructureMap handles the DB.

Example of a Repository class:

public DB CreateNewContext()
    {
        var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
        var profiledConnection = ProfiledDbConnection.Get(sqlConnection);
        return DataContextUtils.CreateDataContext<DB>(profiledConnection);
    }

    public SqlRecipeRepository(DB dataContext)
    {         
        _db = CreateNewContext();
    }

Now I want the dataContext variable to be the profiled version and so come from my DBServiceRegistry class.

Here is the DBServiceRegistry class:

public class DBServiceRegistry : Registry
{
    public DBServiceRegistry()
    {
        var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["GetMeCooking.Data.Properties.Settings.server"].ConnectionString);
        var profiledConnection = ProfiledDbConnection.Get(sqlConnection);
        For<DB>().HybridHttpOrThreadLocalScoped().Use(() => DataContextUtils.CreateDataContext<DB>(profiledConnection));

        //Original method just had this:
        //For<DB>().HybridHttpOrThreadLocalScoped().Use(() => new DB());

    }
}

This code does not cause any errors but I don't get the SQL profiling, what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里人 2024-11-23 21:16:08

注释是正确的,通过在 For 行之外创建 sql 连接,您将覆盖范围命令。

将所有内容封装到匿名委托中要好得多

using System.Configuration;
using System.Data.SqlClient;
using System.Threading.Tasks;

using StructureMap;
using StructureMap.Configuration.DSL;

using Xunit;

public class DBServiceRegistry : Registry
{
    private string connString = ConfigurationManager.ConnectionStrings["GetMeCooking.Data.Properties.Settings.server"].ConnectionString;

    public DBServiceRegistry()
    {
        For<DB>().HybridHttpOrThreadLocalScoped().Use(
            () =>
                {
                    var sqlConnection = new SqlConnection(connString);
                    var profiledConnection = new StackExchange.Profiling.Data.ProfiledDbConnection(sqlConnection,  MiniProfiler.Current);
                    return DataContextUtils.CreateDataContext<DB>(profiledConnection);
                });
    }
}

您可以使用单元测试来验证范围是否正确(测试语法是 xunit.net)

public class DBRegistryTests : IDisposable
{
    private Container container;

    public DBRegistryTests()
    {
        // Arrange (or test setup)
        container = new Container(new DBServiceRegistry());
    }

    [Fact]
    public void ConnectionsAreSameInThread()
    {
        // Create two connections on same thread
        var conn1 = container.GetInstance<DB>();
        var conn2 = container.GetInstance<DB>();

        // Assert should be equal because hybrid thread is scope 
        // and test executes on same thread
        Assert.Equal(conn1, conn2);

        // Other assertions that connection is profiled
    }

    [Fact]
    public void ConnectionAreNotSameInDifferentThreads()
    {
        var conn1 = container.GetInstance<DB>();

        // Request second connection from a different thread 
        // (for < c# 4.0 use Thread instead of task)
        var conn2 = new Task<DB>(() => this.container.GetInstance<DB>());
        conn2.Start();
        conn2.Wait();

        // Assert that request from two different threads 
        // are not the same
        Assert.NotEqual(conn1, conn2.Result);
    }

    public void Dispose()
    {
        // Test teardown
        container.Dispose();
    }
}

The comment is correct, by creating the sql connection outwith the For line, you are overriding the scope command.

Far better to encapsulate the whole lot into an anonymous delegate

using System.Configuration;
using System.Data.SqlClient;
using System.Threading.Tasks;

using StructureMap;
using StructureMap.Configuration.DSL;

using Xunit;

public class DBServiceRegistry : Registry
{
    private string connString = ConfigurationManager.ConnectionStrings["GetMeCooking.Data.Properties.Settings.server"].ConnectionString;

    public DBServiceRegistry()
    {
        For<DB>().HybridHttpOrThreadLocalScoped().Use(
            () =>
                {
                    var sqlConnection = new SqlConnection(connString);
                    var profiledConnection = new StackExchange.Profiling.Data.ProfiledDbConnection(sqlConnection,  MiniProfiler.Current);
                    return DataContextUtils.CreateDataContext<DB>(profiledConnection);
                });
    }
}

You can use unit tests to verify that the scope is correct (test syntax is xunit.net)

public class DBRegistryTests : IDisposable
{
    private Container container;

    public DBRegistryTests()
    {
        // Arrange (or test setup)
        container = new Container(new DBServiceRegistry());
    }

    [Fact]
    public void ConnectionsAreSameInThread()
    {
        // Create two connections on same thread
        var conn1 = container.GetInstance<DB>();
        var conn2 = container.GetInstance<DB>();

        // Assert should be equal because hybrid thread is scope 
        // and test executes on same thread
        Assert.Equal(conn1, conn2);

        // Other assertions that connection is profiled
    }

    [Fact]
    public void ConnectionAreNotSameInDifferentThreads()
    {
        var conn1 = container.GetInstance<DB>();

        // Request second connection from a different thread 
        // (for < c# 4.0 use Thread instead of task)
        var conn2 = new Task<DB>(() => this.container.GetInstance<DB>());
        conn2.Start();
        conn2.Wait();

        // Assert that request from two different threads 
        // are not the same
        Assert.NotEqual(conn1, conn2.Result);
    }

    public void Dispose()
    {
        // Test teardown
        container.Dispose();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文