【问题标题】:Why can't I set "this" to a value in C#?为什么我不能在 C# 中将“this”设置为一个值?
【发布时间】:2013-08-17 04:25:15
【问题描述】:

我正在使用 Dapper.net Extensions,我希望能够检索一个 Photo 对象并将其设置为“this”,而无需单独设置每个属性。实现这一目标的最佳方法是什么?在下面的代码中,它说我不能分配给“this”,因为它是只读的。

public class Photo
{
    public Int32 PhotoId { get; set; }
    public Guid ObjectKey { get; set; }
    public Int16 Width { get; set; }
    public Int16 Height { get; set; }
    public EntityObjectStatus ObjectStatus { get; set; }
    public PhotoObjectType PhotoType { get; set; }
    public PhotoFormat2 ImageFormat { get; set; }
    public Int32 CategoryId { get; set; }

    public Photo(int pPhotoId)
    {
        Load(pPhotoId);
    }

    public void Load(int pPhotoId)
    {
        using (SqlConnection conn = new SqlConnection(Settings.Conn))
        {
            conn.Open();
            this = conn.Get<Photo>(pPhotoId);
        }
    }
}

【问题讨论】:

    标签: c# dapper dapper-extensions


    【解决方案1】:

    不幸的是,如果不设置属性,就无法做到这一点。一个优雅的方法是使用静态方法来加载照片。我没有你正在使用的扩展,所以下面的代码示例有点不同,但它应该可以作为示例。

    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        public class Photo
        {
            public Int32 PhotoId { get; set; }
            public Guid ObjectKey { get; set; }
            public Int16 Width { get; set; }
            public Int16 Height { get; set; }
            public Int32 CategoryId { get; set; }
    
            public static Photo Load(int id)
            {
                using (SqlConnection conn = new SqlConnection("ABC"))
                {
                    return conn.Get<Photo>(id);
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Photo photo = Photo.Load(1);
            }
        }
    }
    

    Jon Skeet 在这里就该主题进行了更多讨论:http://bytes.com/topic/c-sharp/answers/513887-cannot-assign-because-read-only

    【讨论】:

      【解决方案2】:

      您不能,您必须单独复制这些方法,但是您可以使用反射之类的方法或 AutoMapper 之类的库来简化操作。

      话虽如此,我认为更好的计划是让Load 成为静态并让它返回一个新的Photo 实例,这是您在.NETframeworkitself 中最常看到的模式。

      public static Photo Load(int pPhotoId)
      {
          using (SqlConnection conn = new SqlConnection(Settings.Conn))
          {
              conn.Open();
              return conn.Get<Photo>(pPhotoId);
          }
      }
      

      【讨论】:

        【解决方案3】:

        this只读的 ...所以不,你不能那样做。

        有像AutoMapper 这样的框架用于对象之间的映射。也许你应该调查一下。

        话虽如此.. 我认为您的设计可以重新考虑。您已经到了域对象自己加载数据的地步,并且您已经意识到您将编写重复的映射代码。我认为是时候将其提取到“服务”类中并从您的域对象中完全删除逻辑(从而使您的问题无效,因为无论如何您都不会遇到这种情况)。

        【讨论】:

        • 处理 CRUD 操作的服务类是一个很好的建议,我已经实现了。我去探索一种有效的方法来删除冗余代码,你的建议很有帮助!
        猜你喜欢
        • 1970-01-01
        • 2020-08-19
        • 1970-01-01
        • 1970-01-01
        • 2012-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-24
        相关资源
        最近更新 更多