[计算]和[写入(假)]属性之间有什么区别? [英] What's the difference between [Computed] and [Write(false)] attributes?

查看:117
本文介绍了[计算]和[写入(假)]属性之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

资源说明了Computed如何排除属性(仅在更新中) ?).

This resource explains how Computed excludes a property (in an update only?).

指定该属性应从更新中排除.

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)]有什么区别?

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(true/false)]-此属性(不可)写入
  • [Computed]-此属性是计算得出的,不应作为更新的一部分
  • [Write(true/false)] - this property is (not) writeable
  • [Computed] - this property is computed and should not be part of updates

关于Write:

About Write:

如上面文档的第一行所述,Write处理可写"行为.这应该同时包含INSERTUPDATE.

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.

不应成为更新的一部分

should not be part of updates

这是否意味着它可以成为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);
}

使用上面的代码,您将观察到,无论选择哪个属性来装饰属性,对于INSERTUPDATE都不会考虑.因此,基本上,这两个属性都扮演着相同的角色.

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.

可以在 Dapper.Tests.Contrib 在github上测试项目.

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" });

来源: 1 2

通过查看上面的代码中的注释和分配给该属性的值,可以清楚地看到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.

以下是一些其他参考:

评论1

我为此使用[Computed][Write("False")].这不适用于您的情况吗?

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

评论2

很高兴我可以帮忙.每天都是上学日!我不确定它们为什么同时存在,因为我认为它们在功能上是相同的.我倾向于使用[Computed]只是因为它稍微容易键入.

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.

评论3

我了解使用Dapper.Contrib可以在写入操作期间使用WriteComputed属性忽略属性.但是,这将忽略插入和更新的属性.我需要一种忽略更新属性的方法.我的建议是添加2个属性...可能分别命名为Insertable(bool)Updateable(bool).当将false值传递给这些对象时,框架将为给定操作排除该属性.这是解决一个非常常见问题的一种轻量级,直接的方法.

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属性与

I don't think Computed attribute has anything to do with Computed Columns as Dapper.Contrib support multiple RDBMS.

这篇关于[计算]和[写入(假)]属性之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