3

I will be building an application but I am not sure about what framework to use. At places where I have worked, they have used Ntiers with codesmith, nettiers being free but not codesmith. I found out about the Entity Framework but I have also read that the way to use it would be by using something called a "Repository Pattern" but there is a lot of doubts about using this. I do not have a big budget and its only myself, cant afford an expensive tool that would automatically create my classes with update, delete, create capabilities and leaving some code for me to implement. So I am not sure what way to go, should I go entity framework (is this the only one free?), or is there out there something else i could use, maybe not so expensive but reliable.

I would really appreciate a good advise on this as I expect, eventually this application will grow and grow and would like to be easy to maintain.

PS: I would go with entity framework and the repository pattern if that would be like my best choice.

Using C#, Asp.net and MSSQL 2008.

Sandeep Kumar M
  • 3,841
  • 3
  • 38
  • 59
user710502
  • 11,181
  • 29
  • 106
  • 161
  • .NET ORMs - http://stackoverflow.com/questions/1377236/nhibernate-entity-framework-active-records-or-linq2sql/ – Michael Maddox Nov 28 '12 at 21:48
  • I wanted to point out that I work for CodeSmith Tools and we are always willing to work with customers on pricing. Just drop sales an email (codesmithtools.com/contactus). We have EF templates but they do not use the repository pattern. However, you can customize the templates/generated code to use a generic repository pattern quite easily. Also an old saying: A tool that can save you tons of time is well worth the price in gold. – Blake Niemyjski Dec 31 '12 at 16:56

3 Answers3

4

Entity Framework with code first and automatic migrations is one of the easiest ORM's you can use. It's flexible, well supported in the Microsoft stack, and with code first, one of the fastest ways of developing your database.

Using Entity Framework code first, you define your data models as just normal C# classes. This will correspond to the Product table:

public Product {

   public int Id { get; set; }
   public string Name { get; set; }
}

Create a database context.

public MyDbContext : DbContext {

    public DbSet<Product> Products { get; set; }
}

Add the connection string to Web.config, and run the commands in the package manager console:

>Enable-Migrations –EnableAutomaticMigrations
>Update-Database

And you now have a functional database with the Products table. When you make changes to your classes, just run Update-Database again, and it'll migrate the schema for you.

To add a new product:

using (var db = new MyDbContex()
{
    Product product = new Product() { Id = 1, Name = "Tablet" };

    db.Products.Add(product);

    db.SaveChanges();
}

Querying your data becomes as easy as:

using (var db = new MyDbContex()
{
    // get product with id == 1
    Product product = db.Products.Single(p => p.Id == 1);
}

As for the repository pattern, it's just a design pattern. There's a lot of different variations of the repository pattern, but it's really about separating your data stores from your data access layer. This makes unit testing easier, without having to rely on an external database being present.

Edit from your comment: There's a couple ways of using EF and stored procedures. See Does Entity Framework Code First support stored procedures? and Using Stored Procedure to Insert Data.

Community
  • 1
  • 1
mfanto
  • 14,168
  • 6
  • 51
  • 61
  • Thank you very much for the insight, one question, what if I ever need to create a stored procedure to give me some specific data, how does this work in such case? – user710502 Nov 27 '12 at 04:46
  • Edited my answer to add information on using stored procedures. – mfanto Nov 27 '12 at 04:53
2

another list that you can choose is:

Data Tier: Ibatis.Net Or NHibernate (Mostly I used both, NHibernate for CURD, and IBatis.NET for Query in necessary, for example, invoke SP, query with dynamic).

Spring.Net : IoC helper

Asp.NET MVC: MVC framework.

Benifit is: IBatis, NHibernate and Spring.Net are quite simliar with the Java versions, so you can find a lot of documents for them.

What I can said is that 3 tier with a good ORM (EF,Nhibernate or Ibatis) is a best practics for complex project,and it is easier for maintaince and cooperation. If your project is very simple, LINQ + WebForm can help you in several hours, you can abandon MVC at all.

ray_linn
  • 1,382
  • 10
  • 14
2

I think it all depends on where you are as a developer and what you want to do. As far as cost- Entity Framework is free with Visual Studio. Fluent NHibernate is very comparable to EF and it also works over Oracle, MySql and MSSQL and you can get it super easy via nu get.

A repository pattern is just pattern for fetching collections.

I would say both ORM's have their pros and cons.

J.Wells
  • 1,749
  • 12
  • 13