Pages

Tuesday, February 19, 2013

Routed Events in WPF / Silverlight

Routed events are events which navigate up or down the visual tree acording to their RoutingStrategy. The routing strategy can be bubble, tunnel or direct. You can hook up event handlers on the element that raises the event or also on other elements above or below it by using the attached event syntax: Button.Click="Button_Click".
Routed events normally appear as pair. The first is a tunneling event called PreviewMouseDown and the second is the bubbling called MouseDown. They don't stop routing if the reach an event handler. To stop routing then you have to set e.Handled = true;
  • Tunneling The event is raised on the root element and navigates down to the visual tree until it reaches the source element or until the tunneling is stopped by marking the event as handeld. By naming convention it is called Preview... and appears before corresponding bubbling event.
  • Bubbling The event is raised on the source element and navigates up to the visual tree until it reaches the root element or until the bubbling is stopped by marking the event as handled. The bubbling event is raised after the tunneling event.
  • Direct The event is raised on the source element and must be handled on the source element itself. This behavior is the same as normal .NET events.
Create a Custom Routed Event
// Register routed event
public static readonly RoutedEvent SelectedEvent = 
    EventManager.RegisterRoutedEvent( "Selected", RoutingStrategy.Bubble, 
    typeof(RoutedEventHandler), typeof(MyCustomControl));
 
// .NET wrapper
public event RoutedEventHandler Selected
{
    add { AddHandler(SelectedEvent, value); } 
    remove { RemoveHandler(SelectedEvent, value); }
}
 
// Raise the routed event "selected"
RaiseEvent(new RoutedEventArgs(MyCustomControl.SelectedEvent));

 


Thursday, February 14, 2013

AntiPatterns

What Is an AntiPattern?
AntiPatterns, like their design pattern counterparts, define an industry vocabulary for the common defective processes and implementations within organizations. A higher-level vocabulary simplifies communication between software practitioners and enables concise description of higher-level concepts.
An AntiPattern is a literary form that describes a commonly occurring solution to a problem that generates decidedly negative consequences. The AntiPattern may be the result of a manager or developer not knowing any better, not having sufficient knowledge or experience in solving a particular type of problem, or having applied a perfectly good pattern in the wrong context.
AntiPatterns provide real-world experience in recognizing recurring problems in the software industry and provide a detailed remedy for the most common predicaments. AntiPatterns highlight the most common problems that face the software industry and provide the tools to enable you to recognize these problems and to determine their underlying causes.
Furthermore, AntiPatterns present a detailed plan for reversing these underlying causes and implementing productive solutions. AntiPatterns effectively describe the measures that can be taken at several levels to improve the developing of applications, the designing of software systems, and the effective management of software projects.

Software Development AntiPatterns
A key goal of development AntiPatterns is to describe useful forms of software refactoring. Software refactoring is a form of code modification, used to improve the software structure in support of subsequent extension and long-term maintenance. In most cases, the goal is to transform code without impacting correctness.

Software Architecture AntiPatterns
Architecture AntiPatterns focus on the system-level and enterprise-level structure of applications and components. Although the engineering discipline of software architecture is relatively immature, what has been determined repeatedly by software research and experience is the overarching importance of architecture in software development.

Software Project Management AntiPatterns
In the modern engineering profession, more than half of the job involves human communication and resolving people issues. The management AntiPatterns identify some of the key scenarios in which these issues are destructive to software processes.


Tuesday, February 12, 2013

LINQ GroupBy Count

class Program

    {

        static void Main(string[] args)

        {

            var employeeProducts = new List<EmployeeProduct>();

            employeeProducts.Add(new EmployeeProduct(1, 2, "XYZ"));

            employeeProducts.Add(new EmployeeProduct(1, 5, "ZXY"));

            employeeProducts.Add(new EmployeeProduct(2, 2, "XYZ"));

 

            var way1 = employeeProducts.Select(

                ep => new ProductCount

                {

                    ProductNumber = ep.ProductID,

                    EmployeeNumber = ep.EmployeeID,

                    CountProducts = employeeProducts.Count(epc => epc.ProductID == ep.ProductID)

                });

 

            var way2 = employeeProducts

                .GroupBy(ep => ep.ProductID)

                .SelectMany(epg => epg.Select(

                    ep => new ProductCount

                    {

                        ProductNumber = ep.ProductID,

                        EmployeeNumber = ep.EmployeeID,

                        CountProducts = epg.Count()

                    }));

        }

 

        public class EmployeeProduct

        {

            public EmployeeProduct(int employeeID, int productID, string name)

            {

                EmployeeID = employeeID;

                ProductID = productID;

                Name = name;

 

            }

            public int EmployeeID { get; set; }

            public int ProductID { get; set; }

            public string Name { get; set; }

        }

 

        public class ProductCount

        {

            public int ProductNumber { get; set; }

            public int EmployeeNumber { get; set; }

            public int CountProducts { get; set; }

        }

    }