CSLA.Net framework for developing business objects.

I have been fan of CSLA.Net(component-based, scalable, logical architecture) framework since early days of my career had ported it from VB.Net to C# then. This framework comes handy for development of N-Tier architecture. Following are the feature highlights.
  1. Tracking Broken Business Rules
  2. Tracking Whether the Object Has Changed
  3. Strongly Typed Collections of Child Objects
  4. Simple and Abstract Model for the UI Developer
  5. Supporting Data Binding
  6. Object Persistence and Object-Relational Mapping .
  7. Business Object Creation
  8. N-Level Undo Functionality
  9. Data Binding Support
  10. Validation Rules
  11. Data Portal
  12. Custom Authentication
  13. Integrated Authorization

http://www.lhotka.net/

    Posted in .Net, C#. Tags: . 1 Comment »

    Finally WPF/Sliverlight style generator.

    I started this project last week and so far i was able to get Monochromatic, complimentary, Triad , Tetrads and analogous colors. The final goal of this project are as follows.

    1. User should be able to view dark and light style with chosen color on predefined template.
    2. User should be able to generate ResourceDictionary for Silverlight/WPF out of the chosen color. (This would relieve every developer from pain of styling the application :) trust me i know it)

    image

    Coming soon on codeplex…….  Download code.

    Note: Please contribute to the project for the wellbeing of all the developers :)

    Microsoft Security Essentials

    Microsoft Security Essentials provides real-time protection for your home PC that guards against viruses, spyware, and other malicious software. It is available for free, for Windows XP 32-bit, Windows Vista/7 32-bit, and Windows Vista/7 64-bit.

    http://www.microsoft.com/security_essentials/

    Project Tuva from microsoft research.

    http://research.microsoft.com/apps/tools/tuva/index.html

    Microsoft research project tuva home page explains it like this:

    “Project Tuva explores core scientific concepts and theories through presenting timeless videos with its new enchanced Video Player featuring searchable video, linked transcripts, notes, and interactive extras”

    It was amazing to see and hear Dr. Richard Feynman’s lecture on “Law of Gravitation” at Cornell University in 1964.

    Why functional programming?

    Functional programming is not a silver bullet, but learning it will indeed add to the knowledge of every software engineer about solving problem in different a way. One can always implement functional style of programming in any imperative language they use.

    Advantages:

    1. For analysis of mathematical problem that requires human analysts, it is possible to represent the problem in a concise mathematical form that analysts can understand.
    2. A declarative language simplifies the creation and multiple interpretations and static analysis.
    3. Compositional construction allows natural mapping of the problem domain and encourages code reuse.
    4. Functional programming language like haskell makes maintenance easy due to its high level of abstraction which also makes addition of new features a lot simpler then it would be in any imperative language.
    5. As most of the data types in functional languages are immutable concurrency and parallelism is integral art of the language. Most modern functional languages like Haskell and Erlang support multi-core without any extra libraries.
    6. Its easy to implement domain-specific language.

    Disadvantages:

    1. Its difficult to get developers on functional languages and thus it adds to project risk.
    2. UI development is not a proven ground in functional world.
    3. Performance is real unknown for most functional languages as its not widely accepted in developing of large real time systems. Haskell performance. (i personally believe that with multi-core systems any problem needs to be parallelized and immutability is the only answer to that type problems. So even imperative languages have to make data structures immutable to solve the problem on multi-core.)

    Here is the implementation of Black Scholes model for European options in Haskell and C#. I wrote this example to show the difference between functional and imperative language, see it your self.

    1. type Put = String
    2. type Call = String
    3. data Option = Call | Put
    4. deriving (Eq, Show)
    5. blackscholes :: Option -> Double -> Double -> Double -> Double -> Double -> Double
    6. blackscholes  option s x t r v
    7. | option == Call = s * normcdf d1 – x*exp (-r*t) * normcdf d2
    8. | option == Put = x * exp (-r*t) * normcdf (-d2) – s * normcdf (-d1)
    9. where
    10. d1 = ( log(s/x) + (r+v*v/2)*t )/(v*sqrt t)
    11. d2 = d1 – v*sqrt t
    12. normcdf x
    13. | x < 0 = 1 – w
    14. | otherwise = w
    15. where
    16. w = 1.0 – 1.0 / sqrt (2.0 * pi) * exp(-l*l / 2.0) * poly k
    17. k = 1.0 / (1.0 + 0.2316419 * l)
    18. l = abs x
    19. poly = horner coeff
    20. coeff = [0.0,0.31938153,-0.356563782,1.781477937,-1.821255978,1.330274429]
    21. horner coeff base = foldr1 multAdd  coeff
    22. where
    23. multAdd x y = y*base + x
    1. namespace BackScholes
    2. {
    3. class Program
    4. {
    5. static void Main(string[] args)
    6. {
    7. }
    8. }
    9. enum Option
    10. {
    11. Call,
    12. Put
    13. }
    14. /// <summary>
    15. /// Summary description for BlackSholes.
    16. /// </summary>
    17. public class BlackSholes
    18. {
    19. public double BlackScholes(Option option, double S, double X,
    20. double T, double r, double v)
    21. {
    22. double d1 = 0.0;
    23. double d2 = 0.0;
    24. double dBlackScholes = 0.0;
    25. d1 = (Math.Log(S / X) + (r + v * v / 2.0) * T) / (v * Math.Sqrt(T));
    26. d2 = d1 – v * Math.Sqrt(T);
    27. if (option == Option.Call)
    28. {
    29. dBlackScholes = S * CND(d1) – X * Math.Exp(-r * T) * CND(d2);
    30. }
    31. else if (option == Option.Put)
    32. {
    33. dBlackScholes = X * Math.Exp(-r * T) * CND(-d2) – S * CND(-d1);
    34. }
    35. return dBlackScholes;
    36. }
    37. public double CND(double X)
    38. {
    39. double L = 0.0;
    40. double K = 0.0;
    41. double dCND = 0.0;
    42. const double a1 = 0.31938153;
    43. const double a2 = -0.356563782;
    44. const double a3 = 1.781477937;
    45. const double a4 = -1.821255978;
    46. const double a5 = 1.330274429;
    47. L = Math.Abs(X);
    48. K = 1.0 / (1.0 + 0.2316419 * L);
    49. dCND = 1.0 – 1.0 / Math.Sqrt(2 * Convert.ToDouble(Math.PI.ToString())) *
    50. Math.Exp(-L * L / 2.0) * (a1 * K + a2 * K * K + a3 * Math.Pow(K, 3.0) +
    51. a4 * Math.Pow(K, 4.0) + a5 * Math.Pow(K, 5.0));
    52. if (X < 0)
    53. {
    54. return 1.0 – dCND;
    55. }
    56. else
    57. {
    58. return dCND;
    59. }
    60. }
    61. }
    62. }

    Dryad and DryadLinq release for academic community.

    Microsoft’s answer to google map reduce and hadoop for distributed computing is Dryad.

    http://research.microsoft.com/en-us/collaboration/tools/dryad.aspx

    RoutedEvent to Command action behavior (Blend 3).

    Blend 3 has added two new assemblies which consists of new functionalities including advance behaviors

    1. Microsoft.Expression.Interactions.dll
    2. System.Windows.Interactivity.dll

    These advance behavior classes lets you create attached behaviors with simplicity. Code snippet below shows how to create a behavior which can be applied on any UIElement’s Routed event and when that event is triggered the given command is invoked with its command parameters. These behaviors help us write clean code and we dont have to deal with click events when using M-V-vM pattern. We can wite our command and bind that command to any routed event on any user interface element and implement the command on view model.

    Code Snippet
    1. using System;
    2. using System.Windows;
    3. using System.Windows.Input;
    4. using System.Windows.Interactivity;
    5. using EventTrigger = System.Windows.Interactivity.EventTrigger;
    6. namespace Behaviors
    7. {
    8. /// <summary>
    9. /// Behaviour helps to bind any RoutedEvent of UIElement to Command.
    10. /// </summary>
    11. [DefaultTrigger(typeof(UIElement), typeof(EventTrigger), "MouseLeftButtonDown")]
    12. public class ExecuteCommandAction : TargetedTriggerAction<UIElement>
    13. {
    14. /// <summary>
    15. /// Dependency property represents the Command of the behaviour.
    16. /// </summary>
    17. public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(
    18. “CommandParameter”,
    19. typeof(object),
    20. typeof(ExecuteCommandAction),
    21. new FrameworkPropertyMetadata(null));
    22. /// <summary>
    23. /// Dependency property represents the Command parameter of the behaviour.
    24. /// </summary>
    25. public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
    26. “Command”,
    27. typeof(ICommand),
    28. typeof(ExecuteCommandAction),
    29. new FrameworkPropertyMetadata(null));
    30. /// <summary>
    31. /// Gets or sets the Commmand.
    32. /// </summary>
    33. public ICommand Command
    34. {
    35. get
    36. {
    37. return (ICommand)this.GetValue(CommandProperty);
    38. }
    39. set
    40. {
    41. this.SetValue(CommandProperty, value);
    42. }
    43. }
    44. /// <summary>
    45. /// Gets or sets the CommandParameter.
    46. /// </summary>
    47. public object CommandParameter
    48. {
    49. get
    50. {
    51. return this.GetValue(CommandParameterProperty);
    52. }
    53. set
    54. {
    55. this.SetValue(CommandParameterProperty, value);
    56. }
    57. }
    58. /// <summary>
    59. /// Invoke method is called when the given routed event is fired.
    60. /// </summary>
    61. /// <param name=”parameter”>
    62. /// Parameter is the sender of the event.
    63. /// </param>
    64. protected override void Invoke(object parameter)
    65. {
    66. if (this.Command != null)
    67. {
    68. if (this.Command.CanExecute(this.CommandParameter))
    69. {
    70. this.Command.Execute(this.CommandParameter);
    71. }
    72. }
    73. }
    74. }
    75. \

    Once you create a class with the above code and open the project in blend and open any UI, you can see the following view in the asset tab. Marked in red is our behavior.

    image

    Drop the behavior onto any UIElement

    image

    Finally you can see how we can select the Event and bind the command and command parameters in the properties tab.

    image

    XAML for above blend designer setting.

    Code Snippet
    1. <Grid>
    2. <i:Interaction.Triggers>
    3. <i:EventTrigger EventName=”MouseLeftButtonDown”>
    4. <Behaviours:ExecuteCommandAction Command=”{Binding MyCommand}” CommandParameter=”{Binding MyCommandParameter}”/>
    5. </i:EventTrigger>
    6. </i:Interaction.Triggers>
    Posted in Blend 3, Mvvm, WPF. Tags: , , . 2 Comments »

    Design time support for Prism.

    As we all know that Designer-Developer workflow is not feasible when the application is developed in Prism (Composite Application Guidance). Prism requires each use  case to have it’s functionality in different modules and load these modules at run-time.

    Due to runtime loading of the user control neither Visual Studio or Blend can be used for implementing designer-developer workflow as the user interface cannot be seen in its entirety.

    Here is what i implemented with the help of Marc Jacobs . We create an attached behavior that can be set on any ContentControl or ItemsControl. This attached behavior takes the user control which we need to display while design-time.

    Code Snippet
    1. public static void OnContentChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    2. {
    3. if (DesignerProperties.GetIsInDesignMode(depObj))
    4. {
    5. var contentControl = depObj as ContentControl;
    6. if (contentControl != null)
    7. {
    8. contentControl.Content = e.NewValue;
    9. }
    10. else
    11. {
    12. var itemsControl = depObj as ItemsControl;
    13. if (itemsControl != null)
    14. {
    15. itemsControl.Items.Add(e.NewValue);
    16. }
    17. }
    18. }
    19. \

    The magic happens in the OnContentChanged method, when we set the attached behavior content this method is invoked. The code inside the method only inserts the user control in the design mode.

    Code Snippet
    1. <Window x:Class=”DesignTimePrism.ShellView”
    2. xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
    3. xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
    4. xmlns:cal=”http://www.codeplex.com/CompositeWPF”
    5. xmlns:local=”clr-namespace:DesignTimePrism”
    6. xmlns:menu=”clr-namespace:DesignTimePrism.Modules.Menu;assembly=DesignTimePrism.Modules.Menu”
    7. xmlns:main=”clr-namespace:DesignTimePrism.Modules.Main;assembly=DesignTimePrism.Modules.Main”
    8. Title=”Window1″ Height=”600″ Width=”600″>
    9. <Window.Resources>
    10. <main:MainView x:Key=”mainView” />
    11. <menu:MenuView x:Key=”menuView” />
    12. </Window.Resources>
    13. <Grid>
    14. <Grid.ColumnDefinitions>
    15. <ColumnDefinition Width=”0.3*” />
    16. <ColumnDefinition Width=”0.7*” />
    17. </Grid.ColumnDefinitions>
    18. <ContentControl Grid.Column=”0″
    19. local:DesignTime.Content=”{StaticResource mainView}”
    20. cal:RegionManager.RegionName=”MenuRegion” />
    21. <ContentControl Grid.Column=”1″
    22. local:DesignTime.Content=”{StaticResource menuView}”
    23. cal:RegionManager.RegionName=”MainRegion” />
    24. </Grid>
    25. </Window>

    The only downside of using this technique is having the user controls declared in the resource dictionary. Also below are the snapshots  of the application in design and runtime mode.

    Design time snapshot Runtime snapshot
    DesignTime RunTime

    Code DesignTimePrism.zip

    Posted in WPF. Tags: . 2 Comments »

    Interactive Programming Flapjax

    Flapjax is a new programming language based on interactive programming model. It is build as a framework on javascript and thus works with any web browser.

    Its principal features include:

    • Event-driven, reactive evaluation
    • An event-stream abstraction for communicating with web services
    • Interfaces to external web services

    http://www.flapjax-lang.org/

    Why learning F# difficult.

    i found this really gr8 blog post and want to share.

    http://www.gotnet.biz/Blog/post/Why-Learning-FSharp-Is-So-Difficult.aspx thanks to Kevin Hazzard for wonderful post.