实体框架和 POCO

发布于 2025-01-06 15:00:56 字数 1186 浏览 1 评论 0原文

我正在学习实体框架和 POCO,虽然我喜欢很多概念,但我认为我还不太明白。这是一个示例:

我有一个如下所示的架构:

create table Customer
{
  Id int,
  Name varchar(32),
  Value1 varchar(32),
  Value2 varchar(32),
  Value3 varchar(32)
  ...
  Value50 varchar(32)
}

-- ColumnName will map to "Value1", "Value2", etc
create table ColumnMapping
{
  ColumnName varchar(32),
  DisplayName varchar(32) 
}

表示此数据的对象如下所示:

class Customer
{
    public Id { get; set; }
    public Name { get; set;}
    public Dictionary<string, string> CustomData { get; set; }
}  

也就是说,我想将 Value1 到 Value50 映射到字典(其中字典的键由 ColumnMapping 表确定) )。

我想知道最好的方法是什么。

我希望客户成为 POCO,但为了做到这一点,它需要了解 Value1..Value50,以便能够将这些列转换为字典。但考虑到 POCO 应该一直保持无知,我怀疑这是否是正确的方法。

我想,总的来说,我正在努力了解 POCO 到底是什么 - 它是业务层使用的对象,还是需要在 POCO 和“业务对象”以及“业务对象”之间存在映射" 是业务层应该使用的。

任何有关如何处理此类情况的建议将不胜感激。

编辑

由于我没有收到我想问的问题的答案,我将继续说明我的决定(以防有人遇到类似的问题)。虽然 POCO 是持久无知的,因为它不需要知道它是如何持久化的,但它并不是完全持久无知的。也就是说,它必须以某种方式绑定到持久层。

在我的示例中,虽然我不希望业务层了解 Value1、Value2、Value3 等,但有人需要了解它才能将这些值转换为字典。我相信放置该逻辑的正确位置是 POCO,因此,我相信 POCO 应该具有 Value1、Value2、Value3 等列的属性。

谢谢, 埃里克

I am learning about the Entity Framework and POCOs and while I like a lot of the concepts, I think I am not quite getting it. Here's an example:

I have a schema like the following:

create table Customer
{
  Id int,
  Name varchar(32),
  Value1 varchar(32),
  Value2 varchar(32),
  Value3 varchar(32)
  ...
  Value50 varchar(32)
}

-- ColumnName will map to "Value1", "Value2", etc
create table ColumnMapping
{
  ColumnName varchar(32),
  DisplayName varchar(32) 
}

The object which represents this data looks like:

class Customer
{
    public Id { get; set; }
    public Name { get; set;}
    public Dictionary<string, string> CustomData { get; set; }
}  

That is, I'd like to map the Value1 to Value50 to a Dictionary (where the Key of the dictionary is determined by the ColumnMapping table).

I am wondering what the best approach to this.

I'd like the Customer to be a POCO, but in order to do that, it would need to know about Value1..Value50 so that it would be able to convert those columns into a dictionary. But given that a POCO should be persistent ignorant, I am questioning if that is the right approach.

I guess, in general, I am struggling with what the POCO really is - is it the object which is used by the business layer, or does there need to be a mapping between the POCO and a "business object" and the "business object" is what should be used by the business layer.

Any advice on how to deal with this type of scenario will be appreciated.

Edit

As I didn't receive an answer to the question I was trying to ask, I'll go ahead and indicate what I decided (in case anyone has this similar issue). While the POCO is persistent ignorant in that it doesn't need to know about how it gets persisted, it's not entirely persistent ignorant. That is, it has to be tied to the persistence layer in some manner.

In my example, while I don't want the business layer to know about Value1, Value2, Value3, etc, someone needs to know about it in order to convert those values to a dictionary. I believe that the right place to put that logic is the POCO and hence, I believe the POCO should have properties for the Value1, Value2, Value3, etc, columns.

Thanks,
Eric

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

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

发布评论

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

评论(1

酷炫老祖宗 2025-01-13 15:00:56

在 ORM 世界中,这是典型的方法

class Customer
{
   public int Id { get; set; }
   public string Name {get; set; }
   public virtual ICollection<CustomDatum> CustomData { get; set; }
}

class CustomDatum
{
   public int Id { get; set; }   // PK
   public string ColomnName { get; set; }
   public string DisplayName { get; set; }
}

In ORM world, this is typical approach

class Customer
{
   public int Id { get; set; }
   public string Name {get; set; }
   public virtual ICollection<CustomDatum> CustomData { get; set; }
}

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