asp.net mvc:在使用LINQ to Entities时如何为模型创建自定义绑定

时间:2022-11-30 16:27:28

I've got a number of tables in my db that share common cols: modified by, modified date, etc. Not every table has these cols. We're using LINQ to Enties to generate the

我的数据库中有许多共享常见列的表:修改日期,修改日期等。并非每个表都有这些列。我们正在使用LINQ来实现生成

I'd like to create a custom binder class that can handle the automatic binding of these fields. Is there a way to do this without having a custom binding class for each entity class?

我想创建一个自定义绑定器类,可以处理这些字段的自动绑定。有没有办法在没有每个实体类的自定义绑定类的情况下执行此操作?

Here's the code I have:

这是我的代码:

In global.asax.cs, Application_Start():

在global.asax.cs中,Application_Start():

  ModelBinders.Binders.Add(typeof(Foo),new FooBinder());

Then in FooBinder.cs:

然后在FooBinder.cs中:

public override Object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
  var obj = (Foo)base.BindModel(controllerContext, bindingContext);
  var user = controllerContext.HttpContext.User.Identity;
  obj.modified_by = user.Name;
  obj.modified_date = DateTime.Now;
  return obj;
}

Is there a way to generalize this so it can handle multiple types?

有没有办法概括这个,以便它可以处理多种类型?

2 个解决方案

#1


1  

We do this in the repository, not in the binder. We have an interface with the common fields (Modified on). We implement the interface in partial classes which we codegen for our entities using a T4 template.

我们在存储库中执行此操作,而不是在绑定器中执我们有一个带有公共字段的接口(已修改)。我们在部分类中实现接口,我们使用T4模板为我们的实体编译。

#2


0  

You can use reflection to set fields by name, so that you can do foreach over property names. This is not a big deal since model binder uses reflection itself to get to the properties, anyway. Then you can just register binder for each of the "common" types - which is also OK to do using reflection, for example by checking that entity has all fields required present - so it works even if you add new such entities.

您可以使用反射按名称设置字段,以便您可以对属性名称进行预处理。这不是什么大问题,因为无论如何,模型绑定器使用反射本身来获取属性。然后你可以为每个“常见”类型注册binder - 这也可以使用反射,例如通过检查实体是否存在所需的所有字段 - 所以即使你添加新的这样的实体它也可以工作。

Just have your binder call default one and then update the fields.

只需将您的活页夹调用默认为一个,然后更新字段。

#1


1  

We do this in the repository, not in the binder. We have an interface with the common fields (Modified on). We implement the interface in partial classes which we codegen for our entities using a T4 template.

我们在存储库中执行此操作,而不是在绑定器中执我们有一个带有公共字段的接口(已修改)。我们在部分类中实现接口,我们使用T4模板为我们的实体编译。

#2


0  

You can use reflection to set fields by name, so that you can do foreach over property names. This is not a big deal since model binder uses reflection itself to get to the properties, anyway. Then you can just register binder for each of the "common" types - which is also OK to do using reflection, for example by checking that entity has all fields required present - so it works even if you add new such entities.

您可以使用反射按名称设置字段,以便您可以对属性名称进行预处理。这不是什么大问题,因为无论如何,模型绑定器使用反射本身来获取属性。然后你可以为每个“常见”类型注册binder - 这也可以使用反射,例如通过检查实体是否存在所需的所有字段 - 所以即使你添加新的这样的实体它也可以工作。

Just have your binder call default one and then update the fields.

只需将您的活页夹调用默认为一个,然后更新字段。