我有以下类和一个工厂(省略了不必要的代码)。我有 3 个单独的 IManageableEntryDao 实现,以及一个在 createDao 方法中访问的字符串/类型映射。
我收到以下编译错误:"ManageableEntry.IManageableEntryDao"需要'1'类型参数"。 解决此问题的最佳实践是什么?我想以某种方式确定是什么吗?还是有替代解决方案?
public interface IManageableEntryDao<T> where T : IManageableEntry {
T findById(long id);
T findByName(string name);
int findUnapprovedCount();
List<T> findUnapproved(ManageableEntryCriteria criteria);
long insert(T manageableEntry);
bool update(T manageableEntry);
bool delete(T manageableEntry);
}
public class ManageableEntryDaoFactory {
public IManageableEntryDao createDao(string manageableEntryType) {
manageableEntryType = manageableEntryType.ToLower();
Type type = daoTypes[manageableEntryType];
if (type != null) {
object dao = Activator.CreateInstance(type);
return dao as IManageableEntryDao;
}
throw new NotImplementedException("Failed to find DAO for type: " + manageableEntryType);
}
}
您需要在方法调用中指定类型。 这确实意味着您可能可以避免对字符串的需求:
public IManageableEntryDao<T> CreateDao<T>() where T : IManageableEntry
{
Type manageableEntryType = typeof(T);
// You'll need to modify daoTypes to be a HashSet<Type> (or List<Type>) of allowable types, or something similar, instead of using a dictionary lookup
if (daoTypes.Contains(manageableEntryType) {
object dao = Activator.CreateInstance(type);
return dao as IManageableEntryDao<T>;
}
throw new NotImplementedException("Failed to find DAO for type: " + manageableEntryType);
}
你可以:
- 通过使
CreateDao
方法(或ManagableEntryDaoFactory
本身)泛型,向IManageableEntryDao<T>
提供类型参数。或 - 从
CreateDao
方法返回IManagableEntry
接口,而不是返回泛型IManagableEntryDao<T>
。
编辑:基于评论
遗憾的是,不能基于输入字符串从 CreateDao
方法返回特定类型。您能做的最好的事情是返回 daoTypes
列表中所有类型通用的基类型或接口。
另一个想法是返回一个非泛型接口,并在实现的方法中将接口强制转换为特定类型。 这里有一个小程序来说明这一点:
class Program
{
static void Main(string[] args)
{
var customerEntry = ManageableEntryDaoFactory.CreateDao("customer");
var orderEntry = ManageableEntryDaoFactory.CreateDao("order");
customerEntry.Update(new Customer() { Name = "John Doe" });
orderEntry.Update(new Order() { OrderId = 123 });
Console.ReadKey();
}
}
public class Customer
{
public string Name { get; set; }
}
public class Order
{
public int OrderId { get; set; }
}
public class CustomerEntry : IManageableEntryDao
{
public void Update(object objCustomer)
{
var customer = objCustomer as Customer; // now you have a Customer type...
Console.WriteLine("Updated customer: " + customer.Name);
}
}
public class OrderEntry : IManageableEntryDao
{
public void Update(object objOrder)
{
var order = objOrder as Order; // now you have an Order type...
Console.WriteLine("Updated order: " + order.OrderId);
}
}
public interface IManageableEntryDao
{
void Update(object entry);
// ...other methods, properties, events...
}
public static class ManageableEntryDaoFactory
{
private static readonly Dictionary<string, Type> daoTypes = new Dictionary<string, Type>()
{
{"customer", typeof(CustomerEntry) },
{"order", typeof(OrderEntry) }
};
public static IManageableEntryDao CreateDao(string manageableEntryType)
{
manageableEntryType = manageableEntryType.ToLower();
Type type = daoTypes[manageableEntryType];
if (type == null)
throw new NotImplementedException("Failed to find DAO for type: " + manageableEntryType);
return Activator.CreateInstance(type) as IManageableEntryDao;
}
}