两分钟前,我想出了一个用接口选择重载方法的想法。 我们目前正在使用Microsoft应用程序块中的UIP。 他们使用OnEnterTask-Method在每个控制器中有一个对象参数。 但这会导致类型检查混乱。
所以我提出了使用界面来选择正确方法的想法:
interface IAcceptTaskArguments<TInputArguments> where TInputArguments : InputArguments
{
void OnEnterTask(TInputArguments arguments);
}
这样做是为了避免那种类型混乱?这是一个好习惯吗?
谢谢你们。答案 0 :(得分:1)
我更喜欢使用泛型与方法重载。当需要重载时,我更喜欢传递具有所有参数的类(或接口)的实例,然后检查每个参数的默认自定义值,如果值是默认值则表示没有值给出了这样的参数。
使用 4.0的Microsoft框架,使用泛型更容易,因为有一些名为 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 );
}
}
}