我应该将集合公开为一种方法吗? [英] Should I expose a collection as a method.

查看:114
本文介绍了我应该将集合公开为一种方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我一直在写一个使用泛型的提供程序,以允许将简单类的xml序列化以存储在中低端网站上.

该系统运行良好,但是当我在项目上使用FXcop时,我收到警告:方法GetAllItems()
上应使用属性代替Get/Set方法"
现在GetAllItems()返回一个集合,该集合是使用提供程序搜索我的泛型类型的唯一方法.

如下.....

Hi all,

I''ve been writing a provider that uses generics to allow for xml serialization of simple classes for storage on low to medium use websites.

The system works great but when I used FXcop on my project I received the warning:"Properties should be used instead of Get/Set methods " on my method GetAllItems()

Now GetAllItems() returns a collection that acts as the only means to search for my generic type with my provider.

as follows.....

XmlProvider<Person> xmlProvider = new XmlProvider<Person>(path);
var q = from item in xmlProvider.GetAllItems() where item.Name.Equals("James South") select item;
Person p = q.FirstOrDefault();



那是可以接受的做法还是有更好的方法?我想念一些简单的东西吗?

非常感谢吗?



Is that acceptable practice or is there a better method? Am I missing something simple?

Many thanks?

推荐答案

我没有发现任何错. FxCop尽力而为,但根据您的使用情况并不总是正确的,它只是试图让您评估设计.我相信它会被标记,因为Get in名称.

当然,您也可以使用属性来实现,这对于框架或API来说是更好的设计

public IEnumerable Items{...}
I don''t see anything wrong with this. FxCop does it''s best but it isn''t always correct based on your usage, it''s just trying to get you to evaluate the design. I beleive it is being flagged because the Get in the name.

Of course you could also implement this with a property, which would be a better design for a framework or API

public IEnumerable Items{...}


如果您依靠FXCop来验证您的代码,那么您将大失所望.仅在此提供您可以选择遵循或忽略的准则,具体取决于您自己的代码.

如果要避免警告,可以始终在Linq语句之外检索列表,而改用检索到的列表对象.
If you''re relying on FXCop to validate your code, you''re in for a big let-down. It''s only there to provide guidelines that you can choose to follow or ignore, depending on your own code.

If you wanted to avoid the warning, you could always retrieve the list outside your Linq statement, and use the retrieved list object instead.


这篇关于我应该将集合公开为一种方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