实际上,WebService 方法(在其他答案中提到)意味着您将 NHibernate 及其逻辑移至 Web 服务。然后,WebService 使用 WebService 的方法向应用程序公开可用的 db 功能。
数据库实际上只有一个用户,WebService 使用的用户,如果您希望应用程序用户拥有不同的数据库权限,您可以从 WebService 层抽象它
最后,WinForms 应用程序只知道通过 WebService 的方法请求数据的 WebService 的位置,您可以在这两个端点之间应用任何所需的安全措施。
对于离线功能,一切都归结为以安全的方式将数据持久保存到本地存储并通过 WebService 提供同步方法
我实际上是使用与 DB 通信的 web 服务和仅与 web 服务通信的 WinForm 应用程序 (.NET Compact Framework) 完成此操作的,在没有蜂窝网络覆盖的情况下,它会将更改序列化到存储卡 (数据并不重要,因此对于我的案例而言,未采取晦涩/淫秽的安全措施)
按要求更新一个小例子(尽管要求提供一个例子,但我确实觉得很奇怪)
您已经在 ASP.NET WebService Application 类型的项目中设置了域类和休眠配置以及(例如)您的存储库内容。为简单起见,我将只有一个 Web 服务类 Foo(在 Foo.asmx.cs 中)和一个 Bar 域类
所以你得到了这个(实际实现会有所不同):
namespace FWS
{
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class FooService : WebService
{
private readonly ILog errorLogger = LogManager.GetLogger("ErrorRollingLogFileAppender");
private readonly IDaoFactory daoFactory = new DaoFactory();
private readonly ISession nhSession = HibernateSessionManager.Instance.GetSession();
}
[WebMethod]
public Bar[] GetFavoriteBars(string someParam, int? onceMore){
return daoFactory.GetBarDao().GetFavoriteBars(someParam, onceMore); //returns a Bar[]
}
}
我们将 dabehaviour 抽象化,或者直接使用 nhsession,暴露为 webmethod。
现在从 WinForm 应用程序中,您需要做的就是Add a WebReference,它对配置进行所有必要的更改,但也会生成所有必要的类(在此示例中,它将创建一个 Bar 类,因为 Web 服务会公开它)。
namespace WinFormK
{
public class KForm(): System.Windows.Forms.Form
{
public void Do()
{
var service = new FWS.FooService();
string filePath = "C:\\temp\FooData.xml";
Bar[] fetched = service.GetFavoriteBars("yes!", null);
//lets write this to local storage
var frosties = new XmlSerializer(typeof(Bar));
TextReader reader = new StreamReader(filePath);
try
{
var persisted = (T)frosties.Deserialize(reader);
}
catch(InvalidOperationException)
{
//spock, do something
}
finally
{
reader.Close();
reader.Dispose();
}
}
}
}
有些事情你需要注意: