Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimizing Repository’s SubmitChanges Method
    primarykey
    data
    text
    <p>I have following repository. I have a mapping between LINQ 2 SQL generated classes and domain objects using a factory. </p> <p>The following code will work; but I am seeing two potential issues</p> <p>1) It is using a SELECT query before update statement.</p> <p>2) It need to update all the columns (not only the changed column). This is because we don’t know what all columns got changed in the domain object.</p> <p>How to overcome these shortcomings?</p> <p>Note: There can be scenarios (like triggers) which gets executed based on specific column update. So I cannot update a column unnecessarily. </p> <p><strong>REFERENCE</strong>:</p> <ol> <li><p><a href="https://stackoverflow.com/questions/11189688/linq-to-sql-updating-without-refresh-when-updatecheck-never">LINQ to SQL: Updating without Refresh when “UpdateCheck = Never”</a></p></li> <li><p><a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=113917" rel="nofollow noreferrer">http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=113917</a></p></li> </ol> <p><strong>CODE</strong></p> <pre><code>namespace RepositoryLayer { public interface ILijosBankRepository { void SubmitChangesForEntity(); } public class LijosSimpleBankRepository : ILijosBankRepository { private IBankAccountFactory bankFactory = new MySimpleBankAccountFactory(); public System.Data.Linq.DataContext Context { get; set; } public virtual void SubmitChangesForEntity(DomainEntitiesForBank.IBankAccount iBankAcc) { //Does not get help from automated change tracking (due to mapping) //Selecting the required entity DBML_Project.BankAccount tableEntity = Context.GetTable&lt;DBML_Project.BankAccount&gt;().SingleOrDefault(p =&gt; p.BankAccountID == iBankAcc.BankAccountID); if (tableEntity != null) { //Setting all the values to updates (except primary key) tableEntity.Status = iBankAcc.AccountStatus; //Type Checking if (iBankAcc is DomainEntitiesForBank.FixedBankAccount) { tableEntity.AccountType = "Fixed"; } if (iBankAcc is DomainEntitiesForBank.SavingsBankAccount) { tableEntity.AccountType = "Savings"; } Context.SubmitChanges(); } } } } namespace DomainEntitiesForBank { public interface IBankAccount { int BankAccountID { get; set; } double Balance { get; set; } string AccountStatus { get; set; } void FreezeAccount(); } public class FixedBankAccount : IBankAccount { public int BankAccountID { get; set; } public string AccountStatus { get; set; } public double Balance { get; set; } public void FreezeAccount() { AccountStatus = "Frozen"; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload