I'm currently working on a local application, which has access to a SQL Server 2008 Express. What I did was create a Class that manages all the access and actions over the database (e.g. INSERT,DELETE,etc) but the class is growing really large, and has at least 40 methods. So my question was: Is there a way to refactor this kind of class? or is it normal to be that way?
+8
A:
Yes, it's called an Object-Relational-Mapping layer or ORM for short.
In particular I'd recommend looking at Linq To SQL as this is a nice an easy introduction. You pretty much add a new "Linq to SQL Classes" object from Add->New Item->Data
(call it DB.dbml), go to View->Server Explorer
(specify your SQL server settings), drag your SQL tables onto a designer window, build, and then can access your database like objects in code:
DBDataContext db = new DBDataContext();
var cars = db.Cars.Where(c => c.Name == "Ford Falcon");
Graphain
2010-07-20 04:14:31
Nhibernate is also a good option for ORM over .Net
Amit Ranjan
2010-07-20 05:38:36
+1
A:
Linq to Entities will do most all of this for you. Have you considered that option?
Otherwise, it seems most people end up making 1 class per table, and more complex relationship management is typically handled in the business logic tier.
Steve
2010-07-20 04:14:37
Linq to Entities vs. Linq to SQL: http://dotnetaddict.dotnetdevelopersjournal.com/adoef_vs_linqsql.htm
Steve
2010-07-20 04:17:57
+2
A:
You can try these ORM libraries for .NET:
- BLToolkit -> these is quite simple library to start playing with ORM, very easy to start with
- Linq2Sql -> this libary is a dead project, microsoft had stopped developing it, and i think it's not a good idea to use "dead" technology
- Entity Framework -> a good point to start, has and easy wizard to create initial Model with necessary table mappings (just install it and create "New entity project", the wizard will help you creating your first mappings)
- NHibernate -> its a kind a "monster" ORM mapping library, a bit complicated, but one of the most advanced libraries on .NET ORM market
Kru
2010-07-20 07:04:21