麻烦与大量的代码重复

Gua*_*ano 6 c# generics maintainability android-sqlite

我有很多方法在大部分内容遵循相同的算法,理想情况下我希望能够调用一个消除大量代码重复的泛型方法.

我有很多类似下面的方法,我最好希望能够只调用 Save<SQLiteLocation>(itemToSave);但是我有很多麻烦,因为这些方法 SQLiteConnection.Find<T>不会接受像泛型中的T这样的抽象数据类型.

有没有办法解决这个问题,如果我能解决这个问题,我会节省多达150行代码

这是我的代码:

    public bool SaveLocation(ILocation location, ref int primaryKey)
    {
        var dbConn = new SQLiteConnection (dbPath);

        SQLiteLocation itemToSave = new SQLiteLocation ();
        itemToSave.LocationName = location.LocationName;
        itemToSave.Latitude = location.Latitude;
        itemToSave.Longitude = location.Longitude;
        itemToSave.PrimaryKey = location.PrimaryKey;

  ----------------------------------------------------------------------------------------

        SQLiteLocation storedLocation = dbConn.Find<SQLiteLocation>
                                        (x => x.PrimaryKey == location.PrimaryKey);

        if (storedLocation != null)
        {
            dbConn.Update(itemToSave);
            return true;
        }

        else if (storedLocation == null)
        {
            dbConn.Insert(itemToSave);
            primaryKey = itemToSave.PrimaryKey;
            return true;
        }
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

在这里另一种方法看看我的虚线下面的两种方法中的代码基本上是一样的

    public bool SaveInvitation(IInvitation invitation, ref int primaryKey)
    {
        var dbConn = new SQLiteConnection(dbPath);

        SQLiteInvitation itemToSave = new SQLiteInvitation ();
        itemToSave.GroupName = invitation.GroupName;
        itemToSave.InviterName = invitation.InviterName;
        itemToSave.ParseID = invitation.ParseID;
        itemToSave.GroupParseID = invitation.GroupParseID;
        itemToSave.PrimaryKey = invitation.PrimaryKey;

---------------------------------------------------------------------------------------

        SQLiteInvitation storedInvitation = dbConn.Find<SQLiteInvitation> 
                                            (x => x.PrimaryKey == invitation.PrimaryKey);

        if (storedInvitation != null)
        {
            dbConn.Update(itemToSave);
            return true;
        }
        else if (storedInvitation == null)
        {
            dbConn.Insert(itemToSave);
            primaryKey = itemToSave.PrimaryKey;
            return true;
        }
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

Gua*_*ano 0

由于某种原因,当我使用时它抛出了不支持的异常:

    T storedItem = dbConn.Find<T>(x => x.PrimaryKey == item.PrimaryKey);
Run Code Online (Sandbox Code Playgroud)

下面的代码解决了我的问题,感谢大家的帮助,我喜欢这个网站!另外,我对 T 添加了另一个约束,因为 T 必须是非抽象类型,而我认为接口就是这样的

    private void SaveItem<T>(T item, ref int primaryKey) 
        where T : ISQLiteClass, new()
    {
        var dbConn = new SQLiteConnection(dbPath);

        T storedItem = dbConn.Find<T>(primaryKey);

        if (storedItem != null)
        {
            dbConn.Update(item);
        }
        else if (storedItem == null)
        {
            dbConn.Insert(item);
            primaryKey = item.PrimaryKey;
        }
    }
Run Code Online (Sandbox Code Playgroud)