- GB article G real world example w`‡q SOLID principle explain Ki‡ev|
- SOLID principles n‡”Q cuvPwU programming principles hvnv mKj well designed application Gi foundation wnmv‡e consider Kiv nq|
- GB principle mg~n e¨envi K‡i easy to extend I maintainable application ‰Zix Ki‡Z e¨eüZ nq|
- GLb Avgiv principle mg~n wb‡q Av‡jvPbv Ki‡ev|
- cÖ_‡g Avgiv D³ principle mg~‡ni acronym define Ki‡ev:
S: Single Responsibility Principle
-- GKwU class/Modules/Functions change Gi Rb¨ GKwU
gvÎ reason _vK‡e.
O: Open/Closed Principle – Classes/Functions/Modules mg~n extension
Gi Rb¨ open _vK‡e wKš‘ modification
Gi Rb¨ closed _vK‡e.
L: Liskov Substitution Principle -- Derived class ‡K base class Gi cwie‡Z© e¨envi Kiv
hv‡e|
I: Interface Segregation Principle –
Larger interface Gi AcÖ‡qvRbxq member
mg~n implement
Kivi Rb¨ Clients †K force Kiv
hv‡e bv ZvB larger interface ‡K †f‡½ Smaller I Consistent interface G fvM Ki‡Z
n‡e|
D: Dependency Inversion Principle – Top level class mg~n Ges bottom level class mg~n G‡K Ac‡ii
Dci dependent _v‡K bv| Df‡q Abstraction Gi gva¨‡g G‡K Ac‡ii mv‡_ Connected nq
A_©vr Top level I
bottom level class mg~n
Abstraction/Interface Gi
Dci dependent _vK‡e|
- We will be working with a Web application that searches a database for documents that match very simple criteria:
- Avgiv GKwU Web application Gi search method wb‡q Av‡jvPbv Ki‡ev |
- GB search method G Avgiv data search Kivi Rb¨ database G search Ki‡ev| GB search method wb‡¤œv³ criteria mg~n Øviv database G search Ki‡e-
- First name,
- Last name and
- Account number hvnv Aek¨B user enters character Øviv Start n‡e|
- wb‡¤œ search screen ‡`qv n‡jv-
Search button Gi
code wb‡¤œ
†`qv n‡jv (Bnv refactoring version bv)-
protected void BtnSearch_Click(object sender,
EventArgs e)
{
var
sbCriteria = new StringBuilder();
using (var
db = new SolidDemoEntities())
{
var
query = db.Documents.AsQueryable();
if (!String.IsNullOrWhiteSpace(TxbAccount.Text))
{
query
= query.Where(d => d.Account.StartsWith(TxbAccount.Text));
sbCriteria.AppendFormat("Account
starts with '{0}' AND ", TxbAccount.Text);
}
if (!String.IsNullOrWhiteSpace(TxbLastName.Text))
{
query
= query.Where(d => d.LastName.StartsWith(TxbLastName.Text));
sbCriteria.AppendFormat("Last
Name starts with '{0}' AND ", TxbLastName.Text);
}
if (!String.IsNullOrWhiteSpace(TxbFirstName.Text))
{
query
= query.Where(d => d.FirstName.StartsWith(TxbFirstName.Text));
sbCriteria.AppendFormat("First
Name starts with '{0}' AND ", TxbFirstName.Text);
}
if (sbCriteria.Length
> 5)
sbCriteria.Remove(sbCriteria.Length
- 5, 5);
else
sbCriteria.Append("--
all records --");
LblCriteria.Text
= sbCriteria.ToString();
LvwResults.DataSource
= query.ToArray();
LvwResults.DataBind();
}
}
- Dc‡iv³ coding style Single Responsibility Principle ‡K violate K‡i KviY-
- mKj responsibility mg~n GKwU button click Gi against G handle Kiv nq-
- wb‡¤œ handled responsibility mg~n cÖ`Ë n‡jv-
- User's input parse K‡i|
- Database connection Manage K‡i|
- Database query build K‡i (string AvKv‡i Ges LINQ Expression AvKv‡i)
- Query Execute K‡i|
- Result mg~n form G Bind K‡i|
- GgbwK Bnv‡Z †Kvb error-handling bvB|
- Dc‡iv³ approach bad ‡Kb?
- mKj code GKwU place G ivLv n‡q‡Q
- hZÿY GKwU application ‡QvU _v‡K ZZÿY Bnv mgm¨v bv|
- Application hw` Large nq Ges cÖwZwU pages G multiple actions _v‡K Z‡e GB approach Gi wKQz drawbacks _v‡K|
- wb‡¤œ KwZcq drawbacks D‡jøL Kiv n‡jv-
ü Application Gi unit-test possible bv|
ü Bnv
ZLbB KvR Ki‡e hLb ASP.NET context G
run Ki‡e|
ü Bnv
database presence expect Ki‡e|
ü †Kvb
can reuse Kiv
hv‡e bv|
- d‡j Avgiv hw` more features add Ki‡Z PvB (D`vniY ¯^iƒc audit Gi Rb¨ search log manage Kiv) Z‡e D³ logging Gi Rb¨ †h coding Ki‡Z n‡e ZvnvI same method G Ki‡Z n‡e|
- †m‡ÿ‡Î existing trusted code modify Ki‡Z n‡e|
- Existing trusted code modify Ki‡j wb‡¤œv³ Problem mg~n raise Ki‡Z cv‡i-
- Existing code G bug introduce n‡Z cv‡i|
- Unit Testing complex n‡q hv‡e|
- Code maintainability lose Ki‡e|
- Multiple logic handle Kivi Kvi‡Y method Gi complexity raise Ki‡e|
- Readability K‡g hv‡e|
- Reusability K‡g hv‡e|
- GQvov Av‡iv A‡bK ..
- Dc‡iv³ Problem mg~n Overcome Kivi Rb¨B Avgiv SOLID Principle use Ki‡ev|
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন