ASP.NET webforms中的模型Binder

多年来我做了ASP.NET Web表单开发我被一个专有库所破坏,这让我可以做以下事情:

UpdatetoObject(ControlsCollection,obj)
    UpdateFromObject(ControlsCollection,obj)

概念上,代码做了一些与MVC Model Binder非常相似的东西,即将表单的发布值作为输入,它将填充自定义对象.基本上它使开发人员免于做猴子代码,如

employee.Name = txtName.Text;
employee.dob = DateTime.Parse(txtdob.Text);

等等..

现在,这个专有的库在我参与的新项目中不可用,它是一个Web表单项目.所以我想知道是否有一种方法在Web窗体的上下文中使用System.Web.Mvc.DefaultModelBinder.目标是从域对象和背面实现简单和容易的控制,最好考虑验证注释.
如果不可能,有人可能会指出我的开源解决方案来满足这种需求.我真的不想重写这样的代码.

提前致谢.

解决方法

Sherlock,你会遇到一些试图使用MVC中的ModelBinder的问题,因为它们依赖于ControllerContext.

我在ChangeType,Convert – Converting from one type to another之前回答了类似的问题,但它确实是你在寻找的.

在我的博客上查看此博客文章
ChangeType – Changing the type of a variable in C#

从本质上讲,您将获得一个名为ChangeType< T>的方法.以强类型方式返回您要查找的参数的值,如果参数不存在则返回认值.

现在关于自定义类(主要是DTO类型),如果你不介意使用反射,那么我有一个解决方案,它也将处理大多数自定义类. DtoBinder类在意志结束时提到了很好的工作.

从本质上讲,最后3个代码清单包含了您需要的所有代码,以便处理典型Web应用程序场景中的几乎所有需求.此外,它还具有可扩展性,因此如果您需要实现自己的活页夹,您可以非常简单地执行此操作,并在应用程序的任何位置使用RequestBinder注册活页夹.

因此,如果您不想对某些经常使用的DTO对象使用反射,您可以为该类型实现绑定并注册它,从那时起它将使用您的自定义绑定器.在许多方面,它在概念上类似于MVC ModelBinder.

编辑 –

下面是一个带有一堆类的.cs文件,我过去用它来完成你所需要的.第一个MsPropertyAssignerProvider是您在页面中使用的那个.

您将遍历控件并调用GetPropertyAssigner方法向其传递控件的类型名称.此方法返回一个ObjectPropertyAssigner实例,该实例具有一个名为SetPropertyValue的方法,您可以将对象实例和控件实例传递给它.

internal class MsPropertyAssignerProvider
  {
    private Hashtable propertyAssigners;

    internal MsPropertyAssignerProvider()
    {
      propertyAssigners = new Hashtable();
      RegisterPropertyAssigner(typeof(TextBox).ToString(),new TextBoxValueExtractor());
      RegisterPropertyAssigner(typeof(DropDownList).ToString(),new DropDownListValueExtractor());
      RegisterPropertyAssigner(typeof(Label).ToString(),new LabelValueExtractor());
      RegisterPropertyAssigner(typeof(CheckBox).ToString(),new CheckBoxValueExtractor());
    }

    internal void RegisterPropertyAssigner(string identifier,imsObjectPropertyAssigner assigner)
    {
      if (propertyAssigners.ContainsKey(identifier))
        throw new DuplicatePropertyAssignerRegistrationException(identifier);
      propertyAssigners.Add(identifier,assigner);
    } 

    internal imsObjectPropertyAssigner GetPropertyAssigner(string identifier)
    {
      return (propertyAssigners.ContainsKey(identifier)) ? (imsObjectPropertyAssigner)propertyAssigners[identifier] : null;
    }
  }

随附的课程如下

public interface imsObjectPropertyAssigner
  {
    void SetPropertyValue(object obj,System.Web.UI.Control control); 
  }

  internal abstract class BaseValueExtractor : imsObjectPropertyAssigner
  {
    protected MsReflectionHelper reflectionHelper = new MsReflectionHelper();
    protected string FixStringForNumber(string stringValue)
    {
      if (stringValue.Length == 0)
        return "0";
      else
        return stringValue;
    }
    public abstract void SetPropertyValue(object obj,System.Web.UI.Control control);
  }

  internal class TextBoxValueExtractor : BaseValueExtractor
  {
    public override void SetPropertyValue(object obj,System.Web.UI.Control control)
    {
      TextBox textBox = (TextBox)control;
      PropertyInfo propInfo = reflectionHelper.GetPropertyInfo(obj,control.ID);
      Type propType = propInfo.PropertyType;
      if (propType == typeof(System.String))
        reflectionHelper.SetPropertyValue(obj,control.ID,textBox.Text);
      else if (propType == typeof(system.int16))
        reflectionHelper.SetPropertyValue(obj,Int16.Parse(FixStringForNumber(textBox.Text),System.Globalization.NumberStyles.Currency));
      else if (propType == typeof(system.int32))
        reflectionHelper.SetPropertyValue(obj,Int32.Parse(FixStringForNumber(textBox.Text),System.Globalization.NumberStyles.Currency));
      else if (propType == typeof(system.int64))
        reflectionHelper.SetPropertyValue(obj,Int64.Parse(FixStringForNumber(textBox.Text),System.Globalization.NumberStyles.Currency));
      else if (propType == typeof(System.Double))
        reflectionHelper.SetPropertyValue(obj,Double.Parse(FixStringForNumber(textBox.Text),System.Globalization.NumberStyles.Currency));
      else if (propType == typeof(System.Single))
        reflectionHelper.SetPropertyValue(obj,Single.Parse(FixStringForNumber(textBox.Text),System.Globalization.NumberStyles.Currency));
      else
        reflectionHelper.SetPropertyValue(obj,textBox.Text);
    }
  }

  internal class DropDownListValueExtractor : BaseValueExtractor
  {
    public override void SetPropertyValue(object obj,System.Web.UI.Control control)
    {
      DropDownList dropDownList = (DropDownList)control;
      reflectionHelper.SetPropertyValue(obj,dropDownList.SelectedValue);
    }
  }

  internal class LabelValueExtractor : BaseValueExtractor
  {
    public override void SetPropertyValue(object obj,Control control)
    {
      Label label = (Label)control;
      reflectionHelper.SetPropertyValue(obj,label.Text);
    }
  }

  internal class CheckBoxValueExtractor : BaseValueExtractor
  {
    public override void SetPropertyValue(object obj,Control control)
    {
      CheckBox checkBox = (CheckBox)control;
      reflectionHelper.SetPropertyValue(obj,checkBox.Checked);
    }
  }

对不起,无论我做什么,编辑器都完全搞砸了代码清单.但我希望这会有所帮助.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....