返回泄漏实现细节的委托

发布于 2024-10-02 10:26:43 字数 1490 浏览 1 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞慾 2024-10-09 10:26:43

查看 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 the MapBuilder class itself; either way, the caller can discover the private Dictionary 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文