创建C# Winform开发生产环境

发布于 2024-08-02 22:55:10 字数 279 浏览 3 评论 0原文

我想知道为我的 C# winforms 项目正确创建开发和生产环境的最佳方法是什么。不幸的是没有开发环境。相反,每次我希望创建生产或开发版本时,我都必须指定发布路径。此外,由于每个产品或开发版本都使用不同的连接字符串,因此我也必须进入代码并更改它。

编辑我想补充的另一件事是,可以说“测试人员”将从本地 .exe 运行该程序,该程序将查看源文件并检测是否需要更新被制作。我提到这一点的唯一原因是测试人员不会在“调试”模式下运行代码。

有什么想法吗?

提前致谢!

I am wondering what is the best way to properly create a Development and Production environment for my C# winforms project. The unfortunate thing is that there is no development environment. Rather, I must specify the publish path each time i wish to create either the Production or Development build. Also since each prod or dev build uses a different connection string I must then go into the code and change that as well.

EDIT Another thing i would like to add is the fact that the 'testers' so to speak will be running the program from a local .exe which will look at the source files and detect if an update needs to be made. The only reason i am mentioning this is because the testers will not be running the code in 'DEBUG' mode.

Any ideas?

Thanks in advance!

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

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

发布评论

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

评论(4

玩物 2024-08-09 22:55:10

更新

唷,2009 年确实是很久以前的事了;)我将把最初的答案留给后人,但是,正如 Eric J. 已在 2012 年帖子的更新版本中指出 Slowcheetah 是解决此问题的一种更灵活的方法,因为它允许您为每个配置创建多个配置文件。

原始答案

我无法回答发布部分,因为我不确定是否可以完成。但是,对于连接字符串部分,您可以创建一个“连接字符串提供程序类”,如果您处于开发阶段,它将返回开发连接字符串;如果您处于生产阶段,它将返回生产连接字符串。

我不知道您是否在配置管理器中使用“调试”和“发布”编译模式,但可以将其与预编译器指令一起使用:

public class ConnectionStringProvider
{
  public static string GetConnectionString()
  {
    #if (DEBUG)
      return DevConnectionString;
    #else
      return ProdConnectionString;
    #endif
  }
}

如果您使用 VS2005 设置窗格(在项目设置中),您可以在那里设置 2 个连接字符串并使用它来代替常量值。如果您使用任何 .Net 数据源,则可以始终使用 DEV 连接字符串来创建对象。

另外,连接字符串提供程序可以使用单例模式,但我不确定是否会有真正的好处。

UPDATE

Whew, 2009 sure was a long time ago ;) I'm going to leave the original answer for posterity, but, as Eric J. already stated in an updated version of his post in 2012, Slowcheetah is a much more flexible approach to this problem as it let's you create multiple config file per configuration.

Original answer

I can't answer for the publish part as I'm not sure it can be done. However, for the connection string part, you could make a "Connection String Provider Class" which would return the development connection string if you are in development, or the production connection string if you are in production.

I don't know if you are using Debug and Release compilation mode in the configuration manager, but it could be possible to use that with a pre-compiler directive:

public class ConnectionStringProvider
{
  public static string GetConnectionString()
  {
    #if (DEBUG)
      return DevConnectionString;
    #else
      return ProdConnectionString;
    #endif
  }
}

If you are using the VS2005 Settings pane (in the project settings), you can set 2 connections strings in there and use that instead of a constant value. If you are using any of the .Net DataSource, you could be always using the DEV connection string to create your object.

Also, the connection string provider could use a Singleton pattern, but I'm not sure there would be a real benefit.

倾城月光淡如水﹏ 2024-08-09 22:55:10

我不确定你的意思

不幸的是,有
没有开发环境。

但是,我想指出你可以处理类似的事情

此外,由于每个产品或开发版本都使用
我必须使用不同的连接字符串
然后进入代码并更改它
也是如此。

带有条件编译标志。例如,您可以这样:

#if DEBUG
connString = "ABC";
#else
connString = "DEF";
#endif

DEBUG 可能不是适合您的环境的标志。您可以定义自己的标志,例如 PROD 或 DEV,并指定是否为特定构建类型定义它们,或者如果在 IDE 外部构建,则从命令行定义它们。

更新

自从最初发布此答案以来,SlowCheetah< /a> 已可用。它允许每个构建配置对 app.config/web.config(以及任何其他 XML 文件,例如 NLog 配置文件)进行 XML 转换。因此,如果您有 QA 构建配置,则可以对 QA 进行 XML 转换,为 QA 环境设置适当的连接字符串。

这比在源代码中放置配置字符串和类似内容要

I'm not sure what you mean by

The unfortunate thing is that there is
no development environment.

However, I would point out that you can handle things like

Also since each prod or dev build uses
a different connection string I must
then go into the code and change that
as well.

with conditional compilation flags. For example you could so something like:

#if DEBUG
connString = "ABC";
#else
connString = "DEF";
#endif

DEBUG may not be the appropriate flag for your environment. You can define your own flags such as PROD or DEV and specify whether to define them for a particular build type, or from the command line if building outside of the IDE.

UPDATE

Since originally posting this answer, SlowCheetah has become available. It allows for XML transformations for your app.config/web.config (and for any other XML file such as NLog configuration files) per build configuration. So, if you have say a QA build configuration, you can have an XML transformation for QA that sets e.g. an appropriate connection string for the QA environment.

This is much preferred to placing configuration strings and similar in source code.

命硬 2024-08-09 22:55:10

您可以从配置文件中读取连接字符串。

然后,您将在 PROD 和 DEV 中使用相同的程序文件,只是不同的配置文件。

You could read the connection string from a configuration file.

You would then use the same program file in PROD and DEV, just a different configuration file.

阿楠 2024-08-09 22:55:10

使用 Virtual PC 创建单独的环境怎么样?现在这是最佳实践。

What about using Virtual PC to create a separate environment ? That's now a best-practice.

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