大量代码重复的问题

本文关键字:问题 代码 | 更新日期: 2023-09-27 18:33:06

我有很多方法在很大程度上遵循相同的算法,理想情况下,我希望能够调用一个通用方法,以消除大量代码重复。

有很多方法,如下所示,我最好希望能够调用 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;
    }

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

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

大量代码重复的问题

你不应该做这样的事情吗:注意:ICommonInterface 是您希望用作 T 的任何允许类之间的通用项,最好查看您的代码,一个公开 PrimaryKey 属性的接口或类。

public bool SaveItem<T>(T item, ref int primaryKey) where T : ICommonInterface, new()
{
    var dbConn = new SQLiteConnection(dbPath);

    T storedItem = dbConn.Find<T>(x => x.PrimaryKey == item.PrimaryKey);
    if (storedItem != null)
    {
        dbConn.Update(item);
        return true;
    }
    else if (storedItem == null)
    {
        dbConn.Insert(item);
        primaryKey = item.PrimaryKey;
        return true;
    }
    return false;
}

编辑:将new()约束添加到方法中。

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

    T storedItem = dbConn.Find<T>(x => x.PrimaryKey == item.PrimaryKey);

下面的代码解决了我的困境,谢谢大家的帮助,我喜欢这个网站!我还在 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;
        }
    }