dependency injection - How to practice SOLID principle of OOP design? -
i'm new solid principle understand it. main problem having hard time designing classes follow solid specially dependency inversion. it's easy write whole logic procedural pattern rather use solid.
for example:
let's creating attendance monitoring system, , have logic(or procedure) scan employee fingerprint, it's id, determine whether or not it's valid or not, determine time in, write login info database, , show if it's successful or not.
it easy write in procedural manner bunch of 'if else', loop , switch. in future i'm gonna suffer 'code debt'.
if applying solid principle here. know need have kind of object 'attendanceserviceclass' has method 'scanemployeeid()', 'processthislogin()' or 'isitsucessful()'. , know class has dependency repository, userinfo, , other objects.
basically problem analyzing design of class , dependencies
what step step way of analyzing design of class?
sorry english.
sometimes it's easy write whole logic procedural pattern rather use solid
i cannot agree more, easier programmer handle code in procedural pattern. makes oop hard programmer accustomed procedural programming.
however found easier write general interface , consumer first rather breaking interface designed smaller modules. is, kind of test first development -> red, green, refactor
practice. (please note that, if want achieve neat design
, consider following tdd instead guide. guide small section of doing tdd)
say want create serviceattendance
scanemployeeid
. has interface (please notice example in c# naming):
public interface iserviceattendance{ bool scanemployeeid(); }
please noted decide method return bool instead of void determine success/failure operation. please notice consumer example below not implement di because want show how consume it. in consumer, can have:
public void consumeserviceattendance(){ iserviceattendance attendance = resolve<iserviceattendance>(); if(attendance.scanemployeeid()){ // } }
this concludes consumer. move implementation. can develop using procedural programming , got monolithic code block. can state implementation pseu-like statement.
public class serviceattendance : iserviceattendance{ public bool scanemployeeid(){ bool isempvalid = false; // 1 scan employee id // 2 validate login // 3 if valid, create login session // 4 notify user return isempvalid; } }
now have 4 steps done in 1 operation. principal is, not on 3 facade process in 1 method can refactor 3 , 4 1 process. have
public class serviceattendance : iserviceattendance{ public bool scanemployeeid(){ bool isempvalid = false; // 1 scan employee id // 2 validate login // 3 if valid, create login session , notify user return isempvalid; } }
this, have 3 main operation. can analyze whether need create smaller module or not breaking down operation. want break second operation. can get:
// 2 validate login // 2.1 check if employee id matches format policy // 2.2 check if employee id exists in repository // 2.3 check if employee id valid access module
the breakdown operation obvious enough break second module smaller module. 2.2
, 2.3
, need smaller module injected. because need dependency repository, need injected. same case apply operation step 1 scan employee id
, because need dependency fingerprint scanner, scanner handler must implemented in separated module.
we can breakdown operation, can in 2.1
:
// 2.1 check if employee id matches format policy // 2.1.1 employee id must match length // 2.1.2 employee id must has format emp#####
now unsure if 2.1.1
, 2.1.2
need broken down 2 separated modules, decide. , got interfaces, can start implementation. expect throw exceptions
during validation or need pass custom class handle error messages.
Comments
Post a Comment