亚音速添加或更新方法转换为通用方法
所以我有这两种方法
private static void AddOrUpdate(Computer input)
{
if (Simple.Repository.Exists<Computer>(o => o.ObjectSid == input.ObjectSid))
{
Simple.Repository.Update(input);
}
else
{
Simple.Repository.Add(input);
}
}
private static void AddOrUpdate(User input)
{
if (Simple.Repository.Exists<User>(o => o.ObjectSid == input.ObjectSid))
{
Simple.Repository.Update(input);
}
else
{
Simple.Repository.Add(input);
}
}
,作为练习,我想知道是否可以使用泛型方法,但仅将方法更改为
private static void AddOrUpdate<T>(T input)
{
if (Simple.Repository.Exists<T>(o => o.ObjectSid == input.ObjectSid))
{
Simple.Repository.Update(input);
}
else
{
Simple.Repository.Add(input);
}
}
不起作用 - 编译器表示类型 T 必须是引用类型。
那么有可能吗?或者甚至是令人向往的?有更好的重构吗?
public class User
{
private string _samAccountName;
[DisplayName("User Name")]
public string SamAccountName
{
get { return _samAccountName ?? "No User"; }
set { _samAccountName = value; }
}
public string UserPrincipalName { get; set; }
public string DisplayName { get; set; }
public DateTime LastLogonTimeStamp { get; set; }
[SubSonicPrimaryKey] public Guid ObjectSid { get; set; }
public Guid? ComputerGuid { get; set; }
}
and
public class Computer
{
public string DistinguishedName { get; set;}
public string DnsHostname { get; set; }
public string Description { get; set; }
public string Cn { get; set; }
public DateTime LastLogonTimeStamp { get; set; }
public String OperatingSystem { get; set; }
public string OperatingSystemServicePack { get; set; }
[SubSonicPrimaryKey] public Guid ObjectSid { get; set;}
public Guid? UserGuid { get; set; }
}
So I've got these two methods
private static void AddOrUpdate(Computer input)
{
if (Simple.Repository.Exists<Computer>(o => o.ObjectSid == input.ObjectSid))
{
Simple.Repository.Update(input);
}
else
{
Simple.Repository.Add(input);
}
}
private static void AddOrUpdate(User input)
{
if (Simple.Repository.Exists<User>(o => o.ObjectSid == input.ObjectSid))
{
Simple.Repository.Update(input);
}
else
{
Simple.Repository.Add(input);
}
}
and as an exercise I'm wondering if I could use a generic method but just changing the method to
private static void AddOrUpdate<T>(T input)
{
if (Simple.Repository.Exists<T>(o => o.ObjectSid == input.ObjectSid))
{
Simple.Repository.Update(input);
}
else
{
Simple.Repository.Add(input);
}
}
doesn't work - the compiler says that type T must be a reference type.
So is it possible? Or even desirable? Is there a better refactoring?
public class User
{
private string _samAccountName;
[DisplayName("User Name")]
public string SamAccountName
{
get { return _samAccountName ?? "No User"; }
set { _samAccountName = value; }
}
public string UserPrincipalName { get; set; }
public string DisplayName { get; set; }
public DateTime LastLogonTimeStamp { get; set; }
[SubSonicPrimaryKey] public Guid ObjectSid { get; set; }
public Guid? ComputerGuid { get; set; }
}
and
public class Computer
{
public string DistinguishedName { get; set;}
public string DnsHostname { get; set; }
public string Description { get; set; }
public string Cn { get; set; }
public DateTime LastLogonTimeStamp { get; set; }
public String OperatingSystem { get; set; }
public string OperatingSystemServicePack { get; set; }
[SubSonicPrimaryKey] public Guid ObjectSid { get; set;}
public Guid? UserGuid { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 Computer 和 User 继承自定义 public Guid ObjectSid 的基类,那么您可以
随时定义通用方法。
If Computer and User inherit from a base class that defines
public Guid ObjectSid
then you can define your generic method asand away you go.
我们通过让所有 SubSonic 生成的类实现一个接口来完成此操作:
IsPersisted 的实现检查主键字段是否等于该类型的默认值。然后,我们在此基础上做出“添加”或“更新”决定“保存”。这让我们可以避免查询数据库来查看 id 是否存在。
We have done this by making all our SubSonic generated classes implement an interface:
The implementation of IsPersisted checks if the primary key field is equal to the default value for that type. We then drive an Add or Update decision on Save off of that. This let's us avoid querying the DB to see if the id exists there or not.