Anytime I design a businesslogic, I think about the same things.
How can you build a powerful businesslogic class without writing tons of code?
Okay, in this post I'll talk a little bit about the businesslogic's getter methods. I decided to create two getter methods.
First getter returns all Items from a suggested type.
1: internal List<T> GetItems()
2: {
3: // do some getter action here
4: }
The second one will have only one parameter, a predicate which does the filter action.
Let me explain this snippet shortly. First we have to check if the given predicate is set or not. Isn't the predicate set, we return all Items from the type T. But when the developer passes a predicate we invoke the predicate for each item of type T. If the invoke returns true, the current item matches our conditions and will be added to the return value which is a list of items from type T. If the item doesn't match the predicate we can ignore it.
1: internal List<T> GetItems(Predicate<T> filter)
2: {
3: if(filter!=null)
4: {
5: List<T> filteredValues = new List<T>();
6: businessCache.Foreach(delegate(T any)
7: {
8: if(filter.Invoke(any))
9: filteredValues.Add(any);
10: });
11: return filteredValues;
12: }
13: else
14: {
15: return businessCache;
16: }
17:
18: }
With this implementation the businesslogic is very flexible and every developer can create his own predicates to get only the items which will match the predicate.
Here is a short example how this method will be used inside the PresentationLayer
1: this.dataGridViewSample.AutoGenerateColumns = true;
2: Predicate<Customer> pr = new Predicate<Customer(delegate(Customer anyCustomer){
3: return anyCustomer.IsActive;
4: });
5: this.dataGridViewSample.DataSource = BusinessLogic.DataManager.Instance.GetItems(pr);