简体   繁体   English

C#-使用枚举作为唯一标识符的替代方法

[英]C# - alternatives to using enums as unique identifiers

I believe this is a common situation but couldn't find a satisfactory answer with Googling. 我认为这是一种常见的情况,但无法通过Google搜索找到令人满意的答案。

Question: Is there a pattern that addresses looking up enum values from the database? 问题:是否有一种模式可以解决从数据库中查找枚举值的问题?

Situation: I have been tasked with refactoring a Winforms app that is getting really heavy on memory usage. 情况:我的任务是重构一个Winforms应用程序,该应用程序确实占用大量内存。 The main culprit is a "Constants" project - basically a bunch of Enum classes. 罪魁祸首是“常量”项目-基本上是一堆枚举类。 Their purpose is to duplicate database lookup table values (see example below). 它们的目的是复制数据库查找表值(请参见下面的示例)。

    public enum Status
    {
        None = -1,
        Active = 0,
        Completed = 1,
        Review = 2,
        Proceed = 3
    }

All of these enums have to be loaded in memory every single time the app loads. 每次应用加载时,所有这些枚举都必须加载到内存中。 Is there a pattern that allows you to fix this problem, but also doesn't kill the database with queries? 有没有一种模式可以让您解决此问题,但又不会通过查询杀死数据库?

While it's quite hard to believe that an assembly of enum s could indeed be the main source of high memory usage (I don't even want to imagine the SIZE of that assembly), some possible solutions flow into mind: 尽管很难相信enum的集合确实可以成为高内存使用率的主要来源(我什至不想想象该程序集的SIZE),但想到了一些可能的解决方案:

  1. Exploring your memory-greedy enum assembly for things other than enum s. 探索您的内存贪婪enum程序集,以查找除enum之外的其他内容。 Yes, it is a trivial advice, but it could prove the most reasonable after all. 是的,这是一个琐碎的建议,但毕竟可以证明是最合理的。
  2. Merging your enum assembly with main project. enum程序集与主项目合并。 While enum s by themselves are hardly memory consuming, associated oversized assembly could well be all by itself (because of heavy usage of attributes, for instance). 尽管enum本身几乎不占用内存,但关联的超大程序集很可能完全是单独的(例如,由于大量使用属性)。
  3. Implementing necessary values with a bunch of not-necessarily static classes, filled with a bunch of public static readonly int fields (not const s). 使用一堆不必要的static类来实现必要的值,这些类填充了一堆public static readonly int字段(不是const )。 Hardly will reduce memory usage if all of them are called at least once, but could lead to some reduction depending on user's actions. 如果全部调用至少一次,几乎不会减少内存使用量,但是根据用户的操作可能会导致内存使用量的减少。 However, those pesky int s are harder to account for then enum s. 但是,那些烦人的int很难解释为enum
  4. Implementing necessary values with cached singleton-ish pattern (perhaps there is a name for such a pattern, but I am not familiar with it). 用缓存的单例模式实现必要的值(也许有这样一种模式的名称,但我并不熟悉)。 While obviously harder on memory on a per-instance basis, it allows (while depending on GC) to be put into life and snuffed out of it based on usage. 虽然显然每个实例在内存上都比较困难,但它允许(取决于GC)投入使用并根据使用情况将其淘汰。 Basically, it should be something like 基本上应该是这样的

     internal abstract class ErzatsEnumBase { protected static readonly ObjectCache Cache = MemoryCache.Default; protected ErzatsEnumBase ( string CacheKey, CacheItemPolicy CachePolicy ) { Cache.Add ( CacheKey, this, CachePolicy, null ); } } public sealed class ErzatsEnum: ErzatsEnumBase { private static CacheItemPolicy policy = new CacheItemPolicy () { AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration, SlidingExpiration = new TimeSpan ( 0, 15, 0 ) }; private ErzatsEnum ( string CacheKey ) : base ( CacheKey, policy ) { } public ErzatsEnum Instance1 { get { ErzatsEnum result = ErzatsEnumBase.Cache.Get ( "Instance1" ); if ( result == null ) result = new ErzatsEnum ( "Instance1" ); return result; } } public ErzatsEnum Instance2 { get { ErzatsEnum result = ErzatsEnumBase.Cache.Get ( "Instance2" ); if ( result == null ) result = new ErzatsEnum ( "Instance2" ); return result; } } } 

Hope some of it proved useful :) 希望其中的一些有用:)

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

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