13 Jul 2013, 17:14

Elixir + MongoDB + Dynamo -> because we are all web junkies

Now that you know how to have an elixir software accessing mongodb, what is the next logical step? to build a blog engine, obviously

So lets get started, shall we?

23 Jun 2013, 17:15

So, what is elixir?

Elixir logo

A bit of background first: In my masters I had a module called Programming Paradigms and Languages. What this module is about is the different paradigms, and one language for each of them.

The chosen languages were: Object Oriented: Ruby Functional: Clojure Logical: Prolog Process Oriented: Erlang Mathematical Programming: Matlab

Out of that what I can say is:

Ruby syntax is awesome. So elegant but there are many performance issues
Clojure is very nice for the academic world, but I cant imagine anyone using it for commercial software building.
Prolog. Ah, prolog... I have had a love/hate relationship with this since my undergrad. Powerful but very specific.
Erlang: blazing fast. handle processes like no one else, but it is probably the worst syntax I've ever seen. Ok, not the worst, but close to that (Yes mind fuck, I didn't forget you)
Matlab... this doesn't even had to be in this list

So, after that my conclusion is: I want a language able to handle many processes like erlang, but with a nice syntax like ruby and maybe add there some cool recursion features from clojure if you can.

That is elixir.

“Elixir is a functional meta-programming aware language built on top of the Erlang VM. It is a dynamic language with flexible syntax with macros support that leverages Erlang’s abilities to build concurrent, distributed, fault-tolerant applications with hot code upgrades."- http://elixir-lang.org/

So, just like that it has become my favorite language. But just like all open source, making it work is always a hassle. But this is for the next post.

Lets have an easy start: how to add Elixir to your path? I am new to the mac world, for those of you that like me are not great at this, let’s go step by step:

check if it is already there: type "echo $PATH" on bash (capital letters)
if elixir is there, you are good to go. if it is not, add it by typing
echo 'PATH=$PATH:/Software/elixir/bin' >> ~/.bash_profile (I put it in the folder Software under root, use your path)
close your bash and open it again. type iex, if you get the interactive shell, go ahead and play with it :)

07 Nov 2012, 17:29

Raspberry pi + ruby + sinatra = #winning

There is something about hardware/low level development that amazes me. I am not really good at it, I don’t actually want to be, my relation with hardware is the same I have with sports: I love to learn new sports, practice it a little bit, but when it starts to get too competitive, ie, the guys are getting serious about it, I loose interest.

That being said, I have to say I’ve played a lot with this things. The first one was the pic. I friend of mine(electric engineer) helped me to get started and to like it. The next step was to upgrade(or is it downgrade?) to arduino, a platform much easier than pic, and that comes ready for development. I got a lot of fun with that, but then when I’ve moved to london, I brought just the essential, so it was left behind.

But when I read about the new raspberry pi, got all excited again! I would be finally able to create high level code and run it in a computer with the size of a credit card. Then you ask: but master, how can I do that???

Simple little grasshopper, you just have to have a pi and a little patience. step1: install ruby; step2: install sinatra; step3: install node.js, and this is the tricky one, I had no idea it was needed… run your code and GG!

Now, making your rasp visible to the outer world… that is for another post. ps: It involves changing your router settings, and that…. that is enough to make me sad…

24 Mar 2012, 17:31

PetaPoco. Said whaaaat?

So, maybe some of you guys checked out my dead database project, the dNet.Db. It is free and open source @ http://dnetdb.codeplex.com/ I started developing that framework because I always thought it is important to automatize the simple and boring part of database. And by this I mean that it load an object values should be as easy as object.load(), or object.LoadList() if you want an array. And it was working fine. But we all know how a project like that requires a lot of maintenance, adding new databases support, fixing bugs and things like that.

