我有一个应用程序实体的基类:
public class LadderEntityBase : ICloneable
{
public Guid PK { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
public object Clone()
{
return this.MemberwiseClone();
}
}
然后从它派生两个类,具有相同的功能重载,允许创建具有或不具有PK(Guid)参数的那些类。如果省略PK参数 - 将创建新的Guid:
public class Order : LadderEntityBase
{
public Order() : this(Guid.NewGuid())
{
}
public Order(Guid guid)
{
this.PK = guid;
}
public string OrderFrom { get; set; }
}
和
public class Parcel : LadderEntityBase
{
public Parcel() : this(Guid.NewGuid())
{
}
public Parcel(Guid guid)
{
this.PK = guid;
}
public string SentTo { get; set; }
}
是否可以将Order和Parcel的两个构造函数移动到基类中?
答案 0 :(得分:2)
我相信它尽你所能。移动基类中的所有逻辑,并在继承的
中保留基本调用[HttpGet("[action]")]
public async Task<myPaginatedReturnedData> MyMethod(int page)
{
int perPage = 10;
int start = (page - 1) * perPage;
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("externalAPI");
MediaTypeWithQualityHeaderValue contentType =
new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(contentType);
HttpResponseMessage response = await client.GetAsync(client.BaseAddress);
string content = await response.Content.ReadAsStringAsync();
IEnumerable<myReturnedData> data =
JsonConvert.DeserializeObject<IEnumerable<myReturnedData>>(content);
myPaginatedReturnedData datasent = new myPaginatedReturnedData
{
Count = data.Count(),
myReturnedData = data.Skip(start).Take(perPage).ToList(),
};
return datasent;
}
}
答案 1 :(得分:0)
您可以在Base类本身上设置PK,然后使用base关键字设置GUID(如果您希望两个派生类都有不同的GUID)。
public class LadderEntityBase : ICloneable
{
public Guid PK { get; set; }
public string Name { get; set; }
public string Comment { get; set; }
public LadderEntityBase(Guid guid)
{
this.PK = guid;
}
public object Clone()
{
return this.MemberwiseClone();
}
}
public class Order : LadderEntityBase
{
public Order() : this(Guid.NewGuid())
{
}
public Order(Guid guid) : base(guid)
{
}
public string OrderFrom { get; set; }
}
public class Parcel : LadderEntityBase
{
public Parcel() : this(Guid.NewGuid())
{
}
public Parcel(Guid guid) :base( guid)
{
}
public string SentTo { get; set; }
}