Reputation: 682
I want to implement factory pattern Architecture. I have created Interface with parameterized function.
1) Step 1 :
public interface IDatabase
{
bool Create(string userId,string password,string host,string dbName);
bool Delete(string userId, string password, string host, string dbName);
}
Step 2:
This interface is Implemented in below class:-
public class IntialDBSetup : IDatabase
{
public bool Create(string userId, string password, string host, string dbName)
{
SqlConnection con = new SqlConnection("Data Source=" + host + ";uid=" + userId + ";pwd=" + password + "");
try
{
string strCreatecmd = "create database " + dbName + "";
SqlCommand cmd = new SqlCommand(strCreatecmd, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
var file = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ScriptLocation"]));
string strscript = file.OpenText().ReadToEnd();
string strupdatescript = strscript.Replace("[OWpress]", dbName);
var server = new Microsoft.SqlServer.Management.Smo.Server(new Microsoft.SqlServer.Management.Common.ServerConnection(con));
server.ConnectionContext.ExecuteNonQuery(strupdatescript);
con.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
public bool Delete(string userId, string password, string host, string dbName)
{
throw new NotImplementedException();
}
}
Step 3:
created factory class
public class DBFactory
{
public static IDatabase DbSetup(string DbType, string userId, string password, string host, string dbName)
{
try
{
if (DbType == DBTypeEnum.IntialDB.ToString())
{
return new IntialDBSetup();
}
}
catch (Exception ex)
{
throw new ArgumentException("DB Type Does not exist in our Record");
}
return null;
}
}
Here I want to pass some parameter to my class, Hgow can I get this?
Upvotes: 2
Views: 480
Reputation: 109100
Add a constructor to your class.
If DBFactory
and IntialDBSetup
are in the same assembly then that constructor can be marked internal
(preventing code outside the assembly creating instances directly).
If the factory method was a static
member of IntialDBSetup
then the constructor can be private
preventing direct creation even in the same assembly.
Upvotes: 1