C# 设计一个单独的应用程序设置?
我正在设计一个在两个第三方服务之间同步数据的应用程序。在其中一个服务中,我在应用程序启动(或池刷新)时获取了各种ID,并将其放入具有静态属性的结构中 比如说C# 设计一个单独的应用程序设置?,c#,oop,singleton,class-design,C#,Oop,Singleton,Class Design,我正在设计一个在两个第三方服务之间同步数据的应用程序。在其中一个服务中,我在应用程序启动(或池刷新)时获取了各种ID,并将其放入具有静态属性的结构中 比如说 public struct SfRecordTypes { public static string Order = "REMOVED"; public static string Issue = "REMOVED"; } public struct SfQueues { public static string
public struct SfRecordTypes
{
public static string Order = "REMOVED";
public static string Issue = "REMOVED";
}
public struct SfQueues
{
public static string Billing = "REMOVED";
public static string Support = "REMOVED";
public static string SupportDev = "REMOVED";
public static string Field = "REMOVED";
}
在启动时,这些被称为
public static void PopulateRecordTypes()
{
var sf = new SalesForce();
var recordTypes = sf.GetRecordTypes();
if (recordTypes == null) return;
SfRecordTypes.Order = recordTypes.First(n => n.Name != null && n.Name.Contains("Order")).Id;
SfRecordTypes.Issue = recordTypes.First(n => n.Name != null && n.Name.Contains("Issue")).Id;
sf.Dispose();
}
public static void PopulateQueueIDs()
{
var sf = new SalesForce();
var groups = sf.GetAllGroups();
if (groups == null) return;
SfQueues.Billing = groups.First(n => n.Name != null && n.Name.Contains("Billing Queue")).Id;
SfQueues.Support = groups.First(n => n.Name != null && n.Name.Contains("Support Queue")).Id;
SfQueues.SupportDev = groups.First(n => n.Name != null && n.Name.Contains("Support Developer Queue")).Id;
SfQueues.Field = groups.First(n => n.Name != null && n.Name.Contains("Field Services Queue")).Id;
sf.Dispose();
}
然而,我正试图找出如何设计/实现一个我可以调用的单例类,该类将自动填充这些值(以使其只读的方式),以便将来使用它们时,它们遵循适当的OOP原则,并且更干净。我对单例有点陌生,我的研究结果喜忧参半。从静态字段到单例是一个开始,但为什么要将其变成单例呢?你为什么要把它当作一个全球性的国家?提示:您有一个返回
void
的方法。。。为什么不返回一个填充了结果的对象呢?这些调用可能有点昂贵,通常需要几秒钟。我基本上希望它们在第一次使用时被初始化,并一直保持到循环发生。当它们在global.asax
中填充应用程序启动时的字段时,立即标记为void。通过使用单例,我可以稍微远离这些静态类,或者可以使用依赖项注入,在启动时初始化值,但确保在需要的任何地方都提供相同的对象。然后,您仍然可以使用不同的值轻松地进行测试。我真的会尽量避免使用单例…我已经使用了一些单例(假设它们是cookie-cuter),它们保留了大量用户、组织等自定义缓存对象的列表。为什么避免单身会有好处?全球状况很难解释,也很难检验。我建议你研究一下这一点——搜索“为什么单身汉是邪恶的”会给出很多答案:)