使数据模块能够使用 --- pass-Store-use 可重用数据模块内的连接

发布于 2024-07-11 05:07:12 字数 1979 浏览 5 评论 0原文

我想将站点的数据模块分成一个程序集(单个dll文件), 在处理 Web 应用程序时,获取存储并传递站点的 ConnectionString 的最佳方式是什么? 在数据程序集中,我创建了一个名为 ConnectionManager 的静态类。 它有一个名为 DatabaseConnectionName 的属性,我想传递并存储 Web.Config 文件中的连接名称。 在此策略中,我决定获取名称并在网站加载时在 Global.asax 文件中建立连接,并在我之前提到的属性 (DatabaseConnectionName) 中进行连接。 但是,这只是我使用的策略,我不知道完成这项工作的常见模式是什么。

部分代码: ======================================= [ ------------ Global.asax ------------ ]

the code in the site that makes the Data module accessible for the site
    void Application_Start(object sender, EventArgs e) 
    {
         OurCompany.Data.ConnectionManager.DatabaseConnectionName = "MasterConnection";
    }

[ ------------ < strong>ConnectionManager类 ------------ ] 这是在数据模块中,除了站点

public static class ConnectionManager
{

    public static SqlConnection  GetMasterConnection()
    {
        string connectionString = ConfigurationManager.ConnectionStrings[**DatabaseConnectionName**].ConnectionString;
        SqlConnection conn;     
        //conn.Open();
        conn = new SqlConnection(connectionString);
        return  conn;
    }

    private static string **databaseConnectionName**;
    public static string DatabaseConnectionName
    {
        get
        {
            return databaseConnectionName;
        }
        set
        {
            databaseConnectionName = value;
        }
    }

== END ======================================= ======================

--- 问题是: ---

  1. 在哪里存储连接? (这是 ConnectionManager 类中的一个属性, theCompany.Data.ConnectionManager.DatabaseConnectionName )

  2. 何时建立此连接? (这里是 Global.asax 应用程序加载时间)

  3. 哪种方法最适合存储此类信息 :SessionState或ViewState或一个简单的属性

  4. 这个策略好吗? 你知道有什么更好的方法或者 这是常见的模式吗?

感谢您提供任何信息 - MHM -

I want to seperate the data module of a site into an assembly ( a single dll file ) ,
What is the best way to get-store and pass the ConnectionString of the site in dealing with the web application .
Inside the data assembly I made a static class named ConnectionManager . It has a property named DatabaseConnectionName , that I want to pass and store the connection namewhich is inside Web.Config file .
In this strategy I decided to get the name and make the connection in the load time of the website at the Global.asax file and stroe that in the property I mentioned earlier ( DatabaseConnectionName ) .
But , This is just the strategy that I used , I dont know what is the common pattern for doing this job .

Parts of code : =====================================
[ ------------ Global.asax ------------ ]

the code in the site that makes the Data module accessible for the site
    void Application_Start(object sender, EventArgs e) 
    {
         OurCompany.Data.ConnectionManager.DatabaseConnectionName = "MasterConnection";
    }

[ ------------ ConnectionManager Class ------------ ]
this is in the data module apart from the site

public static class ConnectionManager
{

    public static SqlConnection  GetMasterConnection()
    {
        string connectionString = ConfigurationManager.ConnectionStrings[**DatabaseConnectionName**].ConnectionString;
        SqlConnection conn;     
        //conn.Open();
        conn = new SqlConnection(connectionString);
        return  conn;
    }

    private static string **databaseConnectionName**;
    public static string DatabaseConnectionName
    {
        get
        {
            return databaseConnectionName;
        }
        set
        {
            databaseConnectionName = value;
        }
    }

== END ===========================================================

--- Questions are : ---

  1. Where to store the connection ?
    ( here was a property inside the ConnectionManager Class ,
    theCompany.Data.ConnectionManager.DatabaseConnectionName )

  2. When make this connection ?
    ( here was at Global.asax Application Load time )

  3. Which method is best for storing such information
    : SessionState or ViewState or a simple property

  4. Is this strategy good ? Do you know any better way or
    the common pattern for this ?

Thanks for any information
- MHM -

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

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

发布评论

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

评论(1

红尘作伴 2024-07-18 05:07:12

一些想法...

  1. 您不应该存储并挂在打开的数据库连接上。 打开连接,执行数据库操作,然后立即关闭。 应用晚获取、早释放的规则。

  2. 请参阅第 1 点。

  3. 不要将数据库连接存储在会话状态中。 再次参见第 1 点。 如果您指的是连接字符串,那么只需在需要时从配置管理器中读取它即可。 它已经为您缓存了,不要重新发明或白费力气。

  4. 我建议您查看模式和实践企业库,它抽象了管理数据访问的许多常见模式:

http://www.codeplex.com/entlib

A few thoughts...

  1. You shouldn't be storing and hanging onto an open database connection. Open the connection, do your database operations, then close immediately. Apply the rule of acquire late, release early.

  2. See point 1.

  3. Don't store database connections in session state. See point 1 again. If you mean the connection string, then just read it from the configuration manager when you need it. It's already cached for you, don't re-invent or wrap the wheel.

  4. I suggest you take a look at the patterns and practices enterprise library which abstracts away many of the common patterns of managing data access:

http://www.codeplex.com/entlib

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