Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking Entity Class Closed for Changes
    primarykey
    data
    text
    <p>I have a database relationship as shown below. The domain objects are created based on LINQ to SQL ORM. </p> <p>A payment comprises of Cash Payment and Gift Coupon Payments. Suppose the total amount of purchase is 550. It can be paid as following components</p> <pre><code>1 Gift Coupon Valued 300 1 Gift Coupon Valued 200 I Cash Currency Valued 50 </code></pre> <p><img src="https://i.stack.imgur.com/1ZxQH.jpg" alt="enter image description here"></p> <p>I am inserting new payment records using the “InsertOnSubmit” function of ORM. The following code is working fine. However, if I the company is introducing a new payment component using credit card, I need to make changes to my “Payment” domain class. How do I make the payment class <strong>Open for Extension and Closed for Changes</strong> still using <strong>ORM</strong>?</p> <p>Note: The Payment class has <strong>behaviors</strong> (E.g. GetTotalAmountCollected). I am trying to make the "Payment" class to satisfy OCP.</p> <p>Note: There is a specific <strong>behavior</strong> for Coupon type. Is the Coupon issued date is less than 1/1/2000, it should not be used in calculation for Total Amount (i.e, CouponValue should be zero). Refer <a href="https://stackoverflow.com/questions/11435320/refactoring-code-using-strategy-pattern">Refactoring code using Strategy Pattern</a> also.</p> <p>Note: I am using <strong>.Net 4.0</strong></p> <p>Reference: </p> <ol> <li><a href="https://stackoverflow.com/questions/1174394/getting-an-error-when-using-objectcontext-addobject-with-entity-framework">Getting an error when using ObjectContext.AddObject with Entity Framework</a></li> <li><a href="https://stackoverflow.com/questions/11435320/refactoring-code-using-strategy-pattern">Refactoring code using Strategy Pattern</a></li> <li><a href="https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance">Prefer composition over inheritance?</a> </li> <li><a href="https://stackoverflow.com/questions/5446316/ef-4-1-code-first-vs-model-database-first">Code-first vs Model/Database-first</a></li> <li><a href="https://stackoverflow.com/questions/1706056/strategy-pattern-and-dependency-injection-using-unity">Strategy Pattern and Dependency Injection using Unity</a></li> <li><a href="https://stackoverflow.com/questions/984050/c-sharp-strategy-design-pattern-by-delegate-vs-oop">C# Strategy Design Pattern by Delegate vs OOP</a></li> <li><a href="https://stackoverflow.com/questions/3449634/how-to-use-the-strategy-pattern-with-c">How to use the Strategy Pattern with C#?</a></li> <li>Inheritance with EF Code First: Part 2 – Table per Type (TPT) <a href="http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx</a></li> </ol> <p>C# Code:</p> <pre><code>public class PaymentAppService { public RepositoryLayer.ILijosPaymentRepository Repository { get; set; } public void MakePayment() { DBML_Project.Payment paymentEntity = new DBML_Project.Payment(); paymentEntity.PaymentID = 1; paymentEntity.PaymentType = "PurchaseP"; DBML_Project.CashPayment cashObj = new DBML_Project.CashPayment(); cashObj.CashPaymentID = 1; cashObj.CurrencyNumber = 123; cashObj.CurrencyValue = 100; DBML_Project.GiftCouponPayment giftCouponObj = new DBML_Project.GiftCouponPayment(); giftCouponObj.GiftCouponPaymentID = 1; giftCouponObj.CouponValue = 200; giftCouponObj.CouponNumber = 124; paymentEntity.CashPayments = new System.Data.Linq.EntitySet&lt;DBML_Project.CashPayment&gt;(); paymentEntity.CashPayments.Add(cashObj); paymentEntity.GiftCouponPayments = new System.Data.Linq.EntitySet&lt;DBML_Project.GiftCouponPayment&gt;(); paymentEntity.GiftCouponPayments.Add(giftCouponObj); Repository.InsertEntity(paymentEntity); Repository.SubmitChanges(); } } </code></pre> <p>Repository:</p> <pre><code>public class LijosPaymentRepository : ILijosPaymentRepository { public System.Data.Linq.DataContext MyDataContext { get; set; } public void InsertEntity(DBML_Project.Payment payment) { //Insert the entity MyDataContext.GetTable&lt;DBML_Project.Payment&gt;().InsertOnSubmit(payment); } public void SubmitChanges() { MyDataContext.SubmitChanges(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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