Marc Rasmussen
Marc Rasmussen

Reputation: 20565

C# Nhibernate mapping objects

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?

Upvotes: 0

Views: 739

Answers (2)

h.alex
h.alex

Reputation: 902

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

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68440

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.

Upvotes: 1

Related Questions