泛型是解决方法重载的好方法吗?

发布于 2024-10-06 12:39:06 字数 445 浏览 0 评论 0原文

两分钟前,我想出了一个想法,用接口选择重载方法。 我们目前使用 Microsoft 应用程序块中的 UIP。 他们在每个控制器中使用带有一个对象参数的 OnEnterTask-Method。 但这会导致类型检查混乱。

所以我想出了使用接口来选择正确方法的想法:

interface IAcceptTaskArguments<TInputArguments> where TInputArguments : InputArguments
{
   void OnEnterTask(TInputArguments arguments);
}

做这样的事情以避免类型检查混乱是一个好习惯吗?

谢谢你们。

two minutes ago, i came up with an idea to select overloaded methods with an interface.
we currently using UIP from Microsoft application blocks.
they use an OnEnterTask-Method with one object parameter in each controller.
but this leads to an type-checking mess.

so i came up with the idea to use an interface to select the correct method:

interface IAcceptTaskArguments<TInputArguments> where TInputArguments : InputArguments
{
   void OnEnterTask(TInputArguments arguments);
}

is it a good practice to do something like this to avoid that typechecking-mess?

thank you guys.

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

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

发布评论

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

评论(1

冬天旳寂寞 2024-10-13 12:39:06

我更喜欢使用泛型而不是方法重载。当需要重载时,我更喜欢传递具有所有参数的类(或接口)的实例,然后检查每个参数的默认自定义值,如果该值是默认值,则意味着没有值给出了这样的参数。

Microsoft Framework 4.0版本中,使用泛型变得更加容易,因为有一些名为dynamic的关键字,您可以通过它从泛型方法调用静态方法:

namespace Layer.Logic
{
    public class EntityObjectCommands : LogicContextBase, IEntityObjectCommands
    {
        public Tresult Get<Tcommand, Tresult>(Tcommand command) where Tcommand : ICommandGet
        {
            Tresult result = default(Tresult);
            DBEntityObjectCommands dbfactory = new DBEntityObjectCommands(GetDataServiceParam(dbserver));
            result = dbfactory.Get<Tcommand, Tresult>(command);     
            return result;
        }
    }
}
namespace Layer.Data
{
    public class DBEntityObjectCommands : IEntityObjectCommands
    {
        public Tresult Get<Tcommand, Tresult>(Tcommand command) where Tcommand : ICommandGet
        {
          Tresult result = default(Tresult);
          OleDbCommandInfo commandInfo = DBCommandProvider<Tcommand>.Get(command);

          //-- implement logic to use [commandInfo] ...........

          return result;
        }       
    }   
    public static class DBCommandProvider<Tcommand> where Tcommand : ICommand
    {
        public static OleDbCommandInfo Get(Tcommand command)
        {   
            return DBCommands.Get( (dynamic)command );
        }
    }   
}

I prefer to use generics versus method overloading. When overloading is required I prefer to pass an instance of a class (or an interface) having all the parameters, then checking for default customized values for each parameter, if the value is the default it means no value was given for such parameter.

With version 4.0 of Microsoft's Framework, it is easier to use generics because, there is some keyword named dynamic by which you can call a static method from generic methods:

namespace Layer.Logic
{
    public class EntityObjectCommands : LogicContextBase, IEntityObjectCommands
    {
        public Tresult Get<Tcommand, Tresult>(Tcommand command) where Tcommand : ICommandGet
        {
            Tresult result = default(Tresult);
            DBEntityObjectCommands dbfactory = new DBEntityObjectCommands(GetDataServiceParam(dbserver));
            result = dbfactory.Get<Tcommand, Tresult>(command);     
            return result;
        }
    }
}
namespace Layer.Data
{
    public class DBEntityObjectCommands : IEntityObjectCommands
    {
        public Tresult Get<Tcommand, Tresult>(Tcommand command) where Tcommand : ICommandGet
        {
          Tresult result = default(Tresult);
          OleDbCommandInfo commandInfo = DBCommandProvider<Tcommand>.Get(command);

          //-- implement logic to use [commandInfo] ...........

          return result;
        }       
    }   
    public static class DBCommandProvider<Tcommand> where Tcommand : ICommand
    {
        public static OleDbCommandInfo Get(Tcommand command)
        {   
            return DBCommands.Get( (dynamic)command );
        }
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文