简体   繁体   English

流利的NHibernate解决方案结构-如何在Web App / Console App后端引用一次DLL

[英]Fluent NHibernate Solution Structure - How to reference DLL's once in back end of Web App / Console App

I'm organizing a VisualStudio 2008 solution using Fluent NHibernate and I would like to keep all the NHibernate dll dependencies buried in a "Back End" class library and let the front end Web app or Console app be ignorant of NHibernate. 我正在组织使用Fluent NHibernate的VisualStudio 2008解决方案,并且我想将所有NHibernate dll依赖项都埋在“后端”类库中,并使前端Web应用程序或控制台应用程序对NHibernate不了解。

My Solution structure is as follows: 我的解决方案结构如下:

BackEnd -- Class Library -- Business logic and Data Repositories & public API.. this level uses NHibernate privately for data storage and does not expose any NHibernate classes to the front end 后端 -类库-业务逻辑和数据存储库以及公共API ..此级别将NHibernate私有用于数据存储,并且不向前端公开任何NHibernate类

Public -- Class Library --- POCO Objects with no dependencies used by both front end and back end. 公共 -类库---前端和后端均不使用任何依赖项的POCO对象。 Backend uses NHibernate to persist these objects. 后端使用NHibernate持久化这些对象。

Front End -- Console App & a MVC Web App -- (Two front end apps (1) MVC2 Web app && (2) a console app) reference the Public & Back End projects and just use a few public methods on to interact with the Back End using Public objects. 前端 -控制台应用程序和MVC Web应用程序-(两个前端应用程序(1)MVC2 Web应用程序&&(2)控制台应用程序)引用Public&Back End项目,并且仅使用一些公共方法与之交互使用Public对象的后端。

What I would like to do is only reference NHibernate and its many dependencies once in Back End and have the Front End apps just reference the back end project. 我想做的只是在后端引用NHibernate及其许多依赖项,让前端应用程序仅引用后端项目。 However, the Fluent BackEnd crashes at runtime if I don't reference every one of Nhibernate's dependencies in my front end app in addition to referencing them in the Back End. 但是,如果除了在后端中引用它们之外,如果我没有在前端应用程序中引用Nhibernate的每个依赖项,Fluent BackEnd就会在运行时崩溃。 Here's the Back End code it crashes on complaining it can't find the nhibernate dlls when the front end omits the reference: 这是后端代码,当前端省略引用时,它抱怨抱怨找不到nhibernate dll而崩溃:

   public static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
         .Database(SQLiteConfiguration.Standard.UsingFile(DbFile))
         .Mappings(m => m.AutoMappings
               .Add(AutoMap.AssemblyOf<EngineInfo>(type => type.Namespace.Contains("BackEnd"))
               .Conventions.Add(DefaultCascade.All())
               .Conventions.Add(DefaultLazy.Never())
               )
             ) //emd mappings
       .ExposeConfiguration(BuildSchema)//Delete and remake a fresh tmp db 
       .BuildSessionFactory();//finalizes the whole thing to send back. 


    }

It may be that I've accidentally exposed a NHibernate dependent resource to the Front End apps but I don't get any compile time errors, and I'd love a little direction. 可能是我不小心将NHibernate依赖的资源公开给了前端应用程序,但是我没有得到任何编译时错误,所以我希望有一点指导。

If I do reference Nhibernate's dlls everything runs fine but in my front end apps I get the following warning at compile time: 如果我确实引用了Nhibernate的dll,那么一切运行正常,但是在前端应用程序中,我在编译时收到以下警告:

* *

Found conflicts between different versions of the same dependent assembly. 发现相同从属程序集的不同版本之间存在冲突。

* *

Any suggestions? 有什么建议么?

--Update-- -更新-

I've narrowed the dlls that I have to reference in the front end down to NHibernate.ByteCode.Castle.dll and System.Data.SQLite I can skip all the others and not get runtime errors.. at least none yet. 我将前端必须引用的dll缩小为NHibernate.ByteCode.Castle.dll和System.Data.SQLite,我可以跳过所有其他的DLL,并且不会遇到运行时错误..至少没有。

