0

I want to allow 3rd party code to extend an NHibernate mappings at run time. This is what I have now:

Sql:

TABLE Orders
    Id INT identity,
    [more fields...]

Code in my project:

public interface IOrder
{
    int Id { get; set; }
    // more properties...
}

internal class Order : IOrder
{
    public int Id { get; set; }
}

public class OrderDAL
{
    public IEnumerable<IOrder> GetOrders()
    {
        ICriteria criteria;
        // build some criteria
        var result = criteria.List<Order>();
        RaiseOrdersLoaded(result);
        return result;
    }
}

NHibernate hbm file:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="..." assembly="...">
  <class name="Order" table="Orders">
    <id name="Id" column="Id" type="int" >
      <generator class="identity" />
    </id>
    [more properties...]
  </class>
</hibernate-mapping>

The 3rd party is a dll I load with Ioc. It knows the interface IOrder but not the Order class. Its developer is also adding a table in my database:

TABLE OrderExtension
    OrderId PK, FK from Orders
    CustomField nvarchar

Now the 3rd party developer should be able to do:

  1. Add his CustomField to the query in GetOrders. I thought about extending the hbm file in run time but I don't know how.
  2. Add criteria to the query to filter by his CustomField.
  3. Listen to OrdersLoaded event and get his data somehow.

Is all this possible?

Thanks

Amiram Korach
  • 13,056
  • 3
  • 28
  • 30

1 Answers1

1

You can put Named Queries in the mappings, but these would be in SQL: http://nhibernate.info/doc/nh/en/index.html#querysql-namedqueries

You can also define filters in the mappings, that will get injected into other types of queries: http://nhibernate.info/doc/nh/en/index.html#filters

As for the Criteria, Queryover, HQL and LINQ queries, you can build whatever construct you like in the Orders class to make it possible to configure via some initialization method that the plugin can call.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
Oskar Berggren
  • 5,583
  • 1
  • 19
  • 36
  • Thank you for the answer. As development on my side progress I need to completely change my question. I'll ask again and hopefully you'll be able to help me. – Amiram Korach Jan 03 '13 at 05:52
  • Here is my new question http://stackoverflow.com/questions/14134066/initialize-associated-entity-or-collection-fails-with-stateless-session-in-nhibe – Amiram Korach Jan 03 '13 at 06:34