c# - 大量代码重复的问题

标签 c# generics android-sqlite maintainability

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

我有很多类似下面的方法,我最希望能够调用 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 的任何允许类之间的任何公共(public)项。最好查看您的代码、公开 PrimaryKey 属性的接口(interface)或类。

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() 约束。

关于c# - 大量代码重复的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27302774/

相关文章:

android - 使用 Room Persistence 库插入一对多关系

android - 在 Android 中使用 OPEN_READWRITE 访问权限在 SD 卡上打开 SQLiteDatabase 数据库

c# - 如何返回 Task<int>?

c# - C# 中的通用类型构造函数解析,适用于具有通用服务注册的 IoC

c# - 从 Internet/Intranet 访问 SQL Server

java - 通用通配符的正确使用

ios - swift 为通用类分配一些空值

java - 在Android widget 类中使用静态成员是否正常?

c# - ManagementObjectSearcher 内存不足异常

c# - 目录不存在。参数名称 : directoryVirtualPath