简体   繁体   English

适当的项目组织和架构,用于在Visual Studio / .NET项目中共享代码

[英]Proper project organization and architecture for sharing code in Visual Studio/.NET projects

I'm looking for recommendations on how to best share code between multiple Visual Studio projects. 我正在寻找有关如何在多个Visual Studio项目之间最佳共享代码的建议。 I'm struggling with a fundamental topic and trying to get some ideas to get over it. 我正在为一个基本主题而苦苦挣扎,并试图获得一些克服它的想法。

My solution has: 我的解决方案有:

  • Several web app projects 几个网络应用项目
  • Several standalone process projects, eg Windows Services and/or console apps and/or Azure WebJobs 几个独立的流程项目,例如Windows服务和/或控制台应用程序和/或Azure WebJobs

An example of functionality which is common to all projects is the need to call some common web service, or the need to read and write from Amazon S3, for example. 所有项目共有的功能示例是,例如,需要调用某些通用的Web服务,或者需要从Amazon S3进行读写。

Where I struggle is this: Obviously the code that implements the common functionality should be broken out on its own, for example in a separate class library project. 我要努力解决的问题是:显然,实现通用功能的代码应该自己分解,例如在单独的类库项目中。 To talk to S3 for example, the code needs to know my Amazon credentials, S3 endpoints, etc. - all these things would normally be stored in app configuration files. 例如,要与S3对话,代码需要知道我的Amazon凭证,S3终端节点等-所有这些内容通常都存储在应用程序配置文件中。 But I don't like the idea of putting config files in class library projects because it binds a particular implementation to them. 但是我不喜欢将配置文件放在类库项目中的想法,因为它会将特定的实现绑定到它们。 But in order to not do that, I have to pass in this information from the calling project. 但是为了不这样做,我必须从调用项目中传递此信息。 So for example the web app's web.config and the console app's app.config files contain this connection information. 因此,例如,Web应用程序的web.config和控制台应用程序的app.config文件包含此连接信息。 When calling the S3 code, I would assumingly pass this config info into the shared code. 调用S3代码时,我假设会将此配置信息传递到共享代码中。

However, this seems yucky* to me and I'm not sure why. 但是,这对我来说似乎令人讨厌*,我不确定为什么。 It still feels to me like I'm "binding" the S3 code (for example) to a particular config method, if that makes sense. 在我看来,我仍然感觉像是将S3代码(例如)“绑定”到特定的配置方法,如果那是有道理的。 I'm not sure if my feeling is a mis-formed bias. 我不确定我的感觉是否是一种错误的偏见。

*For example, I may have an arbitrary amount of configuration data that would have to be passed in: *例如,我可能要传递任意数量的配置数据:

  • Connection strings 连接字符串
  • Credentials for web services Web服务凭证
  • API endpoints API端点
  • Arbitrary data from my app's config settings (which some of the callers will need, but others won't, so lots of times the data will just be useless yet I have to do the work of passing something in) 应用程序的配置设置中的任意数据(某些调用者会需要,但其他调用者则不需要,因此很多时候这些数据将变得毫无用处,但我必须做一些传入的工作)

So every time I added a config variable in my main app, I'd have to modify the constructor of the common code. 因此,每次我在主应用程序中添加一个config变量时,都必须修改通用代码的构造函数。 Things would be in constant motion. 事物会不断地运动。

Can you give me suggestions on this? 你能给我建议吗?

I like to use Configuration objects as parameters, that way the signature never changes, even if you add/remove properties. 我喜欢使用Configuration对象作为参数,这样即使您添加/删除属性,签名也不会改变。 For instance.... 例如....

public class AmazonConfigSettings {
    public string AWSkey { get; set; }
    public string ApiEndpoint { get; set; }
    ......
}

Your signature could then always look like: 这样,您的签名就始终可以像:

public MySharedClass(AmazonConfigSettings config) { .... } 

Even if (when) Amazon overhauls their webservice settings completely. 即使(完全)Amazon彻底检查了其网络服务设置。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM