我想创建一个类,使用工厂来找到正确的类,然后调用该类的方法。 我做错了什么,虽然因为在Visual Studio中的intellisense不断警告我的错误,当我尝试访问类应该在工厂返回他们不可用的方法。类的通用工厂?
谁能告诉我我做错了什么?
这里就是我想要去的类的引用代码:
DBBase dal = DALFactory.GetDAL(typeof(T));
myCollection = dal.GetByCriteria(myCriteria, sortExpression, startRowIndex, maximumRows, propertyNamesToBypassInstantiation);
下面是DALFactory类的代码:
internal static class DALFactory
{
// GetParser
internal static DBBase GetDAL(System.Type BOType)
{
switch (BOType.Name)
{
case "Person":
return new PersonDB();
}
// if we reach this point then we failed to find a matching type. Throw
// an exception.
throw new Exception("Unknown Type");
}
}
下面是部分代码由工厂类返回的类的基类:
public abstract class DBBase
{
protected static T GetSingleBO<T>(ref SqlCommand command) where T : BOBase
{
return GetSingleBO<T>(ref command, null);
}
protected static T GetSingleBO<T>(ref SqlCommand command, List<string> propertyNamesToBypassInstantiation) where T : BOBase
{
T BO = default(T);
try
{
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
reader.Read();
BOParser parser = BOParserFactory.GetParser(typeof(T));
parser.PopulateOrdinals(reader);
if (propertyNamesToBypassInstantiation == null)
{
BO = (T)parser.PopulateBO(reader);
}
else
{
BO = (T)parser.PopulateBO(reader, propertyNamesToBypassInstantiation);
}
reader.Close();
}
else
{
// Whever there's no data, we return null.
BO = default(T);
}
}
catch (Exception ex)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Error populating data: {0}", ex.Message);
if (ex.InnerException != null)
{
sb.AppendFormat(" -- Inner Exception: {0}", ex.InnerException.Message);
}
throw new Exception(sb.ToString(), ex);
}
finally
{
command.Connection.Close();
command.Connection.Dispose();
}
// return the BO, it's either populated with data or null.
return BO;
}
protected static EquatableList<T> GetBOEquatableList<T>(ref SqlCommand command) where T : BOBase
{
return GetBOEquatableList<T>(ref command, null);
}
protected static EquatableList<T> GetBOEquatableList<T>(ref SqlCommand command, List<string> propertyNamesToBypassInstantiation) where T : BOBase
{
EquatableList<T> BOList = new EquatableList<T>();
try
{
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
// Get a parser for this BO type and populate
// the ordinals.
BOParser parser = BOParserFactory.GetParser(typeof(T));
parser.PopulateOrdinals(reader);
// Use the parser to build our list of BOs.
while (reader.Read())
{
T BO = default(T);
if (propertyNamesToBypassInstantiation == null)
{
BO = (T)parser.PopulateBO(reader);
}
else
{
BO = (T)parser.PopulateBO(reader, propertyNamesToBypassInstantiation);
}
BOList.Add(BO);
}
reader.Close();
}
}
catch (Exception ex)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Error populating data: {0}", ex.Message);
if (ex.InnerException != null)
{
sb.AppendFormat(" -- Inner Exception: {0}", ex.InnerException.Message);
}
throw new Exception(sb.ToString(), ex);
}
finally
{
command.Connection.Close();
command.Connection.Dispose();
}
return BOList;
}
protected static int GetBOCount(ref SqlCommand command)
{
int count = 0;
try
{
command.CommandType = CommandType.StoredProcedure;
DbParameter idParam = command.CreateParameter();
idParam.DbType = DbType.Int32;
idParam.Direction = ParameterDirection.InputOutput;
idParam.ParameterName = "@recordCount";
idParam.Value = 0;
command.Parameters.Add(idParam);
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
count = (int)command.Parameters["@recordCount"].Value;
}
catch (Exception e)
{
throw new Exception("Error populating data", e);
}
finally
{
command.Connection.Close();
command.Connection.Dispose();
}
return count;
}
protected static bool DeleteBO(ref SqlCommand command)
{
int result = 0;
try
{
command.Connection.Open();
result = command.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception("Error deleting data", e);
}
finally
{
command.Connection.Close();
command.Connection.Dispose();
}
return result > 0;
}
protected static int GetFKcount(ref SqlCommand command)
{
int count = 0;
try
{
command.CommandType = CommandType.StoredProcedure;
DbParameter idParam = command.CreateParameter();
idParam.DbType = DbType.Int32;
idParam.Direction = ParameterDirection.InputOutput;
idParam.ParameterName = "@recordCount";
idParam.Value = 0;
command.Parameters.Add(idParam);
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
count = (int)command.Parameters["@recordCount"].Value;
}
catch (Exception e)
{
throw new Exception("Error populating data", e);
}
finally
{
command.Connection.Close();
command.Connection.Dispose();
}
return count;
}
public static T GetById<T>(Guid Id)
where T : BOBase
{
return GetById<T>(Id, null);
}
abstract public static T GetById<T>(Guid Id, List<string> propertyNamesToBypassInstantiation)
where T : BOBase;
public static EquatableList<T> GetByCriteria<T,TCriteria>(TCriteria myCriteria)
where T : BOBase
where TCriteria : BaseCriteria
{
return GetByCriteria<T, TCriteria>(myCriteria, null);
}
public static EquatableList<T> GetByCriteria<T, TCriteria>(TCriteria myCriteria, List<string> propertyNamesToBypassInstantiation)
where T : BOBase
where TCriteria : BaseCriteria
{
return GetByCriteria<T, TCriteria>(myCriteria, null, -1, -1, propertyNamesToBypassInstantiation);
}
public static EquatableList<T> GetByCriteria<T, TCriteria>(TCriteria myCriteria, string sortExpression, int startRowIndex, int maximumRows)
where T : BOBase
where TCriteria : BaseCriteria
{
return GetByCriteria<T, TCriteria>(myCriteria, sortExpression, startRowIndex, maximumRows, null);
}
abstract public static EquatableList<T> GetByCriteria<T, TCriteria>(TCriteria myCriteria, string sortExpression, int startRowIndex, int maximumRows, List<string> propertyNamesToBypassInstantiation)
where T : BOBase
where TCriteria : BaseCriteria;
abstract public static int GetCountForCriteria<TCriteria>(TCriteria myCriteria)
where TCriteria : BaseCriteria;
abstract public static bool Save<T>(T myobject)
where T : BOBase;
abstract public static bool Delete<T>(Guid Id)
where T : BOBase;
abstract public static int GetFKcount<T>(Guid Id)
where T : BOBase;
abstract internal static void CreateCriteriaParameters<T, TCriteria>(ref SqlCommand myCommand, TCriteria myCriteria)
where T : BOBase
where TCriteria : BaseCriteria;
}
而这是最终的代码的一部分b应该得到返回ottom类:
public partial class PersonDB : DBBase
{
public override static Person GetById(Guid Id, List<string> propertyNamesToBypassInstantiation)
{
SqlCommand command = GetDbSprocCommand("sprocPersonSelectSingleItem");
command.Parameters.Add(CreateParameter("@id", Id));
return GetSingleBO<Person>(ref command, propertyNamesToBypassInstantiation);
}
public override static EquatableList<Person> GetByCriteria(PersonCriteria myCriteria, string sortExpression, int startRowIndex, int maximumRows, List<string> propertyNamesToBypassInstantiation)
{
SqlCommand command = GetDbSprocCommand("sprocPersonSearchList");
CreateCriteriaParameters(ref command, myCriteria);
CreatePagingAndSortingParameters(ref command, sortExpression, startRowIndex, maximumRows);
return GetBOEquatableList<Person>(ref command, propertyNamesToBypassInstantiation);
}
}
您的泛型类不存在。 – SLaks 2010-09-17 20:42:44
这里的一些代码没有任何意义。你的第一行你将返回一个:DBBase,但是你的抽象DBBase定义不会使用任何通用参数。我错过了什么吗?另外,我可以建议你去掉一些代码注释以提高可读性。 –
CodingGorilla
2010-09-17 20:44:32
我删除了一堆评论,以便更容易阅读,并修复了我返回DBBase而不仅仅是DBBase的错误。我已经在我的代码中解决了这个问题,但是在写出这个问题的过程中,我已经选择了将它修复的问题。 –
2010-09-18 01:38:28