我需要帮助使此方法泛型。它重复大约十次,以获得不同web列表控件的列表(用"MyType“代替特定控件中使用的类型)。
private static IList<MyType> GetList(RequestForm form)
{
// get base list
IMyTypeRepository myTypeRepository = new MyTypeRepository(new HybridSessionBuilder());
IList<MyType> myTypes = myTypeRepository.GetAll();
// create results list
IList<MyType> result = new List<MyType>();
// iterate for active + used list items
foreach (MyType myType in myTypes)
{
if (myType.Active || form.SolutionType.Contains(myType.Value))
{
result.Add(myType);
}
}
// return sorted results
result.OrderBy(o => o.DisplayOrder);
return result;
}如果这不是足够的信息,请告诉我。我认为这需要更高级的语言特性,而我刚刚熟悉这些特性。也许我应该让它们都使用相同的存储库?
谢谢你的帮助。
编辑:感谢您的帮助。我没有任何同龄人的支持,所以这个版面很棒,我从你们每个人身上学到了一些东西。我希望我能接受所有的答案。
发布于 2009-05-28 13:19:23
你可以先让你的函数更简洁一些,如下所示:
private static IList<MyType> GetList(RequestForm form)
{
// get base list
IMyTypeRepository myTypeRepository =
new MyTypeRepository(new HybridSessionBuilder());
IList<MyType> myTypes = myTypeRepository.GetAll();
return myTypes.Where(x => x.Active || form.SolutionType.Contains(x.Value))
.OrderBy(x => x.DisplayOrder).ToList();
}此时,该函数的大部分内容都与MyType直接相关,因此如何进一步改进它在很大程度上取决于MyType与所涉及的其他类型的关系。例如,下面是一个假设版本,如果您的其他类型遵循(在我看来)一个合理的合同,您可以编写:
private static IList<T> GetList(RequestForm form) where T : OrderedValueContainer
{
// we'll want to somehow genericize the idea of a TypeRepository that can
// produce these types; if that can't be done, we're probably better off
// passing a repository into this function rather than creating it here
var repository = new TypeRepository<T>(new HybridSessionBuilder());
IList<T> myTypes = repository.GetAll();
// the hypothetical OrderedValueContainer class/interface
// contains definitions for Active, Value, and DisplayOrder
return myTypes.Where(x => x.Active || form.SolutionType.Contains(x.Value))
.OrderBy(x => x.DisplayOrder).ToList();
}发布于 2009-05-28 13:14:10
如果所有类型都实现了相同的接口(如果它们不实现,则创建它们,并确保将此方法中所需的所有属性添加到接口中),那么您可以这样做:
private static IList<T> GetList(RequestForm form)
where T: IMyInterface
{
// get base list
IMyTypeRepository myTypeRepository = new MyTypeRepository(new HybridSessionBuilder());
IList<T> myTypes = myTypeRepository.GetAll();
// create results list
IList<T> result = new List<T>();
// iterate for active + used list items
foreach (T myType in myTypes)
{
if (myType.Active || form.SolutionType.Contains(myType.Value))
{
result.Add(myType);
}
}
// return sorted results
return result.OrderBy(o => o.DisplayOrder).ToList();
}我所做的另一个更改是最后一行,其中orderby位于单独的一行上,并且从未真正捕获有序列表。
EDIT:为了解决存储库问题,您可以拥有一个分类的存储库工厂,它根据类型T:
public static IMyTypeRepository GetRepository(Type t)
{
if(t == typeof(Type1))
{
return Type1Repository();
}
if(t == typeof(Type2))
{
return Type2Repository();
}
.......
}当然,假设您的所有存储库都实现了IMyRepository接口。
发布于 2009-05-28 13:42:09
首先,您的所有类型都必须实现一个定义Active、Value等属性的通用interface。
此外,据我所知,所有存储库都必须有一个独立于MyType的存储库接口,以便您可以使用如下所示的泛型方法。应该在IRepository中定义GetAll()方法。
public interface IRepository<T> where T : IMyType
{
IList<T> GetAll();
}
public class RepositoryFactory
{
public static IRepository<T> createRepository<T>(ISessionBuilder sb) where T : IMyType
{
// create repository
}
}
public interface IMyType
{
bool Active { get; }
string Value { get; }
}
private static IList<T> GetList(RequestForm form) where T : IMyType
{
// get base list
IRepository<T> repository = RepositoryFactory.createRepository<T>(new HybridSessionBuilder());
IList<T> myTypes = repository.GetAll();
// create results list
IList<T> result = new List<T>();
// iterate for active + used list items
foreach (T myType in myTypes)
{
if (myType.Active || form.SolutionType.Contains(myType.Value))
{
result.Add(myType);
}
}
// return sorted results
return result.OrderBy(o => o.DisplayOrder).ToList();
}https://stackoverflow.com/questions/920742
复制相似问题