在ASP.NET中编写自己的提供程序类 [英] Writing my own Provider Class in ASP.NET

查看:109
本文介绍了在ASP.NET中编写自己的提供程序类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我不想编写自定义成员资格提供程序.

Note: I DON't want to write custom membership provider.

我想编写自己的Provider类,以便可以在web.config中定义它并像Membership类一样访问它.

I want to write my own Provider class so I can define it in web.config and access it like Membership class.

这是我的课程的示例(它有许多其他静态方法):

Here is a sample of my class (it has many other static methods):

public static class MySqlHelper
{
    private static string constring = ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;

    public static int ExecuteNonQuery(string mysqlquery)
    {
        SqlConnection conn = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand(mysqlquery, conn);
    int result;

    try
    {
        conn.Open();
        result= cmd.ExecuteNonQuery();
    }
    finally
    {
        conn.Close();
    }
    return result;

    }
}

用法:MySqlHelper.ExecuteNonQuery("select * from customers");

现在,如您所见,我已经硬编码了连接字符串的名称,即"MyConnString".我正计划使其变得动态.

Now as you see I have hard-coded the name of connectionstring i.e. "MyConnString". I am planning to make it dynamic.

因此,我想知道是否可以使其像静态的内置Membership类一样,在其中可以在web.config中定义connectionStringName.通过这种方式,可以使类可重用,而不必始终将web.config中的连接字符串命名为"MyConnString".

So I was wondering if I can make it like static built-in Membership class, where I can define the connectionStringName in web.config. This way the class can be made re-usable without always naming my connectionstring in web.config to "MyConnString".

1:我不想在每个静态方法中将连接字符串作为参数传递.

1: I DON'T want to pass connectionstring in every static method as a parameter.

2:我必须能够访问类似于Membership.CreateUser的方法,即静态.

2: I must be able to access the methods similar to Membership.CreateUser i.e. static.

我正在并行浏览网络,但是任何输入/指导都会有所帮助.

I am looking over the web in parallel but any inputs/guidance will help.

已编辑:我已经更新了代码示例,以消除一些有关使用静态类的问题的困惑.这是我发布的一个新问题 .抱歉造成混乱.

Edited: I have updated my code sample, to clear some confusion about issues using static class. Here is a new question I posted to clarify that. Sorry about confusion.

推荐答案

我能想到的唯一符合您提出的条件的方法是使用依赖项注入,静态构造函数,并注入类似.这似乎是我能想到的最复杂的事情,所以您可能会喜欢. :)

the only thing i can think of that meets the qualifications you laid out is to use dependency injection, a static constructor, and inject in an something like an IConnectionStringProvider. this seems like about the most convoluted thing i can think of, so you might like it. :)

在阅读您的评论后,似乎您只想能够引用任何连接字符串,但每个应用程序只能引用一个连接字符串.我想说的只是将一个元素添加到名为MySqlProviderConnectionappSettings中,该值是您要使用的连接字符串的名称.

after reading your comment, it seems like you just want to be able to reference any connection string, but only one connection string per application. i'd say just add an element to appSettings named MySqlProviderConnection with the value being the name of the connection string you want to use.

然后在您的帮助器中,检查该appsetting是否存在,获取其值,并将其传递给您的ConfigurationManager.ConnectionStrings调用.这样,您的提供商就可以使用所需的任何连接,而无需更改任何代码.

then in your helper, check for the existence of the appsetting, get its value, and pass it in to your ConfigurationManager.ConnectionStrings call. that way your provider could use any connection you want, without changing any code.

这篇关于在ASP.NET中编写自己的提供程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