I'm still not sure why either would be required because the front end never uses any NH or SQL lite resources except through the public interface of the back end which doesn't offer any Nhibernate or SQLite resources. 我仍然不确定为什么要这样做,因为除了通过后端的公共接口(不提供任何Nhibernate或SQLite资源)外,前端从未使用过任何NH或SQL lite资源。 All classes that touch NHibernate or SQLite are marked Internal so I'm not sure what could cause this dependency. 所有接触NHibernate或SQLite的类都标记为Internal,所以我不确定是什么导致这种依赖。

The visual studio dependency warning offers a solution that I'm a bit hesitant to apply since I don't know why the problem came up in the first place: Visual Studio依赖项警告提供了一个我有点犹豫的解决方案,因为我不知道为什么问题会首先出现:

Do you want to fix these conflicts by adding binding redirect records to the appconfig file? 您是否想通过将绑定重定向记录添加到appconfig文件来解决这些冲突? --- MSDN on the option -- http://msdn.microsoft.com/en-us/library/bb383993.aspx --- MSDN上的选项-http://msdn.microsoft.com/zh-cn/library/bb383993.aspx

Any advice? 有什么建议吗?

---Update--- Tom below seems to have replicated my problem so this is starting to sound like a NHibernate dll bug. ---更新---下面的汤姆似乎已经复制了我的问题,所以这听起来像是NHibernate dll错误。 I also tried upgrading from VS 2008 to 2010 and rebuilt the whole solution and replicated the error there. 我还尝试了从VS 2008升级到2010,并重建了整个解决方案,并在那里复制了错误。 Any one want to take a look to make sure we're not missing something before I try to report it? 有人想看看以确保我们在举报之前没有丢失任何东西吗?

I'm working on a project with a similar architecture, and ran into similar problems just yesterday - needed to include references for NHibernate in my project, even though there no direct references in the code to those assemblies. 我正在开发一个具有类似体系结构的项目,直到昨天遇到了类似的问题-即使我的项目中没有直接引用这些程序集的代码,也需要在我的项目中包括NHibernate的引用。

Otherwise, got a runtime error, complaining about not being able to find NHibernate.ByteCode.Castle. 否则,出现运行时错误,抱怨找不到NHibernate.ByteCode.Castle。

The problem turned out to be the way I was building. 问题原来是我构建的方式。 The NHibernate assemblies were not being properly propogated to my application's runtime directory, because I was using the standard VS2008 folder structure (bin\\Debug, etc). NHibernate程序集未正确传播到应用程序的运行时目录,因为我使用的是标准VS2008文件夹结构(bin \\ Debug等)。

