在许多企业 asp.net mvc 3 Web 应用程序中共享通用 .net 类库的易于维护的方法是什么?

发布于 2024-12-27 11:27:52 字数 937 浏览 0 评论 0原文

我一直在努力以一种满足我所有要求的方式做到这一点。

以下是我们库中的内容:

  • 控制器和服务的基类
  • 业务对象(商店、部门等)
  • 公共部分视图(登录、错误等)
  • HttpApplication 的基类
  • 一般公共代码(读取 INI 文件、创建数据库) conn 等)

一直给我带来麻烦的一个要求如下:

  • 位于服务器上的一个位置。 (即 copy local = false)

这会中断,因为:

  • 包含 HttpApplication 类的 DLL 必须与要启动的 Web 应用程序 DLL 位于同一目录中。我还没有找到解决办法。我可以在每个应用程序中复制此代码,但我不愿意。
  • 如果我使用 Assembly.LoadFrom() 从共享位置加载 dll,共享视图将无法工作。 (我一直在使用 此方法预编译我的视图)
  • web.config 中的任何命名空间快捷方式都会在运行时因编译错误而中断,因为 web.config 在加载程序集之前已被解析。

我问你们的问题是,你们如何在类似的环境中处理通用代码?

GAC 似乎带来的麻烦大于其价值,我们希望所有应用程序都使用相同的代码,而不是在多个版本上拥有多个应用程序,并且必须维护所有这些。在这方面是否有可以指导我们的设计模式/最佳实践?

另外,作为奖励,如果您能够解决上述任何问题,那就太好了。

谢谢!

编辑:我想接下来的一个问题是我们是否应该在服务器上有一个包含公共 dll 的目录,或者它们是否应该只在项目部署/更新时部署?

I've been struggling to do this in a way that fulfills all of my requirements.

Here is what we have in our library:

  • Base classes for controllers and services
  • Business objects (stores, departments, etc)
  • Common Partial Views (Login, Error, etc)
  • Base class for HttpApplication
  • General common code (read an INI file, create a db conn, etc)

The one requirement that has been giving me trouble is as follows:

  • Lives in one place on a server. (i.e. copy local = false)

This breaks because:

  • The DLL containing the HttpApplication class must be in the same directory as the web apps dll to launch. I haven't found a way around that. I'm ok with duplicating this code in every app, but would rather not.
  • The shared views don't like to work if I use Assembly.LoadFrom() to load the dll from the shared location. (I've been using this method to precompile my views)
  • Any namespace shortcuts in web.config break at runtime with compilation errors because the web.config is parsed before the assembly is loaded.

My question to you folks is how do you handle your common code in a similar environment?

The GAC seems to be more trouble than its worth, and we want all of our apps to be using the same code, and not have multiple apps on multiple versions and have to maintain all of that. Are there design patters/best practices that can guide us in this regard?

Also, as a bonus, if you can solve any of the problems above, that would be great, too.

Thanks!

Edit: I guess a question that follows is whether or not we should even have a directory with the common dll(s) on the server, or if they should only be deployed as projects are deployed/updated?

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

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

发布评论

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

评论(2

记忆で 2025-01-03 11:27:52

首先,您需要区分出您想要实现的目标。不要创建一个包揽一切的库,否则你将得到一个大泥球。不要害怕创建几个可维护的库来实现您所追求的目标。是否有特定原因需要将其存储在一个位置?

例如,您提到的几个项目是 MVC 或 Web 特定的。如果您有可由 MVC 重用的项目,请创建一个类库,其中包含您继承的 MVC 基类并在项目中引用它们。尽可能使用单一责任原则。

关于您提到的其他项目,例如数据库连接,如果它是可重用的,请将其抽象到数据访问类库中并引用它。其他简单的操作,如读取 ini 文件或创建文件、创建另一个库并将其抽象为易于使用的方法。

我更喜欢将库 dll 复制到本地。您永远不知道什么时候需要对库进行更改,但您不希望所有项目都停止编译。当您准备好实现新版本的库时,复制 dll 并重新编译。

Firstly, you will want to separate out what you're trying to achieve. Don't create 1 library that does everything or you will have a Big Ball of Mud. Don't be afraid to create several maintainable libraries to achieve what you're after. Is there a specific reason it needs to be stored in one location?

For example, several of the items you mention are MVC or web specific. If you have items that can be reused by MVC, create a class library that contains MVC base classes you inherit and reference them in your project. Use the single responsibility principle as much as possible.

Regarding the other items you mentioned, like database connectivity, if it's reusable, abstract it out in a data access class library and reference it. Other simple operations like reading an ini file or creating a file, create another library and abstract it to easy to use methods.

I prefer to copy the library dlls locally. You never know when you will need to make changes to the library, but you don't want all of your projects to stop compiling. When you're ready to implement a new version of the library, copy the dll in and recompile.

锦欢 2025-01-03 11:27:52

不知道为什么大家都讨厌 gac。它是为了解决这个特定问题而设计的。将常用的 dll 安装到 gac 中,所有应用程序都可以看到它们。需要部署一个新的,只需将其重新安装在一处即可。

Not sure why all the hate towards the gac. It was designed to handle this specific problem. Install your common dlls to the gac and all apps can see them. Need to deploy a new one, just re-install it in one place.

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