fluent-nhibernate icon indicating copy to clipboard operation
fluent-nhibernate copied to clipboard

PersistenceSpecification not working with Compenents mapped

Open alexjamesbrown opened this issue 12 years ago • 11 comments

I've got the following PersistenceSpecification:

new PersistenceSpecification<MyClass>(session)
        .CheckProperty(c => c.Name, "Testing")
        .CheckProperty(c => c.Address.Address1, "Address 1")
        .CheckProperty(c => c.Address.Address2, "Address 2")
        .CheckProperty(c => c.Address.Address3, "Address 3")
        .VerifyTheMappings();

When this is run, the .CheckProperty(c => c.Address.Address1, "Address 1") throws an error.

Upon looking in my database, I see the Name column has been set (to "Testing") however the columns mapped to the component are not

I've mapped the Address component like this (In MyClassMap)

  Component(x => x.Address, m =>
  {
    m.Map(x => x.Address1);
    m.Map(x => x.Address2);
    m.Map(x => x.Address3);
  });

alexjamesbrown avatar Nov 01 '12 13:11 alexjamesbrown

As it turns out, this is because my Address was null

Adding this to the ctor of MyClass fixed this:

  Address = new Address();

But...

Shouldn't PersistenceSpecification of thrown an exception?

If I tried to manually create it like this:

var myClass = new MyClass();
myClass.Address.Address1 = "Something";

Then I get an exception...

Something not right somewhere?

alexjamesbrown avatar Nov 01 '12 16:11 alexjamesbrown

What kind of exception? May be you can post a stacktrace? I'm not quite following, I'm sorry

chester89 avatar Nov 01 '12 17:11 chester89

Sorry.. When I say 'then i get an exception' I mean that I (quite rightly) get object not set to an instance of an object....

But that's in 'my' code - not using PersistenceSpecification

alexjamesbrown avatar Nov 01 '12 18:11 alexjamesbrown

I'm not sure that Persistence Specification can figure out that you're using a component. May be there are some specific way to tell it about that, like CheckReference method that verifies that you have a n-to-one relationship?

chester89 avatar Nov 01 '12 18:11 chester89

I'm having a look at the code - it seems strange that CheckComponentList exists, but CheckComponent doesn't :(

chester89 avatar Nov 01 '12 19:11 chester89

Yeah.. that's what I thought...

There should be a way of testing this mapping though I think?

alexjamesbrown avatar Nov 02 '12 10:11 alexjamesbrown

I'm thinking that writing your own extension method like CheckReference is a way to go.

chester89 avatar Nov 02 '12 10:11 chester89

The correct way to test this would be to instantiate your own instance of the class you want to persist, then use the overload of PersistenceSpecification.VerifyTheMappings that takes an instance of the class.

hotgazpacho avatar Nov 04 '12 12:11 hotgazpacho

@hotgazpacho perfect, thanks I think the wiki page should be updated to show that....?

alexjamesbrown avatar Nov 06 '12 13:11 alexjamesbrown

That sounds like a good idea Alex.

On Wed, Nov 7, 2012 at 12:28 AM, Alex Brown [email protected]:

@hotgazpacho https://github.com/hotgazpacho perfect, thanks I think the wiki page should be updated to show that....?

— Reply to this email directly or view it on GitHubhttps://github.com/jagregory/fluent-nhibernate/issues/187#issuecomment-10110390.

James Gregory

Tel: +61 (0) 411 619 513 Website: http://jagregory.com Twitter: @jagregory http://twitter.com/jagregory

jagregory avatar Nov 06 '12 22:11 jagregory

How did you solve this? Extension method?

chester89 avatar Feb 14 '13 19:02 chester89