The first part of the fix was to create a single directory where all the assemblies are built and run. 修复程序的第一部分是创建一个目录,在其中构建并运行所有程序集。 You can do this by setting the Output path (on the project's Build tab) for all the projects in your solution to point to this directory. 您可以通过将解决方案中所有项目的“输出”路径(在项目的“构建”选项卡上)设置为指向此目录来执行此操作。

The second part of the fix was to set Copy Local = True for all the NHIbernate refs, and also the other assemblies in your solution (you don't need to set this for the standard assemblies - System, etc). 修复的第二部分是为所有NHIbernate引用以及解决方案中的其他程序集设置Copy Local = True(您无需为标准程序集-System等设置此值)。 This causes VS to copy all the assemblies used by a project to the common runtime directory everytime you build. 这使VS每次构建时都将项目使用的所有程序集复制到公共运行时目录。

You should then be able to eliminate the NHibernate references from all the projects that don't use them directly. 然后,您应该能够从所有不直接使用NHibernate的项目中删除它们。

This worked for me, but I can't help wondering if there's a better way to solve this problem. 这对我有用,但是我不禁想知道是否有更好的方法来解决此问题。 But it will do for now. 但这现在就可以了。

Edit: 编辑:

After some more testing, it appears that there is a problem with all the Castle DLLs, NHibernate.ByteCode.Castle most notably. 经过更多测试后,似乎所有Castle DLL NHibernate.ByteCode.Castle都有问题。

One of the project's in my application is an assembly that contains the NHibernate logic, which contains references to all the NHibernate DLLs, including NHibernate.ByteCode.Castle. 我的应用程序中的项目之一是一个包含NHibernate逻辑的程序集,该程序集包含对所有NHibernate DLL(包括NHibernate.ByteCode.Castle)的引用。

When I add a reference to that assembly in another project, and build it, all the NHibernate DLLs get copied to the other project's Output directory - except the Castle DLLs! 当我在另一个项目中添加对该程序集的引用并进行构建时,所有NHibernate DLL都会复制到另一个项目的Output目录中-Castle DLL除外! .

Other people have had similar problems I tried some of the workarounds suggested, with no luck. 其他人也遇到过类似的问题,我尝试了一些建议的解决方法,但是没有运气。

I strongly suspect that the Castle DLLs don't properly support Microsoft's conventions for propagating dependant assemblies to other projects, but don't know enough to be sure. 我强烈怀疑Castle DLL不能正确支持Microsoft的有关将依赖程序集传播到其他项目的约定,但还不足以确保这一点。

Some of the NHibernate related dlls are probably referenced in the web project instead of the backend project. 某些与NHibernate相关的dll可能在Web项目中而不是在后端项目中被引用。 I you really can't find it, remove all NHibernate related dlls, clean your solution and just add them to the backend project. 我真的找不到它,删除所有与NHibernate相关的dll,清理您的解决方案,然后将它们添加到后端项目中。

I worked this issue around. 我解决了这个问题。 In my DataAccess project (where NHibernate and NHibernate.ByteCode.Castle references are) I created this simple class: 在我的DataAccess项目(NHibernate和NHibernate.ByteCode.Castle引用所在的地方)中,我创建了以下简单类:

/// <summary>
/// This class is only to fix building issue of NHibernate.ByteCode.Castle.dll 
/// not being copied to bin directory of projects referencing data access 
/// (either directly or indirectly)
/// </summary>
internal abstract class ReferenceHelper : ProxyFactory
{
}

I never use this class (obviously), but it's only existence helped NHibernate.ByteCode.Castle.dll (and Castle.Core.dll) to appear in referencing project's bin/ directory. 我从未使用过此类(显然),但是它的存在是唯一的存在使NHibernate.ByteCode.Castle.dll(和Castle.Core.dll)出现在引用项目的bin /目录中。 In the solution structure I have separate directory where I placed all related dlls (NHibernate, Castle, Fluent, etc.), so no GAC here. 在解决方案结构中,我有单独的目录,在其中放置了所有相关的dll(NHibernate,Castle,Fluent等),因此这里没有GAC。

您需要热切加载或NHibernateUtils。在通过网络发送它们之前初始化您的对象,我怀疑是城堡代理包装使您感到悲伤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Web或控制台应用程序,如何获取当前DLL的应用程序上下文 - Web or console app, how to get the application context of the current DLL 添加对控制台应用程序的 Web 服务引用 - add a web service reference to a console app 如何修复Windows应用商店解决方案中缺少的程序集参考&#39;Microsoft.Xaml.Interactivity.dll&#39;? - How To Fix Missing Assembly Reference 'Microsoft.Xaml.Interactivity.dll' in Windows Store App Solution? 网站引用了一个控制台应用程序,该应用程序使用NHibernate与数据库对话 - Web site references a console app which uses NHibernate to talk to database 如何使用Fluent NHibernate映射CompositeId的引用 - How to map a reference of a CompositeId using Fluent NHibernate 如何使用流利的NHibernate创建参考表 - How to create reference tables using fluent nhibernate 如何在流利的nhibernate中添加引用条件? - How to add a condition for a reference in fluent nhibernate? 流利的NHibernate映射参考 - Fluent NHibernate Mapping Reference 流利的nHibernate ID是参考 - Fluent nHibernate Id is a reference 在控制台应用程序中引用Web应用程序时出现“ System.IO.FileLoadException” - 'System.IO.FileLoadException' when reference a web app in a console app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM