03 Dec 2013
Months ago, while digging inside the ASP.NET WebAPI I collected a lot of material and I decided to write a small book about the topics. I contacted Syncfusion beacause I really like their series “Succinctly”: small books that go straight to the main topic without much introductions or ceremonies. Few days ago my work have seen the light and you can grab it for free at this address: http://www.syncfusion.com/resources/techportal/ebooks/webapi There are a couple of persons that I need to thanks: Raffaele Rialdi help me a lot in understanting how the model binding works internally (and I can gurantee that is not easy as I expected). Second I have to thanks Ugo Lattanzi that helped me with some oddities of the routing system. Writing a book always was on my todo list and even if it’s not a big book I’m very happy that it is publicly available.
12 Nov 2013
In the last year Functional Programming is the topic that devs are talking about. Many functional languages are becoming popular (Scala, F#, Clojure, Elixir and someone is starting to say that functional programming will be the next big thing. As usual as a curious guy I started to study the fundamentals and theories behind functional programming and I must admit that most of the time we are using the wrong language to resolve our daily problems.
Object Oriented Languages is useful when you have a state to persist (as in a GUI application) but in other cases you don’t need it. Consider a web app. The server side could be just a series of functions that respond to the HTTP commands that arrives from the clients.
Functional programming is about writing programs that are a built as a set of functions that calls each others. Well actually this definition doesn’t made justice, functions must be pure and data must be immutable so consider this is a rough description. :-)
The main points are that you cannot preserve the state and that the functions cannot modify arguments. This means that you can call the function whatever times you want and the function will return always the same result without affecting the state.
This also mean that the execution model is simplyfied and the computer (the compiler/runner) have just to substitute the functions with their result to execute the program. So the implementation of the compilers and interpreters is quite easier.
With object oriented programming the evaluation is much more complicated, since the executor must take care of the state to be able to execute the program. In second instance writing imperatives programs is more difficult since often you must take care of the order of the assignments.
Consider this simple example of a program that calculates the factorial of a given number:
defmodule ImperativeFactorial do def factorial(n) do iter(n, 1, 1) end def iter(n, counter, product) do if counter > n do product else product = counter * product counter = counter + 1 iter(n, counter, product) end end end
This implementation is very imperative and use two variables to store the state (product, counter) between iterations. It works but if we swap the order of the two assignment the result will be wrong:
counter = counter + 1 product = counter * product
Functional programming avoid these flaws because it doesn’t store state. A possible implementation in Elixir is:
defmodule Factorial do def iter(0), do: 1 def iter(n) when n > 0 do n * iter(n-1) end end
Apart the fact that is a lot simpler and short it doesn’t have the pitfall of the previous implementation.
25 Oct 2013
In the last post we saw how the controller issue a command to the pipeline, now we will see how this command in managed.
The CommandExecutor finds the handler for the given commmand. In our case the command was
AddToBasketCommandand the relative handler is
AddToBasketHandler. The code is here:
class AddToBasketHandler def execute(add_to_basket_command) basket = @repository.get_basket(add_to_basket_command.basket_id) basket.add_item (add_to_basket_command.article_id) basket.commit end # more code here... end
The aim of a handler is to find the interested aggregate and send messages to it so that it can execute the needed tasks. In our case, we get from the repository an instance of Basket class using the id attribute of the command and we call
add_itemon that so that our operation will be done. This calls should be made in transaction, but for the sake of simplicity I don’t manage them now. The last call is a message sent to commit method on the aggreate root. This is necessary to confirm the operation, we are saying: ok, we have done with our job. Internally the commit simply apply all the events that were generated in previous methods.
24 Oct 2013
The more I explore Open Source world the more I ask myself why web developers are still using .NET for their web applications.
I know that some of my friends will not agree with what I’m writing, but I invite them to give a try to another platform without pre-conception, just to explore alternatives in web development.
So what’s the problem with C# and .NET?
While I still think that C# is a great language that was well designed and that evolve quite well, I also think that a good language is not enough to build a great development platform. And for web applications there are platforms that are better. Even PHP that could appear like a silly programming language has web frameworks that speed up the development and has tons of packages that are ready to use. We have nuget, yes, but did you compared the number? As today there are about 16k packages on nuget compared to 43k of NPM, 64k of Gems, 17k of Packagist (that start in 2011).
Whatever Microsoft (and it’s community) try they are always in late, they just copy ideas from other platform and they do it well (ASP.NET MVC is a good framework) but they are late. When MS announce a new “marvelous” framework most of my friends says: “Ah…yes, it’s like….”. Just a copy. No innovation.
Another point is the matter of choice, if you need to develop a web application with .NET you don’t have many choice it will be with ASP.NET MVC or NancyFx the new kids on the block. But what if you want to use an add-on on those? Yeoman for example? Or karma? Or Grunt? Most of the time they doesn’t work, or you have to spend hours to configure them to run on windows. What if you want to use ElasticSearch? What if you want to publish your application using Docker? What if you wnat to deploy to heroku? You can’t! Maybe all of these tool will be available in one or two years, but when they will be available for .NET the OSS community will be using something else that would be better that these.
Last point is on the OS. Even if mono is a good project and make you ASP.NET app run on linux few companies use it. In fact, if I want to use linux why should I bother with ASP.NET when I have tens of other framework that I can choose from. I could be wrong on this but, whatever Mono will be, will be always a porting of Microsoft stuff on Linux and it will be never like a native platform (it’s like using Rails on windows….who really use it? Almost nobody because even if it works, it surely creates headache with some gem).
So why should I choose to develop my applications with ASP.NET MVC? No reason at all. Some of you will say “My team has skills only on .NET”, well, invest time in learning a new language, the web development is not on a Windows Box.
21 Oct 2013
My experiments with Ruby and Domain Driven Implementation continues. Two weeks ago I was at the Rupy conf talking about Domain Driven Architectures in Ruby. The code is available on Github and I would like to show you how it works with these small series of post on the topic.
To understand how it works, the best place to start is the test in the file fake_controller.rb.
describe "I'm supposed to be a controller" do include CommandExecutor it "when an action is called I fire a command" do execute_command AddToBasketCommand.new(1,2) end end
This test simulate the code inside a controller. Suppose that you have a page that permits you to add an item to the basket, the page post the information to the controller (baskedId:1 and itemId:2). The controller execute the command to add an item to the basket:
Since we must not use CRUD operation, everytime our app need to do something it issue a command that someone will manage. AddToBasketCommand is just a struct that holds the information for the command, you can see it as the command parameters.
execute_command is defined in the module CommandExecutor that the controller should include to make use of it.
module CommandExecutor def execute_command (command) handler = find_handler(command) handler.execute command end def find_handler(command) class_name = command.class.name.split('::').last.sub(/Command/, '') + 'Handler' klass = Handlers.const_get(class_name) klass.new end end
The CommandExecutor is quite simple. Given a command it finds the handler by convention (xxxCommand is managed by xxHandler). Once it found it, it calls execute on the handler to actually manage the command.
So the controller task stops here, whenever it receive a post/put it needs to create a command and issue the command to the executor. In the next post we will see how the command handler will do the job.