c# - Is it a code smell to return a delegate that leaks implementation details? -
i have class called mapbuilder<t>
internally uses dictionary<propertyinfo,string>
class used build mapping of properties proxied. class looks this::
public class mapbuilder<t>{ private dictionary<propertyinfo, string> m_map = new dictionary<propertyinfo,string>(); public mapbuilder<t> add<tproperty>(expression<func<t, tproperty>> property){ argumentvalidator.assertisnotnull(()=>property); var propertyinfo = reflect.property<t>.infoof(property); m_map.add(propertyinfo, propertyinfo.name); return this; } public mapbuilder<t> add<tproperty>(expression<func<t, tproperty>> property,string columnname){ argumentvalidator.assertisnotnull(() => property); argumentvalidator.assertisnotnull(() => columnname); var propertyinfo = reflect.property<t>.infoof(property); m_map.add(propertyinfo, columnname); return this; } public map compile(){ return m_map.trygetvalue; }
so user use so::
var map= new mapbuilder<myclass>() .add(x => x.name) .add(x => x.id) .add(x => x.active) .compile()
which build map encapsulates 3 properties of name,id,active. problem map
delegate can leak implementation detail end user because can observe method trygetvalue
method of dictionary<propertyinfo,string>
, target private dictionary. consider code smell?
i can wrap in anonymous method, tend consider bad form when method group conversion possible.
the level of effort involved in looking @ target , method of map
delegate same reflecting on fields of mapbuilder
class itself; either way, caller can discover private dictionary
instance.
i wouldn't worry point of view of mapbuilder
class. reflecting on private fields code smell: it's not responsibility, responsibility of user of class.
Comments
Post a Comment