IDbConnection 和 StructureMap 的问题

发布于 2024-11-27 03:52:03 字数 1115 浏览 0 评论 0原文

我在 IContainer Initialize 例程中进行了以下配置:

x.For<IDbConnection>().Use<SqlConnection>().Ctor<string>().Is(MY_SQL_CONNECTION_STRING);

这是我的服务的构造函数:

private readonly IForumRepository _repo;
public ForumService(IForumRepository repo)
{
    _repo = repo;
}

这是我的存储库的构造函数:

private readonly IDbConnection _cn;
public ForumRepository(IDbConnection connection)
{
    _cn = connection;
}

在我的一个服务例程中,我调用一个方法来获取存储库中的一个对象,然后返回到我在存储库中调用第二个方法的服务层 - 但是,在对存储库的第二次调用中,我的连接 (_cn) 不再具有与之关联的连接字符串(在退出 using {} 块时,它似乎被擦除了我的服务第一次打电话 我

这是我在存储库中调用的第一个方法,

public Tag GetTag(int id)
{
    Tag o;
    const string q = @"select * from tags where id = @pId";

    using (_cn)
    {
        _cn.Open();
        o = _cn.Query<Tag>(q, new { pId = id }).SingleOrDefault();
    } *** AT THIS POINT THE CONN STRING PROPERTY OF _CN IS CLEARED?!

    return o;
}

认为这应该可以正常工作,因为我的服务的构造函数实例化了对存储库的第一次调用使用良好的 SQL 连接。 我认为存储库的 SqlConnection 依赖项仍应保留原始连接字符串详细信息。

对我的存储库的后续调用位于相同的服务范围内,

I have the following configured within my IContainer Initialize routine:

x.For<IDbConnection>().Use<SqlConnection>().Ctor<string>().Is(MY_SQL_CONNECTION_STRING);

Here's the constructor for my service:

private readonly IForumRepository _repo;
public ForumService(IForumRepository repo)
{
    _repo = repo;
}

And here's the constructor for my repository:

private readonly IDbConnection _cn;
public ForumRepository(IDbConnection connection)
{
    _cn = connection;
}

In one of my service routines, I am calling a method to get an object in my repository, then upon returning to the service layer I call a 2nd method in my repository - however, on this second call to the repository, my connection (_cn) no longer has a connection string associated with it (it appears to be wiped upon exiting the using {} block the first time my service called a method in the repository.

Here's the first method that I call in the repository.

public Tag GetTag(int id)
{
    Tag o;
    const string q = @"select * from tags where id = @pId";

    using (_cn)
    {
        _cn.Open();
        o = _cn.Query<Tag>(q, new { pId = id }).SingleOrDefault();
    } *** AT THIS POINT THE CONN STRING PROPERTY OF _CN IS CLEARED?!

    return o;
}

I 'd have thought this should work fine seeing as the constructor for my service instantiates a SQL Connection which the first call to the repo utilizes fine, and seeing as subsequent calls to my repo are in the same service scope I'd have thought that repository's SqlConnection dependency should still retain the original connection string details.

Can someone please shed some light on where I'm going wrong with this?

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

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

发布评论

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

评论(1

说好的呢 2024-12-04 03:52:03

using 块完成后,正在使用的对象将被释放。连接字符串在处置时是否会被删除?无论如何,您可能不应该在释放该对象后使用该对象。

根据您帖子中的代码,我认为您不想使用 using 块?不过,我认为您需要手动关闭连接(我认为 using 将作为处置的一部分自动执行此操作)。

After the using block is finished, the object being used will be disposed. Does the connection string get removed on disposal? In any case, you likely shouldn't use the object after is has been disposed.

Based on the code in your post, I would think you don't want to use the using block? I think you will want to manually close the connection, though (I think using will do that automatically as part of disposal).

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