I am using Dapper.net Extensions and I would like to be able to retrieve a Photo object and set 'this' to it without having to set each property individually. What would be the best way to accomplish this? In the code below it says I cannot assign to 'this' because it is readonly.
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);
}
}
}
Unfortunately, there is no way to do this without setting the properties. An elegant way to do this would be to use a static method to load the Photo. I don't have the extension you are using, so the following code example is a bit different, but it should work as an example.
Some more discussion here from Jon Skeet about the topic: http://bytes.com/topic/c-sharp/answers/513887-cannot-assign-because-read-only