Reputation: 1409
I have SQL server db ready with simple (not many constraints) tables. Now i need to generate DAL and object classes for tables.
How much can .NET ORM (like NHibernate or any other free FW you suggest) help in this ? Basically helping out with laborious tasks of writing class and db access functions. I will better spend time in writing business logic for those object classes.
Will it be easy to add/alter tables later ? I have conceptual idea about ORMs but no practical experience in using any.
Upvotes: 3
Views: 223
Reputation: 1
Try Vega https://github.com/aadreja/vega. One of the best & fastest .net ORM with enterprise features.
Upvotes: 0
Reputation: 1
I'm a little late the party, but Cocoon ORM on Nuget and Github is good. It writes the data access code dynamically. http://guidelinetech.github.io/cocoon-orm/
Feature list below from the website:
Upvotes: 0
Reputation: 63522
You could use Linq to Sql or Entity Framework to autogenerate your classes as well as Data Access functionality.
You can drag and drop your database tables into the DBML (L2S) or EDMX (EF) from within Visual Studio and it will build your classes with members mapped one-to-one to columns, as well as any relationships you have defined.
To create Linq to Sql Classes
or ADO.NET Entity Data Model
follow these steps:
Linq to Sql Classes
or ADO.NET Entity Data Model
Server Explorer
to open a connection to your databaseFor instance:
User
becomes class User
User
has column FirstName
it becomes property FirstName
User
has a relationship with table Contact
it will generate IList<Contact> Contacts
on your User
class. This will let you write code like user.Contacts.Where(c => c.ContactId == 7000)
from within the generated DataContext
If you need a more customized way to map your tables you can use SQLMetal which will also generate your classes but allow you to make changes. For instance, converting int
properties to enums
.
NHibernate is also a good option and has come a long way.
Upvotes: 3
Reputation: 937
Nhibernate will handle all of your data access logic. I recommend that you define every foreign key and primary key that you can. It is also simpler to have an explicit primary key instead of a composite primary key.
To generate the object classes, I recommend using T4 templates. There are various sample templates available online and it is relatively straight forward to write your own to do exactly what you want.
It is also a good idea to let these object classes serve as data containers and implement your business logic in separate classes that use these DAL classes. This will simplify being able to regenerate them should the underlying tables change.
Upvotes: 0
Reputation: 120937
Both Linq2Sql and Entity framework can help you with this to a great extent. With regards to your question about updating tables: Yes, it is quite easy to update tables later. At least a lot easier than if you had written everything by hand (where you would have to do modifications by hand too).
Upvotes: 1
Reputation: 499042
There are many tools that will generate DALs for you.
SubSonic is not an ORM, but a code generation tool that will generate a DAL for you.
There is Entity Framework and Linq to SQL (from Microsoft) that will also generate a DAL for you, with a design surface in visual studio.
With all of these adding and altering tables required regenerating the DAL, so changes and customizations to these files can get lost.
Upvotes: 1