2

I am trying to map the collection for two days without success. I also read all possible articles and forums but still down there. Ok, here is the problem:

1) The collection class contains a private field "_internalCollection" which is mapped with NHib.

2) The holding entity should expose the collection trough readonly property.

3) I want to avoid implementing NHibernate interface IUserCollectionType!!!

I did this with xml mapping and it work great. The WarehouseEntity is a collection Item. Warehouses is a readonly property in OrgEntity class.

  <component name="Warehouses" class="Core.Domain.Collections.EntitySet`1[Core.Domain.OrgStructure.IWarehouseEntity,Core],Core">
    <set name="_internalCollection" table="`WAREHOUSE`" cascade="save-update" access="field" generic="true" lazy="true" >
      <key column="`WarehouseOrgId`" foreign-key="FK_OrgWarehouse" />
      <!--This is used to set the type of the collection items-->
      <one-to-many class="Domain.Model.OrgStructure.WarehouseEntity,Domain"/>
    </set>
  </component>

Any idea how can I do it with fluent NHibernate?

EDIT: Core.Domain.Collections.EntitySet`1 is base collection class. It provides basic functionality for working with collections and can fit any class which is IEntity interface.

mynkow
  • 4,408
  • 4
  • 38
  • 65

1 Answers1

3

Try:

HasMany(x => x.Warehouses)
    .AsSet().KeyColumn("WarehouseOrgId")
    .Access.CamelCaseField(Prefix.Underscore)
    .ForeignKeyConstraintName("FK_OrgWarehouse");

Edit: I missed a key part of the question so here's another try:

Component(x => x.Warehouses, m =>
    {
        m.HasMany<Warehouse>(Reveal.Member<EntitySet<IWarehouseEntity>>("_internalCollection")
           .AsSet().KeyColumn("WarehouseOrgId")
           .ForeignKeyConstraintName("FK_OrgWarehouse");
    });

I'm sure that's not it but hopefully it puts you on the right path. Also have a look using ComponentMap.

My advice is to avoid custom collections entirely. I replaced all of ours with extension methods on IEnumerable<T>.

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • This wont work. The name of the property is not mentioned here. How Fluent will map the field? Also Warehouses is not of type ISet but EntitySet<>. – mynkow Jul 04 '10 at 18:48
  • I think this will work. Have to run all tests and see it is but the Reveal class was what I looking for... 10x Component>( x => x.Departments, m => { m.HasMany( Reveal.Member>( "_internalCollection" ) ).Access.Field() .KeyColumn( "DepartmentOrgId" ) .ForeignKeyConstraintName( "FK_OrgDepartment" ).AsSet(); } ); – mynkow Jul 05 '10 at 07:33
  • One question. Can you use IEnumerable and extension methods + adding a property SelectedItem and this SelItem to be mapped with NHibernate. If it is possible I will take your advice... – mynkow Jul 05 '10 at 07:37
  • I don't understand the question, you should probably ask it as a new question to get an answer. I use the extension methods to filter collections. – Jamie Ide Jul 05 '10 at 12:49
  • How your extension for adding items to IEnumerable look like? – mynkow Jul 05 '10 at 20:22
  • Please make this a new question. – Jamie Ide Jul 05 '10 at 20:37
  • http://stackoverflow.com/questions/3185320/c-extension-method-for-additem-to-ienumerablet – mynkow Jul 06 '10 at 10:16