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 »

    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. }

    Pulling data from USASpending.gov

    So i found this great post by doug finke on his blog where he has show implementation for retrieving data from usaspending website using Power Shell and LinqToXml here are my implmentation on C# and F#

    ccode1

     

     

     

     

    here is F# implementation.

     

    fcode1

    Functional Fibonnaci series in C#

    C# code for functional fibonnaci numbers

    Func<int, int> fib = null;
    fib = n => n > 1 ? fib(n – 1) + fib(n – 2) : n;

    Okay so you guys would think why would i declare the delegate in one line and instantiate in the next. The reason is that if you do everything in same line complier wont recognize the recursive call using the delegate. Try this 

    Func<int, int> fib = n => n > 1 ? fib(n – 1) + fib(n – 2) : n;

    Above code wont compile 

    Haskell code for same 
    fib n
    | n == 0 = 0
    | n == 1 = 1
    | n > 1 = fib (n-1) + fib (n-2)

    New Class in .Net 4.1

    Came across a new class in framework .Net 4.1 StringOr<TOther>

    public class StringOr<TOther> {
    public StringOr(string stringValue, TOther otherValue);

    public string StringValue { get; }
    public TOther OtherValue { get; }
    }

    There are lot of situations where you store datetime and other values in string simply because they are not formatted correctly or there are chances that database may or may not return a value which can be converted to exact format.

    StringOr<int> userInput= GetUserInput(“Quantity”);
    string szUserInput=userInput.StringValue;
    int intUserInput=userInput.OtherValue;

    Here is the solution to that problem.

    Posted in .Net, C#. Tags: . Leave a Comment »

    C# functional programming

    Finally i have decided to write on how we can achieve  funcational programming in C#.

    Lets start with defining a function 
    Math: f(x) = x
    C#: Action<int> f = x =>x;
    Math : f(x) =  6x + 2
    C#: Func<int,int> f = x=> 6*x + 2;
    Math: f(x,y) = 2x+ 3y
    C#: Func<int,int,int> f = (x,y) => 2*x + 3*y;
    function which returns a function
    Func<int,Func<int,int>> fg = x => (y => y + x);