English 中文(简体)
使用“每次请求”的方法使用“结构映射”和IDbconnection - 连接打开,但执行时关闭
原标题:Using the "per-request" approach with StructureMap and IDbConnection - the connection is opened but is closed when executed

试图用“ 结构图” 设置 Dapper 来使用“ 每一个请求” 的情景类型 。 在我 Global. asax 中, 我有以下功能( 从一个旧的“ Ayende s” 文章中修改, 涉及 Nhebertnate, 一个我发现在“ 结构图” 中正在讨论 < code> BuildUp () 方法 ):

protected static IDbConnection CreateConnection() { 
    var settings = ConfigurationManager.ConnectionStrings["MyConnectionString"];
    var connection = DbProviderFactories.GetFactory(settings.ProviderName).CreateConnection();
    if (connection == null) { 
        throw new ArgumentNullException("connection");
    }
    connection.ConnectionString = settings.ConnectionString;
    return connection;
}

public static IDbConnection CurrentConnection { 
    get { return (IDbConnection)HttpContext.Current.Items["current.connection"]; }
    set { HttpContext.Current.Items["current.connection"] = value; }
}

public Global() { 
    BeginRequest += (sender, args) => { 
        CurrentConnection = CreateConnection();
        CurrentConnection.Open(); 
    };

    EndRequest += (sender, args) => { 
        if (CurrentConnection == null) return;
        CurrentConnection.Close();
        CurrentConnection.Dispose();
    }
}

void Application_Start(object sender, EventArgs e) { 
    ObjectFactory.Initialize(x => { 
        x.For<IDbConnection>().Singleton().Use(CreateConnection());
        x.For<ICustomerRepository>().Use<CustomerRepository>();
        x.SetAllProperties(y => y.OfType<ICustomerRepository>());
    });
}

// BasePage.cs
public class BasePage : System.Web.UI.Page { 
    public IDbConnection CurrentConnection { get; set; }

    public BasePage() { 
        ObjectFactory.BuildUp(this);
    }
}

每次我试着调用这个时, 实际的查询都会失败, 错误显示连接当前状态已关闭, 虽然在“ 开始请求” 处理器上有一个断点显示正在调用 Open () 连接 。

如果我手动打给Open 并关闭每个存储器方法中的 IDB连接,这似乎有效, 但我尽量避免这样做,如果可能的话。

最佳回答

您正在将连接创建为单吨。 这意味着整个应用程序页面将只使用一个连接对象。 您在应用程序_ Start 处理器中新建的连接从未被页面使用, 因为他们将从容器中获取连接 。

你最好用这样的东西:

void Application_Start(object sender, EventArgs e) { 
    ObjectFactory.Initialize(x => { 
        x.For<IDbConnection>().HttpContextScoped().Use(() => CreateConnection());
        ...
    }
}

 public Global() { 
    EndRequest += (sender, args) => { 
        ObjectFactory.GetInstance<IDbConnection>.Dispose();
    }
 }
问题回答

暂无回答




相关问题
Structuremap (or any IoC, really) architecture question

I m going to use structuremap for a project I m working on. The basic gist is that I have a repository pattern with an NHibernate implementation, but I want to use StructureMap to load the ...

Inject static property with StructureMap?

is it possible inject static property, like I do below, because it does not work for me? public static IMerchantModule MerchantModule { get; set; } public RequestBaseValidationRules() { ...

Injecting Subsonic SimpleRepository class to controller

I m tryingot get started with IoC, I have an MVC project in which is use subsonic, I m trying to inject subsonic simplerepository to my controllers but I m getting this error: StructureMap Exception ...

StrucutureMap RhinoMock Record/Playback, Example needed

I m looking for some examples on how to do the following Mock Tests using StructureMap or Unity with NUnit. I have the following code structure public interface IDAL { List<Model> Method1(...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

热门标签