gpt4 book ai didi

c# - [Computed] 和 [Write(false)] 属性之间有什么区别?

转载 作者:行者123 更新时间:2023-12-04 16:28:33 25 4
gpt4 key购买 nike

This资源解释如何Computed排除属性(仅在更新中?)。

Specifie the property should be excluded from update.

[Table("Invoice")]
public class InvoiceContrib
{
[Key]
public int InvoiceID { get; set; }
public string Code { get; set; }
public InvoiceKind Kind { get; set; }
[Write(false)]
[Computed]
public string FakeProperty { get; set; }
}
using (var connection = My.ConnectionFactory())
{
connection.Open();
var invoices = connection.GetAll<InvoiceContrib>().ToList();
// The FakeProperty is skipped
invoices.ForEach(x => x.FakeProperty += "z");
var isSuccess = connection.Update(invoices);
}


没有 Write(false)达到同样的目的? [Computed]有什么区别和 [Write(false)] ?

编辑:

我刚刚查看了资源 linked回答我的问题。它几乎一针见血!有人可以确认这两个属性是否执行相同的操作,但只是以两种不同的方式表达,以便为用户提供更好的抽象?

最佳答案

两者 [Computed]Write(false)将忽略该属性,而 INSERT以及 UPDATE操作。所以,两者都是一样的。您可以使用其中任何一种。

Documentation下面说:

  • [Write(true/false)] - this property is (not) writeable
  • [Computed] - this property is computed and should not be part of updates


关于 Write :

如上述文档第一行所述, Write处理“可写”行为。这应该包括 INSERTUPDATE .

这也可以在源代码 here中得到确认:

var properties = type.GetProperties().Where(IsWriteable).ToArray();
...
...
...
private static bool IsWriteable(PropertyInfo pi)
{
var attributes = pi.GetCustomAttributes(typeof(WriteAttribute), false).AsList();
if (attributes.Count != 1) return true;

var writeAttribute = (WriteAttribute)attributes[0];
return writeAttribute.Write;
}


关于 Computed :

不过,上面文档中的第二行有点宽泛。

should not be part of updates



这是否意味着它可以是 INSERT 的一部分? ?不,不是的;它还涵盖了这两个操作。这可以通过以下代码观察:
CREATE TABLE TestTable
(
[ID] [INT] IDENTITY (1,1) NOT NULL CONSTRAINT TestTable_P_KEY PRIMARY KEY,
[Name] [VARCHAR] (100) NOT NULL,
[ComputedCol] [VARCHAR] (100) NOT NULL DEFAULT '',
[NonWriteCol] [VARCHAR] (100) NOT NULL DEFAULT ''
)
[Table("TestTable")]
public class MyTable
{
[Key]
public int ID { get; set; }

public string Name { get; set; }

[Computed]
public string ComputedCol { get; set; }

[Write(false)]
public string NonWriteCol { get; set; }
}
int id;
using(SqlConnection conn = new SqlConnection(@"connection string"))
{
MyTable myTable = new MyTable();
myTable.Name = "Name";
myTable.ComputedCol = "computed";
myTable.NonWriteCol = "writable";

conn.Insert<MyTable>(myTable);

id = myTable.ID;
}

using(SqlConnection conn = new SqlConnection(@"connection string"))
{
MyTable myTable = conn.Get<MyTable>(id);
myTable.Name = "Name_1";
myTable.ComputedCol = "computed_1";
myTable.NonWriteCol = "writable_1";

conn.Update<MyTable>(myTable);
}

通过上面的代码,你会发现无论你选择哪个属性来装饰属性, INSERT都不会考虑它。也不适用于 UPDATE .所以基本上,这两个属性都扮演着同样的角色。

这可以在 Dapper.Tests.Contrib 中进一步确认github上的测试项目。

[Table("Automobiles")]
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
[Computed]
public string Computed { get; set; }
}
...
...
...
//insert with computed attribute that should be ignored
connection.Insert(new Car { Name = "Volvo", Computed = "this property should be ignored" });

Source: 1 and 2



查看注释和上面代码中分配给属性的值,很明显 Computed还应该忽略 INSERT 的属性手术;这是测试的预期结果。

为什么为同一目的提供这两种方式尚不清楚。它会引起困惑。

以下是一些额外的引用:

Comment 1

I use [Computed] or [Write("False")] for that. Does that not work for your scenario?



Comment 2

Glad I could help. Every day is a school day! I'm not sure why they both exist though as I think they are functionally the same. I tend to use [Computed] just because it is marginally easier to type.



Comment 3

I understand that using Dapper.Contrib I can use the Write and Computed attributes to ignore properties during write operations. However, this will ignore the properties on both insert and update. I need a way to ignore properties on updates. My suggestion would be to add 2 attributes... perhaps named Insertable(bool) and Updateable(bool). When a false value is passed to these the framework would exclude that property for the given operation. This is a lightweight, straightforward approach to a very common problem.



我不认为 Computed属性与 Computed Columns有关因为 Dapper.Contrib 支持多个 RDBMS。

关于c# - [Computed] 和 [Write(false)] 属性之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57673107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com