Sunday, January 13, 2013

Query Command Implementation

I’m building a website which has two basic workflows:
- Getting and displaying data from the database.
- Inserting and updating some data.
In other words I have Queries and Commands. I’m not going to talk what is the “query command pattern” - that you can read in Ayende’s “Limit your abstractions” posts. I am going to talk about a nice way to implement that pattern in your MVC application.
Let’s go
The implementation is pretty easy: if you need to query something you create a query class that does it. If you have to insert or update some data then you create a command class. The controller will execute commands and queries directly from it’s actions, no need no create a BL layer, DAL or repository of some kind.
Command
First we need an abstract command class
   1: public abstract class Command

   2: {

   3:     public abstract void Execute(IDocumentSession session);

   4: }

The Command class a single method “Execute” that receives the session as a parameter. The Command class supposed to handle a business logic that creates, updates or deletes something in the application that’s why it has a void as a return type.
Command example


   1: public class RegisterUserCommand :Command

   2: {

   3:     public User User { get; set; }

   4:  

   5:     public RegisterUserCommand(User user)

   6:     {

   7:         User = user;

   8:     }

   9:     

  10:     public override void Execute(IDocumentSession session)

  11:     {

  12:         if (session == null)

  13:             throw new ArgumentNullException("session");

  14:  

  15:         if (User == null)

  16:             throw new InvalidOperationException("Can't register null user");

  17:  

  18:         session.Store(User);

  19:     }

  20: }


When I create the command I pass it my model, throw in some validations and finally use the received session to save the entity. Note that the command should not return a result. It should succeed or fail.
Query
Same as the command first I’ll create an abstract query class a single method:


   1: public abstract class Query<T>

   2: {

   3:     public abstract T Execute(IDocumentSession session);

   4: }


As oppose to the command class, the query has to return a result (T).

Query example


   1: public class CurrentCustomerQuery : Query<Customer>

   2: {

   3:     private string _email;

   4:  

   5:     public CurrentCustomerQuery(string email)

   6:     {

   7:         if (string.IsNullOrWhiteSpace(email))

   8:             throw new ArgumentNullException("email");

   9:  

  10:         _email = email;

  11:     }

  12:  

  13:     public override Customer Execute(IDocumentSession session)

  14:     {

  15:         if (session == null)

  16:             throw new ArgumentNullException("session");

  17:        

  18:         return session.Query<Customer>().SingleOrDefault(x => x.Email == _email);

  19:     }

  20: }

How to use them
So now we know how queries and commands look like, all that’s left is to use them.
The controller is responsible for executing the commands and the queries according to the action scenario.
The easiest way will be creating the command or the query in the controller and simply execute them.


   1: [HttpPost]

   2:  public ActionResult RegisterCustomer(Customer model)

   3:  {

   4:      if (ModelState.IsValid)

   5:      {

   6:          new RegisterCustomerCommand(model).Execute(RavenSession);

   7:          return RedirectToAction("Index", "Home");

   8:      }

   9:  

  10:      return View(model);

  11:  }


However that’s not really testable since we don’t really want to execute our commands or queries. Furthermore it just looks bad.
We need to wrap the commands/queries so that we could mock them easily (and make the code look better).
In order to wrap the queries and the commands we’ll create “CommandExecuter” and “QueryExecuter” with a single method “Execute”.


   1: public class CommandExecuter

   2: {

   3:     public void Execute(Command command, IDocumentSession sesseion)

   4:     {

   5:         command.Execute(sesseion);

   6:     }

   7: }

   8:  

   9: public class QueryExecuter

  10: {

  11:     public T Execute<T>(Query<T> query, IDocumentSession session)

  12:     {

  13:         return query.Execute(session);

  14:     }

  15: }

The base controller will be ctor injected with both of them and will implement two methods “ExecuteCommand” and “ExecuteQuery” which receive a command or a query and call it Execute method.


   1: public class BaseController : Controller

   2:     {

   3:         #region Properties

   4:  

   5:         private QueryExecuter QueryExecuter { get;  set; }

   6:         private CommandExecuter CommandExecuter { get;  set; }

   7:         private IDocumentStore Store { get; set; }

   8:         public IDocumentSession RavenSession { get; protected set; }

   9:  

  10:         #endregion

  11:  

  12:         #region Ctor

  13:  

  14:         public BaseController(IDocumentStore store, CommandExecuter commandExecuter, QueryExecuter queryExecuter)

  15:         {

  16:             Store = store;

  17:             CommandExecuter = commandExecuter;

  18:             QueryExecuter = queryExecuter;

  19:         }

  20:  

  21:         #endregion

  22:  

  23:         #region Executers

  24:  

  25:         protected void ExecuteCommand(Command command)

  26:         {

  27:             CommandExecuter.Execute(command, RavenSession);

  28:         }

  29:  

  30:         protected T ExecuteQuery<T>(Query<T> query)

  31:         {

  32:             return QueryExecuter.Execute(query, RavenSession);

  33:         }

  34:         #endregion

  35:  

  36:         #region Action Execution

  37:  

  38:         protected override void OnActionExecuting(ActionExecutingContext filterContext)

  39:         {

  40:             RavenSession = Store.OpenSession();

  41:         }

  42:  

  43:         protected override void OnActionExecuted(ActionExecutedContext filterContext)

  44:         {

  45:             if (filterContext.IsChildAction)

  46:                 return;

  47:  

  48:             using (RavenSession)

  49:             {

  50:                 if (filterContext.Exception != null)

  51:                     return;

  52:  

  53:                 if (RavenSession != null)

  54:                     RavenSession.SaveChanges();

  55:             }

  56:         }

  57:  

  58:         #endregion

  59:     }



