Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding .Net Generics - Bank Domain
    text
    copied!<p>This is an attempt to learn Generics (with .Net 4.0). I have been programming for about 4.5 years. Till now I have not used Generics in real time projects. All the time what I have been doing is reading some article about generics and try to understand it. The problem is – most of them try to explains various syntax available with Generics. They explain with examples such as Square, Circle and shapes.</p> <p>Now I have got a chance to design a small application. I would like to use Generics there. [I do see good chances of Generics being a good candidate in my new project]</p> <p>What I have come up with now is an example from Bank domain with the intention of understanding Generics. I am trying to understand the following 4.</p> <p>1) Generic classes</p> <p>2) Generic Methods</p> <p>3) Generic Interfaces</p> <p>4) Generic Delegates</p> <p>EDIT: Operations that are type-independant are good candidates for generics. This is the one of the biggest points I missed in my following example.</p> <p>I have created an example for “Generic classes”. Could you please help with simple examples for other three items with the Bank domain?</p> <p>Note: While using Generic class, I came to know that it helped in <strong>Open-Closed Principle</strong>. Even if I add new account type, the generic class need to change. The changing logic (interest calculation) goes inside the specific class.</p> <p>Note: In the following, the syntax may not be correct as it typed it without a Visual Studio. But the concept holds good.</p> <p><strong>EDIT:</strong> Will "AccountManager" be a more better name for "BankAccount" class based on its role? Is it any kind of anti-pattern?</p> <p><strong>Generic Class</strong> - Example with Bank Domain</p> <pre><code>Public Interface IBankAccount { Public int Interest; Public Int DepositedAmount; Public int DurationInMonth; } Public class FixedAccount: IbankAccount { Public int Interest { Get { Return (DurationInMonth*0.5) } } Public Int DepositedAmount {get;set}; Public int DurationInMonth {get;set}; } Public class SavingsAccount: IbankAccount { Public int Interest { Get { Return ((DurationInMonth/2)*0.1) } } Public Int DepositedAmount {get;set}; Public int DurationInMonth {get;set}; } Public Class BankAccount&lt;T&gt; Where T: IbankAccount { T account = new T(); Public void CreateAccount(int duration, int amount) { account. DurationInMonth = duration; account. DepositedAmount = amont; int interestVal = account. Interest; SaveToDatabase (T); } } </code></pre> <p>READING:</p> <ol> <li><p><a href="https://stackoverflow.com/questions/799369/when-is-it-appropriate-to-use-generics-versus-inheritance">When is it Appropriate to use Generics Versus Inheritance?</a></p></li> <li><p><a href="https://stackoverflow.com/questions/2642598/generics-vs-inheritance-when-no-collection-classes-are-involved">Generics vs inheritance (when no collection classes are involved)</a></p></li> <li><p><a href="https://codereview.stackexchange.com/questions/8797/how-to-make-sure-that-this-code-conforms-to-open-close-principle">https://codereview.stackexchange.com/questions/8797/how-to-make-sure-that-this-code-conforms-to-open-close-principle</a></p></li> <li><p><a href="https://stackoverflow.com/questions/7876462/a-factory-pattern-that-will-satisfy-the-open-closed-principle">A Factory Pattern that will satisfy the Open/Closed Principle?</a></p></li> <li><p><a href="https://stackoverflow.com/questions/573584/im-having-some-trouble-with-generics-and-casting-in-c-sharp">I&#39;m having some trouble with generics and casting in C#</a></p></li> <li><p>Deciding When and Where to Use Generics <a href="http://en.csharp-online.net/CSharp_Generics_Recipes" rel="nofollow noreferrer">http://en.csharp-online.net/CSharp_Generics_Recipes</a>—Deciding_When_and_Where_to_Use_Generics_Problem</p></li> <li><p><a href="https://stackoverflow.com/questions/2321788/code-reuse-through-generics-vs-polymorphism">Code reuse through generics vs polymorphism</a></p></li> <li><p><a href="https://stackoverflow.com/questions/634025/polymorphism-and-type-safety-in-parallel-inheritance-chains">Polymorphism AND type safety in parallel inheritance chains</a></p></li> <li><p><a href="https://stackoverflow.com/questions/4136596/extending-using-c-sharp-generics">Extending using C# generics?</a></p></li> <li><p><a href="https://stackoverflow.com/questions/1814851/c-sharp-generics-and-polymorphism-an-oxymoron">C# Generics and polymorphism: an oxymoron?</a></p></li> </ol>
 

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