Castle ActiveRecord/MonoRail 模型中图像的最佳实践

发布于 2024-07-27 21:22:20 字数 367 浏览 5 评论 0原文

我们的“用户”模型需要一个小的个人资料图片,我不完全确定如何处理它。 当然,我们可以将其保存到磁盘上的文件夹并将路径/文件名存储到数据库中,但我想我宁愿将其存储在数据库本身中。

我的第一个想法是在模型上有一个像这样的属性:

[Property] 
public byte[] ProfilePicture
{
  get;
  set;
}

但确实感觉我必须走很长的路才能让它以这种方式工作——从数据库获取字节数组,然后将其转换为带有某种处理程序的图像。

有没有人看过关于如何处理此类事情的好教程? 这似乎是一个足够常见的要求,我会找到一些 MonoRail 特定的东西,但到目前为止我的搜索结果是空的。

Our "user" model needs a small profile picture on it, and I'm not entirely sure how to handle it. Of course we could just save it to a folder on disk and store the path/filename to the database, but I think I'd rather have it stored in the DB itself.

My first thought was to have a property on the model like this:

[Property] 
public byte[] ProfilePicture
{
  get;
  set;
}

But it sure feels like I'm going to have to go a LONG way to get it working this way -- getting a byte array from the database, then converting it to an image with some sort of handler.

Has anyone seen a good tutorial on how to handle this sort of thing? It seems like it would be a common enough requirement that I'd find something MonoRail specific, but so far my searches have come up empty.

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

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

发布评论

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

评论(1

日裸衫吸 2024-08-03 21:22:20

有关在数据库或文件中存储图像的信息,请参阅此问题

如果您决定将其存储在数据库上,最重要的是您不要每次查询用户时都检索byte[],这可能是潜在的问题大量数据和性能问题。 为此,您可以将图像存储在另一个表中,或者将 byte[] 映射到具有同一表的另一个实体(假设用户只能拥有一张图片):

[ActiveRecord("users")]
public class UserWithoutPicture {
  [PrimaryKey]
  public virtual int Id {get;set;}
...
  [BelongsTo]
  public virtual UserProfilePicture ProfilePicture {get;set;}
}

[ActiveRecord("users")]
public class UserProfilePicture {
  [PrimaryKey]
  public virtual int Id {get;set;}

  [Property]
  public virtual byte[] Image {get;set;}
}

这会产生一些影响不过行为很古怪。 例如,对于任何给定用户,ProfilePicture 永远不会为空。 您不会真正插入或删除 UserProfilePicture 因为它实际上是用户,相反您总是会更新。 并且您会产生额外的加入,并且您必须注意 选择 N+1。 这只是我的想法,完全未经测试

结论:将图像存储在另一个表中更加灵活。

如果您想方便地处理 Image< /code>而不是原始 byte[],实现 IUserType。 但请记住,Image 是一个 IDisposable,并且很难在正确的时间处理它。

实现返回图像的单轨控制器非常简单...只需使用 [ARFetch] 通过 id 获取 UserProfilePicture 并使用适当的内容写入响应流 -类型。

About storing images on database or files, see this question.

If you decided to store it on DB, the most important thing is that you don't retrieve the byte[] every time you query for a User, that could be potentially a lot of data and a perf problem. To do that you could either store the image in another table or map the byte[] to another entity with the same table (assuming the user can have only one picture):

[ActiveRecord("users")]
public class UserWithoutPicture {
  [PrimaryKey]
  public virtual int Id {get;set;}
...
  [BelongsTo]
  public virtual UserProfilePicture ProfilePicture {get;set;}
}

[ActiveRecord("users")]
public class UserProfilePicture {
  [PrimaryKey]
  public virtual int Id {get;set;}

  [Property]
  public virtual byte[] Image {get;set;}
}

This would have some funky behaviors though. For example, for any given user, the ProfilePicture would never be null. You wouldn't really insert or delete UserProfilePicture since it's actually the user, instead you would always update. And you would incur an additional join, and you have to be aware of SELECT N+1. That's just off the top of my head, completely untested.

Conclusion: storing images in another table is much more flexible.

If you want the convenience of dealing with an Image instead of raw byte[], implement IUserType. But remember that Image is an IDisposable, and it'll be very hard to dispose it at the right time.

Implementing a Monorail controller that returns an image is quite straightforward... just use [ARFetch] to get the UserProfilePicture by id and write to the Response stream with the appropriate content-type.

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