A few weeks ago I was talking about this to a friend and he told me about the project massive. Massive is nice, but it doesn’t support everything I needed (mainly because of the stored procedures), so trying to go further into that, I found the PetaPoco(http://www.toptensoftware.com/petapoco/).

This is the kind of framework I consider that fit all my needs:

  • really easy to install
  • easy to fetch objects from the db
  • supports sqlite
  • allows you to use plain old SQL for all the other situations.

So I’ve been using it for a bit now, and it hasn’t let me down. I am not thinking about using my framework anymore. So I decided to write a little tutorial of how it works.

Install: There is a nuget for that. If you don’t know what a nuget is, here is a link, probably I’ll talk about this someday, but this is really cool, check it out.

Model:

1
2
3
4
5
6
[PetaPoco.PrimaryKey("Id")]
public class Project
{
      public int Id { get; set; }
      public string Name { get; set; }
}

Voil’a… Easy as pie… then you could say:-Hum… the loading part might be tricky…. not really… this is the code:

var db = new PetaPoco.Database(“Base”); ViewBag.Projects = db.Fetch<Models.Project>("");

and that’s it. Ok, that’s not it. Well it is if you have a really simple object, but you don’t. So… now comes the tricky part? Yes. Compared to the other part it is. but not so much

The loading bit is exactly the same, but now I’ll add some more code to the model:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
   [PetaPoco.PrimaryKey("Id")]
    public class Feature
    {
        public int Id { get; set; }
        public int ParentId { get; set; }
        public int ProjectId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        [PetaPoco.ResultColumn]
        public Project project { get; set; }
    }
    [PetaPoco.PrimaryKey("Id")]
    public class Project
    {
        public int Id { get; set; }
        public string Name { get; set; }
        [PetaPoco.ResultColumn]
        public Dictionary<long, Feature> Features { get; set; }
    }
    class ProjectFeatureRelator
    {
        Dictionary<long, Feature> features = new Dictionary<long, Feature>();

        public Project MapIt(Project p, Feature f)
        {
            Feature fExisting;
            if (features.TryGetValue(f.Id, out fExisting))
                f = fExisting;
            else
                features.Add(f.Id, f);

            f.project = p;
            p.Features = features;
            return p;
        }
    }

Now, this looks a lot, but let’s understand what happened there: 1 - I’ve added a new class (feature). This class have a primary key Id, and I want it to load the project associated with it. 2 - I’ve added a new property to the project class:Features. this is because I want the list of features to be loaded as well. Note that I’ve annotated the [PetaPoco.ResultColumn] to make the connection. 3 - So far so good, but now comes the ugly part: the join. I admit it is not an elegant code as the rest, but it is not so bad, and it is very easy, you just have to copy it, and use it in your code.

I don’t know if it is a good idea to use this in a big project, but for the small ones, I will definitely use it.

23 Mar 2012, 17:37

Aspnet_mvc

So seems like asp.net mvc is finally popular? That’s perfect, I mean, this is how it should have been since the beginning.

Let’s start with what is mvc: Mvc is to build the application as model, view and controller. What does that means? It means that the view should be only responsible for showing the interface, the data access should be in the model layer, and everything else should be done by the controller.

Sounds nice, right? Old but gold… But the thing is… and how about the web? Usually we had a asp / aspx / php / jsp / etc file. This file is requested, and then, from this point on you can call your controller logic. But this isn’t the proper way to do it. This way your view is mixed with the controller.

And then, there was the light! true MVC to the web applications. Is this hard to implement? off course not, my little grasshopper…. You just have to think your application in a different way. and what is that way? Routes!

And your routes file should look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

    }

And how do you use it? Easy: you set it as your controller name, and action, so every time the user types that address the method defined as action is called. so when the user types website/home/index, the method index inside the controller home is called.

Ok, we’ve got the code, but how to display it to the user? In the controller method, there should be a return View(). You can return View(“name-of-the-view”)

Now you have the request processed in the right place, and you have your view. Sweet! :D Buuuuuuuuut, something is still missing… and that’s because you have to connect those 2. And this is called the mighty ViewBag!

View bag - speaking like mr jobs would say - is a magical, fantastic… the better ever created object to carry values between controller and view. So how do you use this thing? Easy:

1
ViewBag.YourVariable = "The title";

and in the view you can use it like this:

By the way, this brings up another topic:RAZOR

Once again: this is magical, fantastic, amazingly simple. you can put C# code in the view just by adding a @ in front of it. And, if you want to put a chunk of code, just use

1
2
3
@{
  //several lines of code
}

and how about the model? Well… the model is the same as before, I won’t talk about it here. But that’s it for now. I’ll write more about razor.

23 Mar 2012, 17:30

Hello world!

To start with, I’ll explain why I am creating this blog. Maybe you know my old blog. It is dead now. It was in my old server and I decided not to keep it, but what makes me mad is the domain. it expired and now I don’t even have email. Anyway, new blog, new posts, new life. I don’t hate java anymore, yay! Just kidding… I still do. But not as much. You see, now the servlets seems to work. The IDEs are decent. And java is so far behind .Net, they don’t even try to compete anymore… Maybe that’s the best part. So this is my first…ish post… at least the first in this blog.