views:

11

answers:

0

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; }
    }