2010-11-17 35 views
2

好吧,這是我的非常非常流暢的hibernate項目。我在hibernate和nhibernate中有很少的經驗。如何用流利的nhibernate(schemaexport)測試生成表?在asp.net上下文

由於這是一個網絡應用程序項目,所以這個上下文對我來說是全新的。 所以我有我的webapp項目與大多數在網上找到的流利nhibernate。 所以我有這樣的實體:

namespace myproject.model 
{ 
    public class Request 
    { 
    public virtual string Id { get; private set; } 
    public virtual Route route { get; set; } 
    public virtual int code { get; set; } 

    } 
} 

namespace myproject.model 
{ 
    public class Route 
    { 
    public virtual string Id { get; private set; } 
    public virtual string client_id { get; set; } 
    public virtual IList<Request> requests { get; set; } 

    public Route() 
    { 
     requests = new List<Request>(); 
    } 

    } 

} 

//Mapping are like this.will only post one 
namespace myproject.mappings 
{ 
public class RequestMap : ClassMap<Request> 
{ 
    public RequestMap() 
    { 
     Id(x => x.Id); 
     Map(x => x.short_code); 
     References(x => x.route); 
    } 
    } 
} 

//NhibernateSessionPerRequest 
namespace myproject.Boostrap 
{ 
    public class NhibernateSessionPerRequest : IHttpModule 
    { 
    private static readonly ISessionFactory _sessionFactory; 

    static NhibernateSessionPerRequest() 
    { 
     _sessionFactory = CreateSessionFactory(); 
    } 

    //all others IHttpModule event and methods are here 
    private static ISessionFactory CreateSessionFactory() 
    { 

     FluentConfiguration configuration = Fluently.Configure().Database(MsSqlConfiguration.MsSql2005. 
                       ConnectionString(x => x.FromConnectionStringWithKey("localdb"))) 
      .Mappings(m => { 
          m.FluentMappings.AddFromAssemblyOf<myproject.model.Request>(); 
          m.FluentMappings.AddFromAssemblyOf<myproject.model.Route>(); 
          } 
        ).ExposeConfiguration((c)=> savedConfig = c);; 

     return configuration.BuildSessionFactory(); 
    } 

    } 

    private static Configuration savedConfig; 

    public static void BuildSchema(NHibernate.Cfg.Configuration config) 
    { 
     new SchemaExport(config).Create(false, true); 
    } 

    public static void BuildSchema(ISession session) 
    { 
     var export = new SchemaExport(savedConfig); 
     export.Execute(false,true,false,session.Connection,null); 
    } 


} 

我爲了測試我添加了一個測試項目(類庫)表的生成添加模塊webconfig

<add name="NhibernateSessionPerRequest" type="myproject.Boostrap.NhibernateSessionPerRequest"/> 

添加參考nunit.framework 2.8.5和myproject。

namespace myproject.Tests 
{ 
    [TestFixture] 
    public class CanGenerateSchemaTestSuite 
    { 
    [Test] 
    public void CanGenarateSchema() 
    { 
     NhibernateSessionPerRequest.BuildSchema(NhibernateSessionPerRequest.GetCurrentSession()); 

    } 
    } 
} 

的測試方法總是失敗,我具有此異常:

CanGenerateSchemaTestSuite(1個試驗),1次測試失敗:兒童測試失敗 CanGenarateSchema,失敗:System.TypeInitializationException

那麼在asp.net環境下如何測試? 感謝您閱讀本文。謝謝

回答

5

這是我的一個例子。您需要使用ExposeConfiguration,並通過它接受一個配置的方法,你只要建立數據庫那裏,然後用SchemaExport

class SqliteRefSessionFactoryProvider : ISessionFactoryProvider 
{ 

    public const string SqliteRefFileName = "ref.db"; 

    public ISessionFactory GetSessionFactory() 
    { 
     return Fluently.Configure().Database(
      SQLiteConfiguration.Standard.UsingFile(SqliteRefFileName).ShowSql()) 
      .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SsoToken>()) 
      .ExposeConfiguration(BuildSchema) 
      .BuildSessionFactory(); 
    } 

    private static void BuildSchema(NHibernate.Cfg.Configuration configuration) 
    { 
     if (File.Exists(SqliteRefFileName)) 
      File.Delete(SqliteRefFileName); 

     new SchemaExport(configuration) 
      .Create(false, true); 
    } 
} 
+0

哦,那真的很有你。我已經看到了這個竅門。但還有一個問題仍然存在。對於if.I '必須在我的'NhibernateSessionPerRequest'類和'BuildSchem'類中創建private.So如何運行'BuildSchema'方法,甚至進一步如何在我的'測試項目'中運行它?謝謝 – 2010-11-17 12:34:19

+0

正如你所看到的,我有自己的ISessionFactoryProvider,它被注入會話工廠單例中。對於單元測試,我使用一個測試來完成構建。這是你的意思嗎? – Aliostad 2010-11-17 13:04:31

+0

你好,我沒有使用任何類型的依賴注入圖書館,我的問題在這裏不能夠弄清楚如何在一個單獨的項目(意思是我的測試項目)測試這個,我已經更新了這篇文章的標題,並添加新的方法,我從你的答案中得到。請讓我知道如果我錯了。感謝您的時間 – 2010-11-17 14:03:14

7

剛上其他解決一個模糊的評論;您不需要完全刪除數據庫文件;只需刪除表格:

.ExposeConfiguration(SetupTestDatabase) 

... 

private static void SetupTestDatabase(NHibernate.Cfg.Configuration config) 
{ 
    var schema = new SchemaExport(config); 
    schema.Drop(true, true); 
    schema.Create(true, true); 
} 

這隻意味着您可以在不更改其他任何內容的情況下在不同的數據庫上運行測試。

編輯; woops;認爲這是一個公認的解決方案。如果你正在做一個測試,就這樣做吧:

[Test] 
    public void Test_can_store_and_get_objects() 
    { 
     var factory = CreateSessionFactory(); 
     using (var s = factory.OpenSession()) 
     { 
      ... 
     } 
    } 

    private static ISessionFactory CreateSessionFactory() 
    { 
     return Fluently.Configure().Database(SQLiteConfiguration.Standard.UsingFile("firstProject.db")) 
     .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Address>()) // <-- Refer to parent project 
     .ExposeConfiguration(SetupTestDatabase) 
     .BuildSessionFactory(); 
    }