What is so great about ORM?

后端 未结 4 1022
花落未央
花落未央 2021-02-15 10:39

So I\'m having a head against the wall moment and hoping somebody can come help either remove the wall or stop my head from moving!!

Over the last 3/4 weeks I\'ve been i

相关标签:
4条回答
  • 2021-02-15 10:50

    In my experience, most ORMs end up being way more complex than SQL. Which defeats the entire purpose of using them.

    One solution I'm enthusiastic about is LINQ2SQL. It excels as a thin layer about stored procedures or views. It's really easy to use and doesn't try to hide SQL.

    0 讨论(0)
  • 2021-02-15 11:00

    ORM let's you:

    1. To map table rows to objects, that are the the workable pieces of object oriented programming.
    2. To automatically navigate through object relationships
    3. To easily add, edit and remove table rows
    4. To query the database in a more intuitive way as you don't have to think of joins (this one will depend on the ORM and the query method)
    5. To transparently handle L1 and L2 cache.

    All of the above would have to be handled by hand if you werent using ORM.

    PS: I agree to Dmitry as to the startup time of NHibernate (see question comments). Besides, did you try Fluent NHibernate? Fluent NHibernate is impressively easy. I couldn't believe my eyes when I first mapped a database. It's even easier than proprietary ORMs like DevExpress XPO.

    0 讨论(0)
  • 2021-02-15 11:09

    The biggest benefit of an ORM tool is that it will help you layer your application correctly. Most project nowadays use a Data Layer to connect to the database. You start from the ORM tool to produce classes that correspond to your database objects. Then you define an interface using these methods. All persistence code uses the methods of this interface. This way the business logic layer is only coupled to this higher-layer interface and needs to know nothing about the database. In fact there should be no dependency on ADO.NET or even NHibernate.

    Another advantage of ORM tools is that you de-couple your application from the database server. You could change the db engine and still use the same code. Also there isn't only the complexity of the SQL that the ORM hides from you. It can also help you with transactions logic and connection pooling.

    I'd say that for new projects an ORM tool is a necessity. For legacy projects it isn't so much beneficial, unless of course you have the time/money to start from scratch.

    0 讨论(0)
  • 2021-02-15 11:10

    There are basically two questions here:

    What's great about ORMs? There are similar questions on Stackoverflow. See:

    • What are the advantages of using an ORM?
    • Is everyone here jumping on the ORM band wagon?

    How can I improve NHibernate startup time? See:

    • http://ayende.com/Blog/archive/2007/10/26/Real-World-NHibernate-Reducing-startup-times-for-large-amount-of.aspx
    • http://nhforge.org/blogs/nhibernate/archive/2009/03/13/an-improvement-on-sessionfactory-initialization.aspx
    0 讨论(0)
提交回复
热议问题