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.