Thursday, March 20, 2014

Combining Predicates in c#

Today, I was looking for a way to combine 2 or more predicates, We have found an MSDN post about it HERE. It explains 3 different ways to combine predicates using LINQ expressions. .

But, Can we make it simple and cool?
I guess it's time to use our creativity skills.
Think about it we just need to merge lambda expressions. In order to merge lambda expressions we always use &&/!!, so Can we do the same here?
Here's our solution below:
static void Main(string[] args)
{
    Func<Car, bool> theCarIsRed = c => c.Color == "Red";
    Func<Car, bool> theCarIsCheap = c => c.Price < 10.0;
    Func<Car, bool> theCarIsLuxury = c => c.Model == "Audi";
    Func<Car, bool> theCarIsRedOrCheap =    c =>     theCarIsRed(c)
                                                                             && theCarIsCheap(c)
                                                                             || theCarIsLuxury(c);
    List<Car> cars = new List<Car>();
    cars.Add(new Car { Color = "Red", Price = 7 });
    cars.Add(new Car { Color = "Yellow", Price = 90, Model= "Audi" });
    cars.Add(new Car { Color = "Red", Price = 11, Model = "Toyota" });
    cars.Add(new Car { Color = "Red", Price = 60, Model = "Audi" });
    Console.WriteLine("available options: " + cars.Where(theCarIsRedOrCheap).Count());
    Console.ReadLine();
}
class Car
{
    public string Color { get; set; }
    public long Price { get; set; }
    public string Model { get; set; }
}

Conclusion
Combining predicates means combining lambda expressions.  

Tuesday, March 18, 2014

MVVM and WPF Common Problems for Beginners

    For the past 3 years, I have been using Windows Presentation Foundation(WPF) and Model View ViewModel(MVVM). At the beginning I preferred having everything in code behind because It's simple, you feel more productive and finish the screen in less time. But it doesn't follow the open/close principal. The code becomes very complex when fix defects, add or change user requirements, difficult to test, business logic and controls manipulation are mixed up. Having everything in the code behind is decreasing my productivity in the long term.
   
If you are beginner in WPF and MVVM, don't expect it will be smooth trip. you will struggle at the beginning, you will have a bad moments where you are trying to investigate the binding problems. Be patient and believe that you can make it. This means you have decided to suffer now, and be more productive later.

Here's some MVVM guidelines  

  1. Reduce or eliminate your code-behind(Doesn't mean code behind breaks MVVM )
  2. Bind all of your UI inputs/outputs to your ViewModel
  3. Implement INotifyPropertyChanged on your ViewModel
  4. Put your view behavior into the ViewModel
  5. Do not put any view state into the model
  6. When testing, treat ViewModel as the Real UI
  7. Avoid events.  Use commands instead

Here's a list of some problems and a simple way to get over it

Case 1: Passing information to a user control that has It's own View model.
Solution: Instead of creating a dependency property at the user control code-behind and bound it at the parent window XAML, you can simply bound the DataContext property of the user control to the parent window view model property.

Case 2: Communication between different View models
Solution: Based on the complexity of your view models. If it's compels you can use the messenger/mediator pattern. If you can merge them into a single view model and bound and use it instead, this will save you a very big headache.

Case: Having a reference to the UI control at the view model and set the focus if needed.
Solution:

  1. You can learn about the attached behavior and create your own behavior class in order to set the focus on any control.
  2.  If your case is simple, and you would like to set the focus on a  specific control, pass the control itself as a command parameter to the view mode using. Make sure to use the Dispatcher begin invoke method if you would like to change any control properties at the model view level. this will put your code at the ui thread queue. 

To Be Continue.

Conclusion
Using MVVM and WPF is one of the bnest ways to separate your business logic and user controls manipulation. You have a hard time to get through it at the beginning. Once you are comfortable with it, you will love it and feel like converting all of your old code-behind code to the view model.