Here is the sample code for Provider Model pattern along with Singleton pattern using C#.
public abstract class TestProviderBase : System.Configuration.Provider.ProviderBase
{
public abstract List<Test> GetAllTests();
}
{
public abstract List<Test> GetAllTests();
}
public class TestProvider : TestProviderBase
{
public override List<Test> GetAllTests()
{
string connString = ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand command = new SqlCommand("select dbo.Test.ColA,dbo.Test.ColB from dbo.Test", conn))
{
return Common.ConvertReaderToTest(command.ExecuteReader());
}
}
}
}
{
public override List<Test> GetAllTests()
{
string connString = ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand command = new SqlCommand("select dbo.Test.ColA,dbo.Test.ColB from dbo.Test", conn))
{
return Common.ConvertReaderToTest(command.ExecuteReader());
}
}
}
}
public sealed class TestServices
{
static TestProvider testInstance = null;
static readonly object padlock = new object();
TestServices()
{
}
public static TestProvider provider
{
get
{
lock (padlock)
{
if (testInstance == null)
{
testInstance = new TestProvider();
}
return testInstance;
}
}
}
}
{
static TestProvider testInstance = null;
static readonly object padlock = new object();
TestServices()
{
}
public static TestProvider provider
{
get
{
lock (padlock)
{
if (testInstance == null)
{
testInstance = new TestProvider();
}
return testInstance;
}
}
}
}
Code to execute:
GridView1.DataSource = TestServices.provider.GetAllTests();
GridView1.DataBind();
GridView1.DataBind();
No comments:
Post a Comment