返回泄漏实现细节的委托
我有一个名为 MapBuilder
的类,它内部使用 Dictionary
该类用于快速构建将被代理的属性的映射。 该类看起来像这样::
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;
}
所以用户会像这样使用它::
var map= new MapBuilder<MyClass>()
.Add(x => x.Name)
.Add(x => x.Id)
.Add(x => x.Active)
.Compile()
这将构建一个封装了 Name、Id、Active 3 个属性的映射。问题是 Map 委托现在可以向最终用户泄漏实现细节,因为他们可以观察到该方法是 Dictionary
我可以将其包装在匿名方法中,但当可以进行方法组转换时,我倾向于认为这种形式很糟糕。
I have a class called MapBuilder<T>
which internally uses a Dictionary<PropertyInfo,string>
The class is used to quickly build up a mapping of which properties will be proxied.
The class looks like 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 a user would use it like so::
var map= new MapBuilder<MyClass>()
.Add(x => x.Name)
.Add(x => x.Id)
.Add(x => x.Active)
.Compile()
Which would build a map that encapsulates the 3 properties of Name,Id,Active. The problem is that the Map
delegate can now leak implementation detail to an end user because they can observe the method to be the TryGetValue
method of a Dictionary<PropertyInfo,string>
and the target will be the private dictionary. Would you consider this a code smell?
I can wrap this in an anonymous method, but I tend to consider that bad form when a method group conversion is possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看
Map
委托的目标和方法所涉及的工作量与反映MapBuilder
类本身的字段的工作量大致相同;无论哪种方式,调用者都可以发现私有Dictionary
实例。从 MapBuilder 类的角度来看,我不会担心它。反思私有字段肯定会产生代码味道:但这不是你的责任,而是你的类的用户的责任。
The level of effort involved in looking at the target and method of your
Map
delegate is about the same as reflecting over the fields of theMapBuilder
class itself; either way, the caller can discover the privateDictionary
instance.I wouldn't worry about it from the point of view of the
MapBuilder
class. Reflecting over private fields would definitely be a code smell: but then it's not your responsibility, but the responsibility of the user of your class.