问题描述
此资源说明了Computed
如何排除属性(仅在更新中) ?).
This resource explains how Computed
excludes a property (in an update only?).
[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)]
有什么区别?
Doesn't Write(false)
fulfill the same purpose though? What's the difference between [Computed]
and [Write(false)]
?
我刚刚检查了资源链接,以回答我的问题.差点被钉住了!有人可以请您确认两个属性是否执行相同的操作,但只是用两种不同的方式措辞,以便为用户提供更好的抽象?
I've just checked the resource linked in response to my question. It almost hits the nail on this! Could someone please confirm if both attributes perform the same operations, but are just worded in two different ways, as to give a better abstraction to their users?
推荐答案
在INSERT
以及UPDATE
操作时,[Computed]
和Write(false)
都将忽略该属性.因此,它们都是相同的.您可以使用任何一种.
Both [Computed]
and Write(false)
will ignore the property while INSERT
as well as UPDATE
operations. So, both of them are same. You can use any one of it.
文档如下所示:
Documentation says below:
关于Write
:
About Write
:
如上面文档的第一行所述,Write
处理可写"行为.这应该同时包含INSERT
和UPDATE
.
As stated in first line in document above, Write
handles "writeable" behavior. This should include both INSERT
and UPDATE
.
这也可以在源代码此处确认:
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
:
About Computed
:
上面文档中的第二行有点宽.
Second line in document above is bit broad though.
这是否意味着它可以成为INSERT
的一部分?不,不是的;它还涵盖了这两个动作.可以通过以下代码观察到这一点:
Does that mean it can be the part of INSERT
? No, it does not; it also cover both the actions. This can be observed with below code:
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
都不会考虑.因此,基本上,这两个属性都扮演着相同的角色.
With above code, you will observe that no matter which attribute you choose to decorate the property, it will neither be considered for INSERT
nor for UPDATE
. So basically, both the attributes are playing same role.
This can be further confirmed in Dapper.Tests.Contrib test project on 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" });
通过查看上面的代码中的注释和分配给该属性的值,可以清楚地看到Computed
也应该忽略INSERT
操作的属性;这是测试的预期结果.
Looking at the comment and the value assigned to the property in above code, it makes clear that Computed
should also ignore the property for INSERT
operation; it is expected result of the test.
为何出于相同目的提供这两种方式尚不清楚.会引起混乱.
Why those two ways are provided for same purpose is not known. It causes confusion.
以下是一些其他参考:
我认为Computed
属性与计算列作为Dapper.Contrib支持多个RDBMS.
I don't think Computed
attribute has anything to do with Computed Columns as Dapper.Contrib support multiple RDBMS.
这篇关于[计算]和[写入(假)]属性之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!