Now when we want to execute a command or a query we’ll call the proper method and pass it the right parameter.


   1: [HttpPost]

   2: public ActionResult RegisterCustomer(Customer model)

   3: {

   4:     if (ModelState.IsValid)

   5:     {

   6:         ExecuteCommand(new RegisterCustomerCommand(model));

   7:         return RedirectToAction("Index", "Home");

   8:     }

   9:     return View(model);

  10: }

Summary
So what we gained?
1. A single command for every action and a single query for every … query.
2. Every command/query has its own logic but they all implement “Execute” method and the base controller just call it from a single place.
3. The deriving controllers are substitutable for their base type and use the BaseController ExecuteCommand or ExecuteQuery.
4. We have two different “Execute” methods, a void one and one which returns a result. Every command/query implements just the one they need and not forced to implement both.
5. Dependency injecting the command and query executers to the controller’s ctor.
Pretty SOLID.























Tuesday, January 8, 2013

Make Transparent Background


Yes it’s rare however one day you will find yourself copying and pasting an image to your website, a window or a document. Ctrl C -> Ctrl V but then you realize that you image hasenoying white background that doesn’t fit to the place you are trying to paste it.
 Yes you can go to Photoshop and use it's features.
Or you can paste your image in office 2010 and higher (in this post I use power point 2013 but it can be done in word too) , click on it and go to the “Format” tab.

Then click on “Color” and “Set Transparent Color”

 Take the brush to the background that you want to make transparent and there you go.



Thursday, December 13, 2012

RavenDb – Step by Step Tutorial


I’m starting a new project.
It’s going to be an ASP.net MVC4 website.
The content is secret;)
 
 
 
 




So today I was looking for the right DB for the site. Oracle and SQL Server were instinct choices. However as many of you I’m also subscribed to Ayende @ Rahien blog. And so as you I receive at least one raven db post a day. After reading a while I realized that RavenDb gives me the same abilities as a standard rational database and more (there too many articles about the efficiency of NoSql so I don’t want to add another one to the list).
That I do want is to guide you step by step in setting the RavenDb server and using the RavenDb Client in your application.

So let’s go! 







RavenDb has a client and a server side. The client enables you to connect to the RavenDb server. The server can run in several ways:
Win Service
IIS Service
Embeded in the application.

The embedded option is for really small projects or for demos as is in real application you would want to run the RavenDb on a different server and configure it to suite your needs.

I want to make this tutorial for real application’s scenario so we’d choose the Win Service option.
Download the last stable build of RavenDb. In the zip file you’ll find some samples and a server folder, copy it to a suitable location for a database (c:/temp).

In the server folder there’re two files that you are interested in: Raven.Server.exe and Raven.Server.exe.config. In the config file you can specify a custom port for the server to run and the location of the Data Directory where Raven writes the data. Yes, RavenDb is an in-memory database however it writes the data to a file for backup and restore purposes.
 <add key="Raven/Port" value="*"/>
 <add key="Raven/DataDir" value="~\Data"/>
By default the port will be 8080 and the data directory will be stored inside the server directory (perhaps c:/temp wasn’t such a good idea). 

Open the cmd and run the “…\Raven.Server.exe /install”. 

Check you services.msc and you will find RavenDb service running.





A really cool feature is the management studio which is located on localhost:8080 (or your custom port).

Here you can see all your documents, indexes and logs. The logs tab allows you to see all the queries and the commands that Raven executes.
To enable it you need to create NLog.config file in the server directory and paste the following code:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.netfx35.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
   <targets> 
      <target xsi:type="HttpEndpoint" Name="HttpEndpoint" /> 
   </targets> 
   <rules> 
      <logger name="Raven.*" writeTo="HttpEndpoint"/> 
   </rules> 
</nlog> 

Restart the process and now you have logs! 


Getting the client
After we have the RavenDb server running it’s time to add the client and start playing with Raven from the application.
For this tutorial we’ll need an Asp.Net MVC 4 project (if you don’t know how to open one read this).

