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

标签 c# attributes dapper dapper-contrib

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/

相关文章:

c# - Net Core Connection String Dapper visual studio 2017

c# - 将接口(interface)作为参数传递给扩展方法

C# 从 Color 获取所有颜色

c# - PowerShell WPF 应用程序中的动态数据模板

wcf - 用于数据验证的DataMember属性

c# - 使用 Dapper 将 byte[] 数组转换为 sqldbtype.Varbinary

c# - 使用 iOS 客户端应用程序和 C# 服务器进行长轮询

ruby-on-rails - Rails : Virtual attributes and form values

xml - 允许任何具有定义前缀的属性

c# - 在 Dapper 中执行带参数的存储过程