2013-05-23 7 views
0

현재 C# 및 linq을 사용하여 SQL 데이터베이스에 데이터를 삽입/업데이트하고 있습니다. 다음 코드는 완벽하게 작동하지만 지저분하고 중복되어 있다고 느낍니다. linq을 사용하는 동안 중복 코드 방지

는 다음과 같은 코드를 살펴 제발 대신 내 코드를

를 복제 할 필요없이 데이터를 업데이트 할 빠른이 있다면 그냥 속성을 설정하면

Incident inc = new Incident 
     { 
      AccountID = AccountID, 
      SiteID = siteID, 
      DepartmentID = departmentID, 
      LocationID = LocationID, 
      QuestionCategoryID = CategoryID, 
      IncidentSourceID = IncidentSourceID, 
      IncidentTypeID = IncidentTypeID, 
      NonConformanceTypeID = NonConID, 
      ProductGroupID = ProductGroupID, 
      ProductID = ProductID, 
      ComponentID = ComponentID, 
      ProductReference = prodRef, 
      CurrentAssignedUserID = UserId, 
      CurrentAssignedContactID = contactid, 
      OriginalAssignedUser = UserId, 
      OriginalAssignedContact = contactid, 
      LoggedByUserID = logUserId, 
      LoggedByContactID = logContactid, 
      IncidentTitleID = IncidentTitleID, 
      Title = IncidentTitle.ToString(), 
      Description = problemDesc, 
      Comments = comments, 
      ActionsRequired = actions, 
      RiskPriorityID = RiskPriorityTypeID, 
      AffectedPartyID = affectedPartyID, 
      ImpactLevel = Convert.ToInt32(impact), 
      Justification = justification, 
      EsculationDate = DateTime.Today, 
      PriorityID = PriorityID, 
      OriginalPriorityID = PriorityID, 
      CreatedByUser = Convert.ToInt32(loggedInUserID), 
      UpdatedBy = Convert.ToString(loggedInUserID), 
      RiskID = RiskID, 
      Active = true, 
      StatusID = 1, 

      DelayedDate = null, 
      IncidentCloseDate = null, 
      IncidentDate = DateTime.Now, 
      IncidentPendingDate = DateTime.Now, 
      LoggedDate = DateTime.Now, 
      LastUpdated = DateTime.Now, 
      LastActionTaken = DateTime.Now 
     }; 

     // Save the data to the database 
     if (Request.QueryString["IncidentID"] == null) 
     { 
      // Insert a new incident. 
      db.Incidents.Add(inc); 
      db.SaveChanges(); 

     } 
     else 
     { 
      //update an existing incident. 
      long ID = Convert.ToInt64(Request.QueryString["IncidentID"]); 
      var record = db.Incidents.Where(i => i.IncidentID == ID).FirstOrDefault(); 

      record.AccountID = AccountID; 
      record.SiteID = siteID; 
      record.DepartmentID = departmentID; 
      record.LocationID = LocationID; 
      record.QuestionCategoryID = CategoryID; 
      record.IncidentSourceID = IncidentSourceID; 
      record.IncidentTypeID = IncidentTypeID; 
      record.NonConformanceTypeID = NonConID; 
      record.ProductGroupID = ProductGroupID; 
      record.ProductID = ProductID; 
      record.ComponentID = ComponentID; 
      record.ProductReference = prodRef; 
      record.CurrentAssignedUserID = UserId; 
      record.CurrentAssignedContactID = contactid; 
      record.OriginalAssignedUser = UserId; 
      record.OriginalAssignedContact = contactid; 
      record.LoggedByUserID = logUserId; 
      record.LoggedByContactID = logContactid; 
      record.IncidentTitleID = IncidentTitleID; 
      record.Title = IncidentTitle.ToString(); 
      record.Description = problemDesc; 
      record.Comments = comments; 
      record.ActionsRequired = actions; 
      record.RiskPriorityID = RiskPriorityTypeID; 
      record.AffectedPartyID = affectedPartyID; 
      record.ImpactLevel = Convert.ToInt32(impact); 
      record.Justification = justification; 
      record.EsculationDate = DateTime.Today; 
      record.PriorityID = PriorityID; 
      record.OriginalPriorityID = PriorityID; 
      record.CreatedByUser = Convert.ToInt32(loggedInUserID); 
      record.UpdatedBy = Convert.ToString(loggedInUserID); 
      record.RiskID = RiskID; 
      record.Active = true; 
      record.StatusID = 1; 

      record.DelayedDate = null; 
      record.IncidentCloseDate = null; 
      record.IncidentDate = DateTime.Now; 
      record.IncidentPendingDate = DateTime.Now; 
      record.LoggedDate = DateTime.Now; 
      record.LastUpdated = DateTime.Now; 
      record.LastActionTaken = DateTime.Now; 

      db.SaveChanges(); 
     } 
+0

아마이 [코드 검토]에서 질문해야한다 (http://codereview.stackexchange.com/faq). –

답변

0

감사 말해 수 한 번, 날씨에 상관없이 기존 엔티티 여부.

var id = Request.QueryString["IncidentID"]; 
var incidentId = String.IsNullOrEmpty(id) ? 0 : int.Parse(id); 

var record = incidentId !=0 ? 
    db.Incidents.FirstOrDefault(i => i.IncidentID == incidentId); : new Incident(); 

record.AccountID = AccountID; 
record.SiteID = siteID; 
record.DepartmentID = departmentID; 
//etc....... 

if (incidentId == 0) 
{ 
    //set any fields here that are for add only 
    record.CreatedByUser = ... 
    db.Incidents.Add(record); 
} 
db.SaveChanges(); 
0
Incident inc; 

if (Request.QueryString["IncidentID"] == null) 
{ 
    inc = new Incident(); 

    // set properties that are specific to insert 

    db.Incidents.Add(inc); 
} 
else 
{ 
    long ID = Convert.ToInt64(Request.QueryString["IncidentID"]); 
    inc = db.Incidents.Where(i => i.IncidentID == ID).First();  

    // set properties that are specific to update 
} 

// set common properties 

db.SaveChanges(); 
0

당신은 다음 작업을 수행 할 수 있습니다 :

var incidentInitializer = new Action<Incident>(incident => 
{ 
    incident.AccountID = AccountID, 
    incident.SiteID = siteID, 
    ... 
}; 

// Save the data to the database 
if (Request.QueryString["IncidentID"] == null) 
{ 
    // Insert a new incident. 
    var inc = new Incident(); 
    incidentInitializer(inc); 

    db.Incidents.Add(inc); 
    db.SaveChanges(); 
} 
else 
{ 
    //update an existing incident. 
    long ID = Convert.ToInt64(Request.QueryString["IncidentID"]); 
    var record = db.Incidents.Where(i => i.IncidentID == ID).FirstOrDefault(); 
    incidentInitializer(record); 

    db.SaveChanges(); 
}