Friday, October 7, 2011

FubuMVC - Upgrade to 0.9 Note

Just a quick note:

I upgraded my fubuMVC project to version 0.9.1.. and it couldn't load my WebForms. After some emailing with the group, I figured out my fubuRegistry needs to get my view-engine's config. Unfortunately, it's not mentioned in the techie documents.



So, Please add this statement right into your FubuRegistry class if you use 0.9. versions and above.
It is noteworthy to say that FubuMVC is going to hit the 1.0 very soon.

Tuesday, August 30, 2011

FubuMVC - Nuget

FubuMVC now is available via Nuget.Org.

You can install this package in Visual Studio's Package Manager Console by this command.

Install-Package FubuMVC

if you are not interested in Fubu's other packages( Spark, FubuMVC.Validation...) you can get the minimal version on nuget.

Install-Package FubuMVC.References

Also, a nice document is designed by the development team.
Take look at FubuMVC Documents.

Friday, August 26, 2011

EF Code-First in the shape of NHibernate - Profiling



ORM tools made datacentric programs too easy to develop, but if it's been used without care, it will soon face a disaster in terms of SQL efficiency.
Microsoft proposes a tool for the developers who wants to know what is happening under the hood in EF.
It means, all SQL statements and information can be traced and logged in a file based on the developer's desire.
All you have to do is :
   1. Install two wrappers for EntityFramework from Nuget.org

  •    CommunityEFProviderWrappers.EFProviderWrapperToolkit
  •    CommunityEFProviderWrappers.EFTracingProvider

   2. Create a DbConnection object that is configured to log SQL staements like the one below and inject this object into DbContext.

