我目前正在擺弄通用存儲庫。我的測試應用程序使用C#。我做了一個函數,打印出特定存儲庫中的所有項目。存儲庫是輸入參數。整個塊是在這個崗位的底部,目前本身的功能如下:Ordeby(),Where()等用於具有泛型類型的函數
static void PrintCollection<T, TKey>(IRepository<T, TKey> repo)
{
// Error since compiler does not recognizes p.LastName
//foreach (Person p in repo.GetItems().OrderBy(p => p.LastName)) Console.WriteLine(Convert.ToString(p));
foreach (T p in repo.GetItems())
Console.WriteLine(Convert.ToString(p));
}
它工作正常,但它缺少的東西像一類的特定屬性排序,如LastName
在Person
或ProductName
在Product
。我想知道是否可以發送一個額外的參數到PrintCollection<T, TKey>(...)
並測試特定字段是否存在,然後按順序排列。例如:
static void PrintCollection<T, TKey, TOrder>(IRepository<T, TKey> repo, TOrder o = default(TOrder))
{
if (o == default(TOrder))
foreach (T p in repo.GetItems()) Console.WriteLine(Convert.ToString(p));
else
// Order by o
}
目前,我不知道我應該用什麼來測試字段和屬性與他們在課堂上存在的差異。然後,如何在LINQ和Lambdsa Expression中將它們用於例如排序,過濾集合。
感謝
下面是我的代碼片段。我剪掉了不重要的部分。
主程序
class Program
{
static void Main(string[] args)
{
PeopleRepository repPeople = new PeopleRepository();
ProductsRepository repProduct = new ProductsRepository();
PrintCollection(repPeople);
PrintCollection(repProduct);
}
static void PrintCollection<T, TKey>(IRepository<T, TKey> repo)
{
// Error since compiler does not recognizes p.LastName
//foreach (Person p in repo.GetItems().OrderBy(p => p.LastName)) Console.WriteLine(Convert.ToString(p));
foreach (T p in repo.GetItems()) Console.WriteLine(Convert.ToString(p));
}
}
接口IRepository
interface IRepository<T, TKey>
{
IEnumerable<T> GetItems();
T GetItem(TKey key);
// ...
}
庫爲人民
class PeopleRepository : IRepository<Person, string>
{
private static List<Person> _proxy;
public PeopleRepository(List<Person> people = null)
{
if(people == null) _proxy = Person.GetAllPeople();
else _proxy = people;
}
public IEnumerable<Person> GetItems()
{
return _proxy;
}
public Person GetItem(string key)
{
return _proxy.SingleOrDefault(p => p.ID == key);
}
}
庫產品
class ProductsRepository : IRepository<Product, int>
{
private static List<Product> _proxy;
public ProductsRepository(List<Product> products = null)
{
if (products == null) _proxy = Product.GetAllProducts();
else _proxy = products;
}
public IEnumerable<Product> GetItems()
{
return _proxy;
}
public Product GetItem(int key)
{
return _proxy.SingleOrDefault(p => p.ID == key);
}
}
我的最終目標是與EF和ORM合作。感謝編輯。至於'Func <>'我完全忘記了我可以使用這個委託。恥辱:p – Celdor
難以按照我選擇的方式更改此功能,以便進行排序,篩選或排序?這可能是另一個問題的話題。 – Celdor
沒有那麼難,你可以添加任何你想要的參數,然後根據它們來決定,但是我認爲我添加到我的答案中的建議是一個更好的選擇。 –