配置我应该使用哪一个?来自 Fluent 教程还是来自 Cookbook 3.0?

发布于 2024-10-11 15:14:06 字数 2584 浏览 2 评论 0原文

我正在阅读 Nhibernate Cookbook 3.0 和 Fluent 教程,我有点困惑我应该使用哪一个(Cookbook 本身有很多不同的方式)

Fluent Nhibernate Tutorial

private static ISessionFactory CreateSessionFactory()
{
  return Fluently.Configure()
    .Database(
      SQLiteConfiguration.Standard
        .UsingFile("firstProject.db")
    )
    .Mappings(m =>
      m.FluentMappings.AddFromAssemblyOf<Program>())
    .ExposeConfiguration(BuildSchema)
    .BuildSessionFactory();
}

private static void BuildSchema(Configuration config)
{
  // delete the existing db on each run
  if (File.Exists(DbFile))
    File.Delete(DbFile);

  // this NHibernate tool takes a configuration (with mapping info in)
  // and exports a database schema from it
  new SchemaExport(config)
    .Create(false, true);
}

Cookbook 3.0 pg(76) Web 请求

1.  In the hibernate-configuration section of web.config, add the current_
session_context_class property with a value of web.

2.  If it doesn't exist already, add a new Global application class (Global.asax).

3.  In Global.asax, add these using statements.

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Context;

4.  Create a static property named SessionFactory.

public static ISessionFactory SessionFactory { get; 
private set; }

5.  In the Application_Start method, add the following code.

protected void Application_Start(object sender, EventArgs e)
{
  log4net.Config.XmlConfigurator.Configure();
  var nhConfig = new Configuration().Configure();
  SessionFactory = nhConfig.BuildSessionFactory();
}
6.  In the Application_BeginRequest method, add the following code.
protected void Application_BeginRequest(object sender, EventArgs e)
{
  var session = SessionFactory.OpenSession();
  CurrentSessionContext.Bind(session);
}

7.  In the Application_EndRequest method, add the following code:
protected void Application_EndRequest(object sender, EventArgs e)
{
  var session = CurrentSessionContext.Unbind(SessionFactory);
  session.Dispose();
}

然后他们只是用它来运行它。

Guid productId = new Guid(Request["id"]);
Eg.Core.Product product;
var session = Global.SessionFactory.GetCurrentSession();
using (var tran = session.BeginTransaction())
{
  product = session.Get<Eg.Core.Product>(productId);
  tran.Commit();
}
Page.Title = product.Name;
Label1.Text = product.Name;
Label2.Text = product.Description;

通过 Fluent 教程,我也有点困惑我通常会将代码放在 asp.net mvc 应用程序中的位置。我正在尝试将存储库模式与 ninject(DI 注入)一起使用。

因此,对于这两种方式,我不确定如何使其与 ninject 和存储库模式一起工作。

对于存储库模式和 Di 来说,这两种方法更好吗?

I am reading nhibernate cookbook 3.0 and the fluent tutorial and I am kinda confused which one I should be using(cookbook by itself has many different ways)

Fluent Nhibernate tutorial

private static ISessionFactory CreateSessionFactory()
{
  return Fluently.Configure()
    .Database(
      SQLiteConfiguration.Standard
        .UsingFile("firstProject.db")
    )
    .Mappings(m =>
      m.FluentMappings.AddFromAssemblyOf<Program>())
    .ExposeConfiguration(BuildSchema)
    .BuildSessionFactory();
}

private static void BuildSchema(Configuration config)
{
  // delete the existing db on each run
  if (File.Exists(DbFile))
    File.Delete(DbFile);

  // this NHibernate tool takes a configuration (with mapping info in)
  // and exports a database schema from it
  new SchemaExport(config)
    .Create(false, true);
}

cookbook 3.0 pg(76) web request

1.  In the hibernate-configuration section of web.config, add the current_
session_context_class property with a value of web.

2.  If it doesn't exist already, add a new Global application class (Global.asax).

3.  In Global.asax, add these using statements.

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Context;

4.  Create a static property named SessionFactory.

public static ISessionFactory SessionFactory { get; 
private set; }

5.  In the Application_Start method, add the following code.

protected void Application_Start(object sender, EventArgs e)
{
  log4net.Config.XmlConfigurator.Configure();
  var nhConfig = new Configuration().Configure();
  SessionFactory = nhConfig.BuildSessionFactory();
}
6.  In the Application_BeginRequest method, add the following code.
protected void Application_BeginRequest(object sender, EventArgs e)
{
  var session = SessionFactory.OpenSession();
  CurrentSessionContext.Bind(session);
}

7.  In the Application_EndRequest method, add the following code:
protected void Application_EndRequest(object sender, EventArgs e)
{
  var session = CurrentSessionContext.Unbind(SessionFactory);
  session.Dispose();
}

Then they just use this to run it.

Guid productId = new Guid(Request["id"]);
Eg.Core.Product product;
var session = Global.SessionFactory.GetCurrentSession();
using (var tran = session.BeginTransaction())
{
  product = session.Get<Eg.Core.Product>(productId);
  tran.Commit();
}
Page.Title = product.Name;
Label1.Text = product.Name;
Label2.Text = product.Description;

With the fluent tutorial I am also kinda confused where I would typically put that code in an asp.net mvc application. I am trying to use the repository pattern with ninject(DI injection).

So with both ways I am not sure how to make it work with ninject and the repository pattern.

Is either way better for the repository pattern and Di?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掌心的温暖 2024-10-18 15:14:06

您是否尝试下载整个解决方案来运行该项目?这些只是代码示例,您需要整个设置:带有实体类、映射、存储库等的 VS 项目。

我会转到 http://www.sharparchitecture.net/https://github.com/sharparchitecture 下载他们的 Northwind 示例项目具有您所需的确切设置。您必须找到 Northwind 数据库并将其安装在本地计算机上,然后修改 NHibernate.config 以指向您的数据库。

Did you try to download the whole solution to run the project? These are just samples of code and you need the whole setup: VS project with Entity classes, mappings, repositories etc.

I would go to http://www.sharparchitecture.net/ and https://github.com/sharparchitecture download their Northwind sample project that has the exact setup that you need. You will have to find the Northwind database and install it on you local machine then modify the NHibernate.config to point to your database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文