开发者

Fluent NHibernate: testing collection mapping with CheckList

I've used Fluent NH in some projects but I'm having some problems with using the PersistenceSpecification class for testing a collection mapping. Here's the code for my classes (I'm just putting here the collection definition):

public class Ocorrencia : EntityWithAction, IHasAssignedId<Int32> {     
  private IList<Intervencao> _intervencoes = new List<Intervencao>();
  public IEnumerable<Intervencao> Intervencoes {
   get{
   return new ReadOnlyCollection<Intervencao>( _intervencoes );
  }
 set {
   _intervencoes = new List<Intervencao开发者_Python百科>( value );
   Contract.Assume(_intervencoes != null);
 }
}   
public void ModificaEstado(Intervencao intervencao ){
 //some checks removed
 intervencao.Ocorrencia = this;
 _intervencoes.Add(intervencao);
}
//more code removed

}
public class Intervencao : EntityWithAction, IHasAssignedDomainAction {
//other code remove
internal Ocorrencia Ocorrencia { get; set; }
}

And here's the mappings (only the important things):

public class IntervencaoMapping: ClassMap<Intervencao> {
public IntervencaoMapping()
{            
    WithTable("Intervencoes");
    Not.LazyLoad();
    Id(intervencao => intervencao.Id)
        .ColumnName("IdIntervencoes")
        .WithUnsavedValue(0)
        .SetGeneratorClass("identity");
    Map(intervencao => intervencao.Guid, "Guid")
        .Not.Nullable();
    Version(ent => ent.Version)
       .ColumnName("Version");
    References(ent => ent.Action, "IdAccao")
        .Cascade
        .SaveUpdate();
    Map(intervencao => intervencao.TipoEstado, "TipoEstado")
        .CustomTypeIs(typeof (TipoEstado))
        .CustomSqlTypeIs("integer");
    Map(intervencao => intervencao.Observacoes, "Observacoes");
    References(intervencao => intervencao.Ocorrencia, "IdOcorrencias")
           .Not.LazyLoad();
}
}
public class OcorrenciaMapping: ClassMap<Sra.Ocorrencias.Ocorrencia> {
public OcorrenciaMapping()
{            
    WithTable("Ocorrencias");
    Not.LazyLoad();
    Id(ocorrencia => ocorrencia.Id)
        .ColumnName("IdOcorrencias")
        .WithUnsavedValue(0)
        .SetGeneratorClass("identity");
    Map(ocorrencia => ocorrencia.Guid, "Guid")
        .Not.Nullable();
    Version(ocorrencia => ocorrencia.Version)
        .ColumnName("Version");
    Map(ocorrencia => ocorrencia.Descricao)
        .ColumnName("Descricao");
    Map(ocorrencia => ocorrencia.Nif, "Nif")
        .Not.Nullable();
    Map(ocorrencia => ocorrencia.TipoOcorrencia, "TipoOcorrencia")
         .CustomTypeIs(typeof(TipoOcorrencia))
        .CustomSqlTypeIs("integer");
    Map(ocorrencia => ocorrencia.BalcaoEntrada, "Balcao")
        .CustomTypeIs(typeof(TipoBalcao))
        .CustomSqlTypeIs("integer")
        .Not.Nullable();

    References(ocorrencia => ocorrencia.Organismo, "IdOrganismos")
        .Cascade.None()
        .Not.Nullable();
    HasMany(ocorrencia => ocorrencia.Intervencoes)
            .Access.AsCamelCaseField(Prefix.Underscore)
            .AsBag()
            .Cascade
            .All()
            .KeyColumnNames.Add("IdOcorrencias")
            .Not.LazyLoad();
}
}

As you can see, Interncao objects are added through the ModificaEstado method which ensures that Ocorrencia reference on Intervencao "points" to a reference of Ocorrencia. Now, how do I test this relationship with the PersistenceSpecification object? I've ended up with the following code:

[Test]
public void Test() {
using (var session = _factory.GetSession()) {
    using (var tran = session.BeginTransaction()) {

        var accao = CreateAction();
        session.Save(accao);

        var organismo = CreateOrganismo();
        session.Save(organismo);

        var intervencao = CreateIntervencao();
        ((IHasAssignedDomainAction)intervencao).SetActionTo(accao);
        var intervencoes = new List<Intervencao> {intervencao};

        new PersistenceSpecification<Ocorrencia>(session)
            .CheckProperty(e => e.Nif, _nif)
            .CheckProperty( e =>e.Organismo, organismo)
            .CheckProperty( e => e.Descricao, _descricao)
            .CheckProperty( e => e.TipoOcorrencia, TipoOcorrencia.Processo)
            .CheckList( e => e.Intervencoes, intervencoes)
            .VerifyTheMappings());

            tran.Rollback();
    }
}
}

Since IdOcorrencia is defined as an external key in table Intervencoes, the previous code fails because it tries to insert the intervencoes list with IdOcorrencia set to null. If I remove the external key, then the test works fine, but I believe that I shouldn't be doing that.

I'm probably doing something wrong but I'm not sure on what that is. So, can anyone be kind enough and give me a hint on how to solve this?

thanks guys. Luis


The problem was that I was using an old version of the fluent nhibernate. recente versions have overrides which let you solve this kind of problem:

http://www.mail-archive.com/fluent-nhibernate@googlegroups.com/msg06121.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