vb.net - Creating 2 DLLs sharing an Interface on Visual Basic, is it possible/good idea? -
i'm designing visual basic application on uses 1 dll created me (both on visual studio). in app, there's object instantiates 1 class defined dll.
friend withevents robot new dll1class
there new needs on project object should able instantiate 1 class different new dll. since designing new dll, want make change transparent app, same me do:
friend withevents robot new dll1class
or
friend withevents robot new dll2class
my idea create interface ioperations
, make 2 dlls implement it, there no problems on code.
since need instantiate object during runtime, question is, class should create with?
friend withevents robot ¿?¿?¿? ' ... if xxxxx robot = new dllxclass end if
i don't want use as object
because during programming not see list of functions when write robot.
, code , dll functions complex accept this.
is there way can define object implements interface? similar idea:
friend withevents robot classx "that implements ioperations"
if think there better way afford problem it's ok me! since think question make: yes, necessary use dlls because need send them external people use dll on own applications.
you need declare variable interface rather 1 of concrete classes, instance:
friend withevents robot ioperations
now, robot
variable can reference object of type happens implement interface. next, need conditionally set robot
variable correct type of object depending on situation. instance, in constructor, this:
if usingdll1 robot = new dll1class() else robot = new dll2class() end if
as long both of classes implement iopertions
interface, compile , work correctly.
rant
now it's time di rant. if don't care think, skip part :)
having class create own dependencies bad practice. project grows, become more , more complicated , lead spaghetti-code. way avoid use dependency-injection (di). instance, in case, design class this:
public class myclass public sub new(robot ioperations) me.robot = robot end sub friend withevents robot ioperations end class
now, rather creating it's own robot object, class requests robot given it. in way, it's dependency (the robot) injected it.
think of way: if have car class, should create it's own engine object? in real life, car create it's own engine, or given 1 car factory? creating car can create it's own engine complicated , silly, having classes create own dependencies complicated , silly.
Comments
Post a Comment