In the package manager console type “Install-Package RavenDB.Client -Version 1.0.971”, the reason I do it from the console and not from the package manager is because the latest RavenDb.Client package has a Newtonsoft.Json.dll with a lower version when the version of the Newtonsoft.Json.dll that comes with the MVC project. By specifying this RavenDb.Client version we receive ravenDb.Client dlls compiled with the newest Newtonsoft.Json.dll.

Configurating Raven from the application
Similar to NHibernate, RavenDb has a DocumentStore (sort of SessionFactory) which only needs to receive the connection string name. . Paste this code inside the Application_Start in the Global.asax:
Store = new DocumentStore { ConnectionStringName = "RavenDB" };
Store.Initialize();
IndexCreation.CreateIndexes(Assembly.GetCallingAssembly(), Store);
The third row adds custom created indexes from the assembly to the RavenDb Server (we’ll 
talk about it later).
Same as the Nhiberbate’s SessionFactory, the DocumentStore has to be singleton, making it static should work for now.
public static DocumentStore Store;
Add the connection string to the “connectionStrins tag in the web.config.
<add name="RavenDB" connectionString="Url=http://localhost:8080" />
 Session managment
Almost every controller will need an access to the RavenDb Session so lets create a RavenBaseController which will be responsible for opening and closing the session.
   1: public class RavenBaseController : Controller
   2: { 
   3:    public IDocumentSession RavenSession { get; protected set; }
   4:  
   5:    protected override void OnActionExecuting(ActionExecutingContext filterContext)
   6:    {
   7:       RavenSession = MvcApplication.Store.OpenSession();
   8:    }
   9:  
  10:    protected override void OnActionExecuted(ActionExecutedContext filterContext)
  11:    {
  12:       if (filterContext.IsChildAction)
  13:          return;
  14:  
  15:       using (RavenSession)
  16:       {
  17:          if (filterContext.Exception != null)
  18:              return;
  19:  
  20:          if (RavenSession != null)
  21:              RavenSession.SaveChanges();
  22:       }
  23:    }
OnActionExecuting will open the session per web request and OnActionExecuted will flush all the changes (similar to transaction.commit). If an exception was thrown then the session will rollback.

Basic Session Usages

var student = new Student {Name = "San Yang"};
RavenSession.Store(student); 

You can check how is the class converted to a Json document using the raven 
studio. 



Similar to NHibernate (Store = SaveOrUpdate) and if the class has an id property Raven will generate a unique id (numeric or string). However if you store an object with nested objects in it, Raven won’t generate ids for them.

Queries are same as in NHibernate:

var school = RavenSession.Query<School>()
                          .Where(x => x.Students.Any(y => y.Name == "jones"))
                          .ToList();
 
            var school = RavenSession.Query<School>()
                                     .Where(x => x.Name == "")
                                      .ToList(); 

Knock yourself out.

Indexes
RavenDb’s philosophy is “we shall never make you wait” and so far they haven’t disappointed me. Perhaps the dynamic index generation helps them there.
RavenDb analysis every query that is executed on the session and creates a temporary index for it. If the query is executed several (configurable) times then the index becomes permanent.

So that should give you a nice place to start from.
More posts about advanced RavenDb usages and options are coming soon.

Tuesday, December 11, 2012

INotifyPropertyChanged without explicitly using the property Name


public class MyClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _message;
    public string Message
    {
        get { return _message; }
        set
        {
            if (_message != value)
            {
               _message = value;
               OnPropertyChanged("Message");
           }
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

All of you, Silverlight and WPF developers, recognize this code. You have dozen of those properties that call the “OnPropertyChanged” method with hardcoded property name.
Some of you perhaps solved this problem using lambda expressions or some other ways.
In this post I’d like to show you another cool way to avoid hardcoding your properties name every time you call “OnPropertyChanged”.

private void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

You: Huh?
Me: Exactly!

Let me introduce you to the “CallerMemberName” attribute that allows you to obtain the method or property name of the caller to the method.
If the Message property calls the “OnPropertyChanged” method, the propertyName default parameter will be automatically set with the calling property name in compile time, meaning that the property name will be hardcoded inside the dll.
The only catch here is that in order to use this attribute the parameter has to be default.
    public string Message
    {
        get { return _message; }
        set
        {
            if (_message != value)
            {
               _message = value;
               OnPropertyChanged();
           }
        }
    }

FYI
There’re more attributes with abilities like CallerMemberName, such as:
CallerFilePath – gives you the source file path
CallerLineNumber – returns the line number of the calling method 

  
Type safe ;)

Tuesday, November 13, 2012

Entrepreneurship Week of Israel


This week is the "Entrepreneurship Week of Israel".
A variety of entrepreneurs lecture about their success, startup ideas and tips for your great startup. 
All the lectures are free and they take place in different bars, pubs and clubs in all the big cities of Israel (you can't talk about startups and ideas and not drink beer).

Here is a link to all the upcoming events.

Enjoy.