c# - Which pattern should be used to determine the builder to use for an item? -
c# - Which pattern should be used to determine the builder to use for an item? -
i have builder-director implementation i'm using build item.
public void main(itemkey itemkey) { director adirector = new director(); itembuilderfactory itembuilderfactory = new itembuilderfactory(); var builder = itembuilderfactory.getbuilderfor(itemkey); //<--- how this? adirector.construct(builder, itemkey); console.writeline(builder.item.businessname); console.writeline(builder.item.customername); console.writeline(builder.item.advancedname); } public class director { public void contruct(iitembuilder builder, itemkey itemkey) { builder.createitem(); builder.getbusinessiteminformation(itemkey); builder.getcustomeriteminformation(itemkey); builder.getadvancediteminformation(itemkey); } } public interface iitembuilder { item item { get; } void getbusinessiteminformation(itemkey itemkey); void getcustomeriteminformation(itemkey itemkey); void getadvancediteminformation(itemkey itemkey); bool canbuild(itemkey itemkey); // don't , think violates srp. forces me retrieve info item, determine if builder can build it, , homecoming flag indicating it. void createitem(); } public class itembuilderfactory { private readonly list<iitembuilder> _builders; public itembuilderfactory(list<iitembuilder> builders) { _builders = builders; } public iitembuilder getbuilderfor(itemkey itemkey) { //what do here? current impl: homecoming _builders.single(builder => builder.canbuild(itemkey)); //huge reddish flag, fancy if } } public class itemkey { public int upc {get;set;} public string itemcode {get;set;} }
i seek avoid using if statements whenever possible, seems time when might have to. how else can "get builder able build item"? i've thought of creating kind of mapping class severely increases cognitive overhead of looking code, , thought maybe else force me in right direction.
you can utilize ioc , dependency injection map item keys iitembuilder implementations - you'd have register itemkey mappings iitembuilders during construction of whatever ioc container decide utilize (ninject, unity, etc).
no matter technique choose, version of if, whether within ioc framework maintain hands clean or if utilize reflection or technique have in question.
c# design-patterns code-review builder
Comments
Post a Comment