通用方法 - 寻求设计建议

发布于 2024-12-08 03:25:47 字数 831 浏览 0 评论 0原文

我有一个 util 类 (C#),其中有一个静态方法,该方法采用某种类型的对象并启动 Web 服务来获取更多数据。我想支持其他对象类型并且不复制代码,即添加多个类似的方法,我认为通用路线是最好的。

例如,假设我有:

public static void GetData(Building building)
{
    var webClient = new WebClient();
    var wrapper = new WrapperClass(building);

    if (building.Distance.HasValue)
    {
        structure = new Structure((decimal)building.Length.Value, (decimal)building.Height.Value);
    }

    ... // and so on ...

而不是像这样创建另一个方法:

public static void GetDataForBridge(Bridge bridge)
{
    var webClient = new WebClient();
    var wrapper = new WrapperClass(bridge);

    if (bridge.Distance.HasValue)
    {
        structure = new Structure((decimal)bridge.Length.Value, (decimal)bridge.Height.Value);
    }

    // ...

我不知道如何使用泛型来做到这一点。有人可以给我一些提示或建议吗?

I have a util class (C#) where I have a static method that takes a certain type of object and brings up a web service to get further data. I would like to support other object types and to not replicate the code, i.e. add multiple similar methods, I am thinking the Generic route would be best.

For example, let's say I have:

public static void GetData(Building building)
{
    var webClient = new WebClient();
    var wrapper = new WrapperClass(building);

    if (building.Distance.HasValue)
    {
        structure = new Structure((decimal)building.Length.Value, (decimal)building.Height.Value);
    }

    ... // and so on ...

instead of creating another method(s) like so:

public static void GetDataForBridge(Bridge bridge)
{
    var webClient = new WebClient();
    var wrapper = new WrapperClass(bridge);

    if (bridge.Distance.HasValue)
    {
        structure = new Structure((decimal)bridge.Length.Value, (decimal)bridge.Height.Value);
    }

    // ...

I am not sure how to do this using Generics. Can anyone please give me some tips or advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

穿越时光隧道 2024-12-15 03:25:48

在这种情况下,Bridge 和 Building 必须实现相同的接口,例如 IObjectWithHeightAndWidth。然后,您可以将此接口指定为对泛型方法的类型参数的约束。

(或者,这些类可以共享一个公共基类,而不是公共接口。)

不过,正如其他发帖者所指出的,您可能根本不需要泛型。仅当您随后需要将对象作为BridgeBuilding 进行强类型引用时,才需要泛型——例如,如果您需要调用另一个泛型方法在我们正在讨论的方法中。

In this case, Bridge and Building would have to implement the same interface, say, IObjectWithHeightAndWidth. You'd then specify this interface as a constraint on the type parameter of your generic method.

(Or, instead of a common interface, the classes could share a common base class.)

As other posters have pointed out, though, you might not need generics at all. You only would need generics if you subsequently need to have a strongly-typed reference to the object as a Bridge or Building -- for example if you need to call another generic method in the method we're discussing.

放低过去 2024-12-15 03:25:47

在您的情况下,为什么不直接用更简单的 IHaveDistanceLengthAndHeight 接口替换函数声明中的 Building 呢?这样你就根本不需要泛型。

interface IHaveDistanceLengthAndHeight
{
    DistanceType Distance { get; }
    DistanceType Height   { get; }
    DistanceType Length   { get; }
}

class Building : IHaveDistanceLengthAndHeight
{
    ...

In your case, why not just replace Building in function declaration with a simpler IHaveDistanceLengthAndHeight interface? This way you don't need generics at all.

interface IHaveDistanceLengthAndHeight
{
    DistanceType Distance { get; }
    DistanceType Height   { get; }
    DistanceType Length   { get; }
}

class Building : IHaveDistanceLengthAndHeight
{
    ...
遇到 2024-12-15 03:25:47

听起来在这种情况下您可能应该使用共享接口而不是泛型。定义一个接口,其中包含距离属性和长度、高度等内容;并让您的桥和 Building 实现它,然后定义一个接受共享接口实例的 GetData() 方法。

public static viod GetData(IHasDimensions thing)
{
    var webClient = new WebClient();
    var wrapper = new WrapperClass(thing);

    if (thing.Distance.HasValue)
    {
        structure = new Structure((decimal)thing.Length.Value, (decimal)thing.Height.Value);
    }
    ...
}

Sounds like you should probably be using a shared interface rather than generics in this case. Define an Interface which contains things like the Distance property and Length, Height, etc; and have your bridge and Building implement it, then define a GetData() method that takes in an instance of the shared interface.

public static viod GetData(IHasDimensions thing)
{
    var webClient = new WebClient();
    var wrapper = new WrapperClass(thing);

    if (thing.Distance.HasValue)
    {
        structure = new Structure((decimal)thing.Length.Value, (decimal)thing.Height.Value);
    }
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文