var context = new DbContext(CreateTracingConnection(connectionString, "System.Data.SqlClient");
        detail implementation of CreateTracingConnection is written below.


   3. EnableTracing for data context.

      ((IObjectContextAdapter)this).ObjectContext.EnableTracing();

By this approach, you definitely know what commands are being executed when your application is running in debug mode.




Friday, June 24, 2011

EF Code-First in the shape of NHibernate - Using (5/5)



Once your database is built, using EF Code-First for the rest of the project is as easy as NHibenate. Same Patterns like Repository, Unit of work... can be applied to the codes in your application.

Even there is no difference between Code-First or Database-First (the one that you create your model out of your database) mode in your business logic code.

By this approach your model (Genrally POCO [ Plain Old CLR Object]) is first initialized and database is built as the result of your object model. By some tricks like migrations you can narrow your database recreation down to some field name modification or changes in size.

EF Code-First in the shape of NHibernate - Migrating Changes (4/5)

One of the hassles every application developer might have faced during his/her career is how to best synchronize changes between model and database schemas (versioning problem).Several solutions have been proposed but I personally believe ORM tools should care about this problem in their hearts.

Microsoft's EntityFramework.Migrations is a tool designed for going over such hurdles. Although it has some incompatibilities between different versions of EF, but it looks promising. Technically speaking, the tool checks current object model with the one on database( if any). Any differences are detected and written to a script file and finally migration tool is in charge of running the script file and applies changes to database. 

Note : All you have to do to install is write Install-Package  EntityFramework.Migrations in package manager console.  

There 2 ways to create migration, Manual Migration that uses code-based migration and Automatic. I will pick automatic which is very easy to use.
All we have to write is :

    public class Settings : DbMigrationContext<YourContext>
    {
        public Settings()
        {
            AutomaticMigrationsEnabled = true;
            SetCodeGenerator();
            AddSqlGenerator();
        }
    }

Note: replace YourContext with your DbContext class in your project.


Two commands are handy in migration panel.

  • Add-Migration name that generates a script file for all the changes between object model and database schema.  
  • Update-Migration that pushes all pending changes to the database and applies them on database.

Friday, June 10, 2011

EF Code-First in the shape of NHibernate - Mapping Files (3/5)

Once the DbContext is configured, Let's take a look at how can EF comprehend our model and generates schema out of it.

Simply EF looks through all classes listed in OnModelCreating() method, and based on some conventions, it realizes that which classes has to be converted into SQL tables, their SQL column attributes(name, type, size..) and how they are associated with each other. i.e One to One, One to Many, Many to Many.

For those NHibenate users who are pretty familiar with FluentNHibernate.Org, that is really easy for them to switch to EF conventions.

Here's the what DocumentMap looks like:


As you see all mapping classes should inherit from EntityTypeConfiguration. This class accepts a generic class and it has to be our domain class.
By looking at the class internals, you' ll see some conventions are used :
   1. HasKey gets primary key for the table as lambda expression.
  2. Property gets Number property in Document model as SQL column and set as not null.
  3. ToTable sets the name of generated table.

All other properties are converted to SQL columns based on EF conventions.( Note; the EF conventions can be modified manually but that's beyond the purpose of this series)

Same thing applies to DoumentDetailMap, but there is a little tricky point in setting up One-Many in mapping class.




as it clearly shows, HasRequired get Document object in DocumentDetails class then it calls WithMany that related Document to DocumentDetails. Foreign keys can be included in config and at the end, relation can be set to delete all children orphaned rows.

Friday, June 3, 2011

EF Code-First in the shape of NHibernate - Data Context (2/5)


Context in EF are the same as Sessions in Nhibernate. It's very expensive to create and once it's been created we have to watch it till it's been disposed.


Context in EF can be configured simply by inherit from DbContext(System.Data.Entity). You can also inject your ConnectionString  into the context before it's been constructed. Once any ConnectionString mentioned, EF gets its configuration from local SQLExpress on your machine. 


There is only one method that should be overridden when using this approach, OnModelCreating. This method is responsible to go through all mapping classes (next post on the series) and create schema that a database engine needs.




On the next post, I am going through the details of DocumentMap, DocumentDetailMap.

FA |-| I M

EF Code-First in the shape of NHibernate (1/5)

When I have first come across Entity Framework, I was looking for the ways to put my database schema generated by the object model I have in my domain model. This way, I feel like I am coding in NHibenate that makes me satisfied with the result. Because of the testability, maintainability it brings to my code.Unfortunately, It was practically impossible to code that way till now that Microsoft released Entity Framework 4.1 Code-First. 
In the following series I am going to show some of the ways that might be helpful for production code that are very much close to the NHibernate's best practices but this time EF is under the hood. 
My domain model is simple accounting model used for my pet project.





Before I go further, Latest versions of Entity Framework(including code-first) can be get via nuget.org.
You can write (Install-Package EntityFramework) in package manager and get the entity framework.



Saturday, March 26, 2011

FuBuMVC - WebForms

FubuMVC is a plugable framework when User interface comes into play. It can be easily switched from WebForm view-engines to Spark. Other view-engines will be supported in near future.

However, By default WebForm view-engine is available in all samples and there is one convention that is applied for all WebForm pages. For example, if you have a page called HomePage.aspx and your page is inherited from a class called HomePage.

<%@ Page Language="C#" Inherits="MyProject.UI.Actions.Home.HomePage" %>
then HomePage class should be inherited from FubuPage and implements like that :

    public class HomePage : FubuPage
    {
    }

Where "HomeViewModel" is the exact object returned from HomeAction's Execute method ( last post). That means after an action is executed, the returned object is hanged to its own page by convention. By this approach, developers don't need to worry about their code-behind class(such as what we have in WebForm) and of course, if they go to other view-engines. Because, all business codes are in Execute Method.

 

FubuMVC - Actions

FubuMVC is shipped with a sample called DarthFubuMVC on GitHub. If you looak at the project, It has some sort of folder structure like what we have in Asp.Net MVC. In there, you can find Actions folder which is similar to Controllers in Asp.Net MVC.

Generally speaking, Fubu is designed based on One Model In - One Model Out. i.e you as developer have to define a type of object as input argument (model) and define another type of object as return value.

The example below explains more in code.

    public class HomeAction
    {
        public HomeViewModel Execute(HomeInputModel model)
        {
            return new HomeViewModel();
        }

    }

     public class HomeInputModel
    {
    }
     public class HomeViewModel
    {
    }

Every Action/Controller class should have Execute method, in order to run. While this method as mentioned above, should have unique object as input and output.

By default, Fubu's routing engine runs HomeAction's execute method when it reaches the url like this:

http://MyPrject.Com/Home.

As stated in previous posts, all suffixes like action,Command, Query... is ignored due to simplicity of Fubu's routing system. Jeremy fully explained the routing system's internals in this post

Friday, February 25, 2011

FubuMVC - IOC Container

Another niceties of FubuMVC is that it has its own IOC in its heart. Right now just StructureMap is supported but it's been said that other well-known IOC containers might be supported in future (Castle windsor, Autofac,.. even Unity!!)

Look how easy it is to bootstrap IOC in application's startup evet:
 
// ConfigureFubuMVC is explained in the last post



And NhibernateRegsitry class contains all information for our interfaces.
    public class NHibernateRegistry : Registry
    {
        public NHibernateRegistry()
        {
            ForSingletonOf().Use();
            For().HybridHttpOrThreadLocalScoped().Use();
            ....
            .......
        }
    }


Friday, February 18, 2011

FubuMVC - Startup Conventions

FubuMVC is working based on conventions. Although some conventions are set as default in the heart of framework, you as a developer may want to extend or install a brand new convention. That makes programming very easy and efficient. There is no need to tell the code which controllers' method should be execute on and on.

Look how we can configure the fubu :



In the first lines, I tell the framework that configuration only applies to MyProject.dll and MyProject.Data.dll.
Then all classes that ends with action are my controllers. (with mvc literature). 
Then ignore all namespaces for action classes also ignore certain suffixes, for example, if an action class is deep in the project like (MyProject.UI.Actions.Exchange.AddExchangeAction), for routing just call AddExchange.

All Views that are using the model on their back, should have the same name as their models.
This stuff is set when the application starts and no needs to reconfigure for each situation.

Friday, January 28, 2011

FubuMVC - Why

ASP.net MVC has been available for .Net developers for over three years. Despite all the good changes, we've seen through CTP (Beta) versions till now, It has not become mainstream in Microsoft and to be honest it could not grab new developers to .Net world as easy as Ruby did the same during last year. On the other side, Microsoft still heavily promotes his/her developers to provide webform solutions on the web. These are all  the reasons I personally believe asp.net mvc has been faced through out these years.

I have had a look at ASP.net MVC since its early CTPs and I have done two different projects by that. However, some architectural limitations led me into very bad software practices. Violating SRP (Single Responsibility Principle) & OCP(Open-Closed Principle) to name a few.   
While I was working around how to best overcome some design points, I have come across a new open-source project (FubuMVC [For US By US MVC]) which supervised by Jeremmy Miller.( Also Author famous IOC container StructureMap)

Technically speaking, all design limitations in asp.net mvc were removed and the project was inspired by Ruby language( convention over configuration). I forked the project and after some days I could write some webpages based on that framework.
Till now, one the problems with this project is poor documentation. Although some new wiki sites have been built to help new developers the whole idea better.

Although most of the contributors to this projects are advanced developers, I actually want to explore more into this framework and will share my understanding to others.

I hope I will write a new web application by this front-end framework soon.