亚音速添加或更新方法转换为通用方法

发布于 2024-09-18 05:08:19 字数 2047 浏览 2 评论 0原文

所以我有这两种方法

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

錯遇了你 2024-09-25 05:08:19

如果 Computer 和 User 继承自定义 public Guid ObjectSid 的基类,那么您可以

private static void AddOrUpdate<T>(T input) where T : BaseClass, new()
{
    if (Simple.Repository.Exists<T>(o => o.ObjectSid == input.ObjectSid))
    {
        Simple.Repository.Update(input);
    }
    else
    {
        Simple.Repository.Add(input);
    }
}

随时定义通用方法。

If Computer and User inherit from a base class that defines public Guid ObjectSid then you can define your generic method as

private static void AddOrUpdate<T>(T input) where T : BaseClass, new()
{
    if (Simple.Repository.Exists<T>(o => o.ObjectSid == input.ObjectSid))
    {
        Simple.Repository.Update(input);
    }
    else
    {
        Simple.Repository.Add(input);
    }
}

and away you go.

泅人 2024-09-25 05:08:19

我们通过让所有 SubSonic 生成的类实现一个接口来完成此操作:

public interface IPersistable {
  bool IsPersisted { get; }
}

IsPersisted 的实现检查主键字段是否等于该类型的默认值。然后,我们在此基础上做出“添加”或“更新”决定“保存”。这让我们可以避免查询数据库来查看 id 是否存在。

We have done this by making all our SubSonic generated classes implement an interface:

public interface IPersistable {
  bool IsPersisted { get; }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文