c# - Programming model - Manager + Tools -
i have 1 question regarding model descried bellow:
a manager class holds tools , them... every tool something...
every tool should register (in static method) manager (manager can contain static field of tools) when manger starts should able see tools.
what best practice ?
if have differently. have interface tool, itool
contains methods common tools dojob()
or whatever feel appropriate.
then design manager class accepts list of itool
via constructor, , have method enables add more tools @ design time.
interface itool { dojob(); } class manager { public manager(ienumberable<itool> tools); public addtool(itool tool); }
edit: although don't understand requirements completely, think have feeling trying do.
you said assume itool
s private or have been loaded before manager, don't know why might cause problem unless classes implement itool
private in assembly different assembly manager
, if case please read on.
if project big or medium-to-big suggest use mef or ninject di container.
if have small or small-to-medium project wouldn't let itool
s or manager
register them self because violates single responsibility principle.
instead use interface called itoolrepository
- kind of repository design pattern -, let me show you:
assembly itool
implemented:
interface itool { dojob(); } class hammer : itool { // impl details } interface itoolrepository { ienumerable<itool> gettools(); } class toolsrepository : itoolrepository { ienumerable<itool> gettools() { // return itool implementations assembly using //any method like, whether database, web service, or reflection, etc .. } } static repositoryfactory { itoolrepository createrepository() { // returns concrete repository }; }
assembly manager
exists:
class manager { public manager(itoolrepository repository); public manager(ienumberable<itool> tools); public addtool(itool tool); }
now when want create instance of manager
call repositoryfactory.createrepository()
method other assembly , inject result manager
this:
new manager(repositoryfactory.createrepository());
this enhances testability, loose coupling , every class supposed do, no more.
Comments
Post a Comment