0

ive asked a similar question but i am still in doubt about what to do so i wanted to ask this question with abit more information:

Say for instance i have the following Object:

    public class Callback : SuperObject
{
    public virtual DateTime PERIOD { get; set; }
    public virtual int COMPLETED { get; set; }
    public virtual int COMPLETED_WITHIN_2HOURS { get; set; }
}

Now the if i add an XML file containing the mapping information:

    <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Henvendelser" namespace="Henvendelser.Objects.Callback">
  <class name="Henvendelser.Objects.Callback">
    <id name="PERIOD" column="PERIOD">
      <generator class="identity" />
    </id>
    <property name="COMPLETED" />
    <property name="COMPLETED_WITHIN_2HOURS" />
  </class>
</hibernate-mapping>

Then the following will run perfectly fine:

        var query = new StringBuilder();
    query.Append("SELECT   LAST_UPD AS PERIOD, ");
    query.Append("COUNT(CASE WHEN STATUS ='Færdig' THEN 1 END) as completed, ");
    query.Append("COUNT(CASE WHEN SOLVED_SECONDS /60 /60 <= 2 THEN 1 END) as completed_within_2hours ");
    query.Append("FROM     KS_DRIFT.NYK_SIEBEL_CALLBACK_AGENT_H_V ");
    query.Append("WHERE    LAST_UPD BETWEEN '" + start.ToString("yyyy-MM-dd") + "' AND '" + end.ToString("yyyy-MM-dd") + "' ");
    query.Append("AND      STATUS ='Færdig' ");
    query.Append("GROUP BY LAST_UPD ORDER BY LAST_UPD ASC");
    var session = sessionFactory.OpenSession();
    var result = session.CreateSQLQuery(query.ToString()).AddEntity(typeof(Callback)).List<Callback>();

Which is great however in a larger program i might have 10 or more different objects which means that in this case i would have to make 10 xml files that maps the object. Now in my logic the mapping xml is basicly saying how the object class already looks like so it seems kinda redundant!

is there a better way?

2
  • Not really helping I know - but why are you using an ORM if you're just going to ignore what it gives you and build your query yourself? Commented Apr 11, 2014 at 12:47
  • @SimonWhitehead i am kinda new to the whole ORM thing and trying to learn and im not quite sure how to make costum querys where you have count and sum with Nhibernate yet Commented Apr 11, 2014 at 12:49

2 Answers 2

1

To avoid having to define mappings for every class you could use Fluent NHibernate with automapping

Fluent NHibernate has a concept called Auto Mapping, which is a mechanism for automatically mapping all your entities based on a set of conventions.

Auto mapping utilises the principle of convention over configuration. Using this principle, the auto mapper inspects your entities and makes assumptions of what particular properties should be. Perhaps you have a property with the name of Id and type of int, the auto mapping will decide that this is an auto-incrementing primary key.

By using the auto mappings, you can map your entire domain with very little code, and certainly no XML. There are still scenarios where it may not be suitable to use the auto mapping, at which point it would be more appropriate to use the Fluent mapping; however, for most greenfield applications (and quite a few brownfield ones too) auto mapping will be more than capable.

As other user pointed out, it make no sense to use a ORM and create plain sql queries to load data. You could need to do that in a very corner case scenario, but it can't be the rule, otherwise it make no sense to use NHibernate at all.

I'd recommend you to start looking the very basic of how to use NHibernate.

Sign up to request clarification or add additional context in comments.

5 Comments

is Fluent Nhibernate a plugin or does it come with the normal installation of Nhibernate?
@Marc Rasmussen: it is something separated. You have to install it using nuget. Also, I want to point out that I'm not sure if this is the best moment to start with nhibernate. Library development is not very active since long and the future is not very certain. Main developers left the project long time ago.
@Claduio could you give an example of how the above select statement could be done using only fluent Nhibernate? Thank you for your answer btw it is very helpfull when learning
@Marc Rasmussen: I'm afraid the query is quite complex and I'd need a lot of time to create it, time that unfortunatelly I don't have. Sorry man.
NH kicks ass! Learn it! ;)
0

Also, there is the ConfORM auto mapper. It is magical. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.