在领域中执行数据库操作的通用代码实践

时间:2016-11-17 06:41:28

标签: android design-patterns realm

这更像是一个设计模式问题,而不是realm,我试图将两种不同类型的对象添加到领域。

complaint 
dealer

我创建了一个两个单独的函数来添加这两个对象。我的问题是我不想编写单独的代码来为每个对象插入数据,我想通过定义一个公共类然后扩展该类来概括。

这是我当前的代码

经销商

public void insertDealersData(最终交易商经销商){

    final Realm realm = getExplicitRealm() ;
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {

            int newDealersSize = dealers.getDealersLists().size();
            Dealers dbDealers=   realm.where(Dealers.class).findFirst();

            if(dbDealers!=null){
                for(int i =0;i<newDealersSize;i++){
                    DealersList temp=   realm.where(DealersList.class).equalTo("dealerid",
                            dealers.getDealersLists().get(i).getDealerid()).findFirst();
                    if(temp!=null){
                        dbDealers.getDealersLists().set(i,temp);
                    }else{
                        dbDealers.getDealersLists().add( dealers.getDealersLists().get(i));
                    }
                }
            }else{
                realm.copyToRealmOrUpdate(dealers);
                Log.i(TAG, "insertDealersData : dump");
            }
        }
    },new Realm.Transaction.Callback(){

        @Override
        public void onSuccess() {
            realm.close();
        }
        @Override
        public void onError(Exception e ) {
            realm.close();       
        }
    });
}

投诉  public void insertComplaintData(最终投诉投诉){

    final Realm realm = getExplicitRealm() ;

    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {

            int newComplaintSize = complaint.getComplaintLists().size();
            Complaint dbComplaint=   realm.where(Complaint.class).findFirst();

            if(dbComplaint!=null){
                for(int i =0;i<newComplaintSize;i++){
                    ComplaintList temp=   realm.where(ComplaintList.class).equalTo("incidentid",
                            complaint.getComplaintLists().get(i).getIncidentid()).findFirst();
                    if(temp!=null){
                        dbComplaint.getComplaintLists().set(i,temp);
                        Log.i(TAG, "insertComplaintData : update existing");
                    }else{
                        dbComplaint.getComplaintLists().add(complaint.getComplaintLists().get(i));
                        Log.i(TAG, "insertComplaintData : add new ");
                    }
                }

            }else{
                realm.copyToRealmOrUpdate(complaint);
                Log.i(TAG, "insertComplaintData : dump");
            }
        }
    },new Realm.Transaction.Callback(){
        @Override
        public void onSuccess() {
            realm.close(); 
        }
        @Override
        public void onError(Exception e ) {
            realm.close();
        }
    });
}

正如您所看到的,除了对象类型之外,代码中没有任何变化。所以任何人都可以建议如何通过减少冗余来推广代码。

投诉模式

public class Complaint extends RealmObject{

    private String status;    
    private String errorMessage;    
    private String count;
    @PrimaryKey    
    private String userId;    
    private String organizationId;
    @JsonField(name ="complaints" , typeConverter = ComplaintListConverter.class)
    private RealmList<ComplaintList> complaintLists;    

}
@JsonObject
public  class ComplaintList extends RealmObject implements ComplaintInterface {

    @PrimaryKey
    private String _id;    
    private String incidentid;    
    private String title;    
    private String casetypecode;
    private String customerid;
}

经销商型号

public class Dealers extends RealmObject {
    private String status;
    private String errorMessage;
    private String count;
    @PrimaryKey
    private String userId;
    private String organizationId;
    @JsonField(name="dealers", typeConverter = DealersListConverter.class)
    private RealmList<DealersList> dealersLists;
}


public class DealersList extends RealmObject {


    @PrimaryKey
    private String _id;
    private String dealerid;
    private String accountnumber;
    private String name;
    private String alletec_type;
    private String telephone1;

}

1 个答案:

答案 0 :(得分:1)

#pragma omp parallel for
for(int k=0;k<m;k++)
{
   for(int j=n-1;j>=0;j--)
   {
       outX[k+j*m] = inB2[j+n * k] / inA2[j*n + j];
Q1 = k*n
Q2 = n*j
Q3 = m*j + k
#pragma omp declare simd private(i,j,k,m,Q1,Q2,Q3) linear(i) uniform(outX,inA2,inB2) shared(inB2,inA2,outX)
       for(int i=0;i<j;i++)
       {
           inB2[Q1+i] -= inA2[Q2+i] * outX[Q3];
       }
   }
}