首页 \ 问答 \ 使用和持久化枚举的最佳做法(Best practices for using and persisting enums)

使用和持久化枚举的最佳做法(Best practices for using and persisting enums)

我在这里已经看到了关于处理和保持类枚举值的最佳方式的几个问题/讨论(例如, 适用于枚举的持久化数据 , 如何使用NHibernate持久化一个枚举 ),而我想问一般的consenus是什么。

尤其是:

  • 这些值应该如何在代码中处理?
  • 他们应如何坚持到数据库(如文本/数字)?
  • 什么是不同解决方案的权衡?

注意:我把原来包含在这个问题中的解释移到了一个答案。


I've seen several questions/discussions here about the best way to handle and persist enum-like values (e.g. Persisting data suited for enums , How to persist an enum using NHibernate ), and I'd like to ask what the general consenus is.

In particular:

  • How should these values be handled in the code?
  • How should they be persisted to a database (as text/as a number)?
  • What are the tradeoffs of different solutions?

Note: I moved the explanations originally included in this question to an answer.


原文:https://stackoverflow.com/questions/746812
更新时间:2023-11-19 10:11

最满意答案

第一:
硬限制取决于浏览器,而不是画布API。
即便如此,浏览器总是试图提高性能,所以这个数字总是在变化。
但使用WebGL和Canvas制作游戏时,纹理地图集/精灵地图集是HUGE .jpg / .png文件。
机会非常非常好,您的图像更小,我经常在画布上使用4MB / 5MB / 16MB图像进行演示。
一个巨大的图像(或几十个)可能会使标签崩溃,如果足够大,但直到那个时候,画布并没有真正向我抱怨。

第二:
有安全限制。
canvas编辑照片取决于您使用的浏览器,以及该文件是否与您的网站位于同一个域中。

第三:
当你说“大文件不起作用,但他们有时......”
...这让我相信你的图像加载方法是错误的。

如果你做这样的事情:

var canvas = document.createElement("canvas"),
    context = canvas.getContext("2d"),
    img = new Image();

img.src = "//mydomain.com/myimg.png";
context.drawImage(img, 0, 0, img.width, img.height);

...或其他任何不是基于事件或基于回调的,
那么你的问题与canvas没有任何关系,并且与回调有关,并且你正试图在图像加载之前将图像绘制到canvas上。

如果您的浏览器已经缓存了大图片的副本,或者小图片只需要几分之一秒的时间下载,那么没有任何问题。

如果您尝试下载18MB图像,并在为图像设置url后立即将其绘制到画布上,那么您将会看到空白屏幕,因为它尚未完成加载。

相反,您需要等待图像完成加载, 然后在准备就绪时将其绘制到画布上。

var canvas = document.createElement("canvas"),
    context = canvas.getContext("2d"),
    image = new Image();

image.onload = function () {
    var img = this,
        width = img.width,
        height = img.height;

    canvas.width = width;
    canvas.height = height;
    context.drawImage(img, 0, 0, width, height);

    document.body.appendChild(canvas);
};

image.src = "//mydomain.com/path-to-really-huge-image.png";

现在它可能是一张1600万像素的图像。 在文件加载完成之前什么都不会发生。
然后, onload事件触发,并执行其余的设置。

你可以把它变得更抽象,并把它变成一个漂亮的程序,但感觉这是你可能错过的关键。


First:
Hard limitations would depend on the browser, not the canvas API.
Even then, browsers are always trying to improve that performance, so that number would always be changing.
But with WebGL and Canvas being used to make games, texture atlases / sprite atlases are HUGE .jpg/.png files.
Chances are very, very good that your images are smaller, and I've frequently used 4MB/5MB/16MB images in canvas for demonstrations.
A huge image (or dozens of them) might crash the tab, if it's big enough, but until that time, canvas hasn't really complained to me.

Second:
There are security-limitations.
Editing photos in canvas comes down to what browser you're on, and whether the file is on the same domain as your website or not.

Third:
When you say that "large files don't work, but they do sometimes..."
...that leads me to believe that your image-loading method is faulty.

If you do something like this:

var canvas = document.createElement("canvas"),
    context = canvas.getContext("2d"),
    img = new Image();

img.src = "//mydomain.com/myimg.png";
context.drawImage(img, 0, 0, img.width, img.height);

...or anything else which isn't either event-based or callback-based,
then your problem has nothing to do with canvas and has everything to do with callbacks, and that you're trying to draw the image to the canvas before the image is done loading.

If your browser has already cached a copy of the large image, or if a small image only takes a fraction of a second to download, then there's no problem.

If you try downloading an 18MB image, and draw it to the canvas as soon as you set the url for the image, then you're going to get a blank screen, because it hasn't finished loading.

Instead, you need to wait for the image to finish loading, and then draw it to the canvas when it's ready.

var canvas = document.createElement("canvas"),
    context = canvas.getContext("2d"),
    image = new Image();

image.onload = function () {
    var img = this,
        width = img.width,
        height = img.height;

    canvas.width = width;
    canvas.height = height;
    context.drawImage(img, 0, 0, width, height);

    document.body.appendChild(canvas);
};

image.src = "//mydomain.com/path-to-really-huge-image.png";

Now it could be a 16MP image. Nothing will happen until the file is done loading.
Then, the onload event fires, and does the rest of the setup.

You could make it more abstract, and turn it into a nifty program, but it feels like this is the key piece you might be missing.

相关问答

更多
  • 第一: 硬限制取决于浏览器,而不是画布API。 即便如此,浏览器总是试图提高性能,所以这个数字总是在变化。 但使用WebGL和Canvas制作游戏时,纹理地图集/精灵地图集是HUGE .jpg / .png文件。 机会非常非常好,您的图像更小,我经常在画布上使用4MB / 5MB / 16MB图像进行演示。 一个巨大的图像(或几十个)可能会使标签崩溃,如果足够大,但直到那个时候,画布并没有真正向我抱怨。 第二: 有安全限制。 在canvas编辑照片取决于您使用的浏览器,以及该文件是否与您的网站位于同一个域中 ...
  • 我假设你的意思是将图像加载到画布中,而不是从画布中上传图像。 阅读他们在这里的所有画布文章可能是一个好主意https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial/Using_images 但基本上你想要做的是在javascript中创建一个图像,并将image.src =设置为任何文件位置。 在从用户那里加载图像的情况下,您将要使用File System API。 这里总结一个简单的例子: http : //jsfidd ...
  • 有几个选择。 我没有使用这些图书馆之一,但是从我可以告诉的蛋糕似乎通常更令人印象深刻,进口,同时也是三倍。 还有Burst引擎,目前是processing.js的扩展,更小。 我确定那里还有几个。 Processing.js “Processing.js是流行的可视化编程语言的姊妹项目...” 大小:412 KB 拉斐尔 “Raphaël是一个小型JavaScript库,可以简化您在网络上的矢量图形工作,如果要创建自己的特定图表或图像裁剪和旋转窗口小部件,例如,您可以使用该库简单方便地实现它。Raphaël ...
  • 一个基本的例子是使用getImageData : http : //jsfiddle.net/eGjak/60/ 。 var ctx = $('#cv').get(0).getContext('2d'); for(var i = 0; i < 30; i++) { for(var j = 0; j < 30; j++) { ctx.fillStyle = 'rgb(' + ((i/30*255)|0) + ',' + ((j/30 ...
  • 基于这两个例子: http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_canvas_tut_text 我已经建立了一个可以帮助您完成所需操作的示例: http : //jsbin.com/ufiSilu/1/ JavaScript代码: function allowDrop(ev) { ev.pre ...
  • 我建议你阅读这篇文章HTML5画布 - 基础知识 但总之。 它不会取代JavaScript。 HTML 5 画布为您提供了一种使用JavaScript绘制图形的简单而强大的方法。 对于每个画布元素,您可以使用“上下文”(考虑绘图板中的页面),您可以在其中发出JavaScript命令来绘制任何所需内容。 浏览器可以实现多个画布上下文,不同的API提供绘图功能。 I suggest you read this article HTML5 Canvas - the basics But in short. It ...
  • 你可以在jsp中使用html5-canvas。 只要你在jsp中包含html5语法()和canvas的用法。 它会工作。 Canvas仅适用于这些浏览器版本。 http://caniuse.com/canvas对于不支持画布的浏览器,您可以使用pollyfill,否则功能将无法使用。 还有一点 - Spring MVC或任何其他后端技术无关紧要,只要您使用jsp或其他将html呈现给浏览器的视图。 谢谢 As clearly stated in an answer by BalusC to a differ ...
  • 我不知道如何避免刷新canva,因为它会违反刷新页面的基本思路,但您是否考虑过在区域设置或会话存储中缓存初始化数据? 参看 https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage No way that I know of to avoid refreshing the canva as it would go against the basic idea of refreshing the page, but have y ...
  • 我觉得这不符合逻辑。 你会发现首先画一堵墙, 然后选择油漆的颜色是否合乎逻辑? 编辑:fillStyle()实际上并没有绘制任何东西,它只是设置后续绘制调用的参数。 但是fillRect() 会在画布上绘制像素,因此在调用fillRect() 之前必须设置所有参数(颜色,线宽等)。 I just don't find it logical. Would you find it logical to first paint a wall, and then choose the color of the pa ...
  • 在绘制图像后,可以通过在图像上绘制半透明的黑色矩形使画布显得更暗 context.fillStyle = "rgba(0, 0, 0, 0.4)"; context.fillRect(0, 0, 700, 500); 这是jsFiddle的一个例子 您也可以使用context.globalAlpha或context.globalCompositeOperation = "lighter"; 如本SO帖所述 You can make the canvas appear darker by drawing a ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。