【问题标题】:How can I match two concrete classes by a Property that is generated at runtime - WebAPI?如何通过运行时生成的属性匹配两个具体类 - WebAPI?
【发布时间】:2015-04-12 06:50:58
【问题描述】:

这是我的previous question 的扩展。

我必须将 CorelationID 传递给我的 API 服务并匹配部门和员工记录。

我从 Javascript 传递的数组如下

EmployeesAPIController.cs

using System.Web.Http;

    namespace EmployeeService
    {
        [RoutePrefix("Employee")]
        public class EmployeesAPIController : ApiController
        {  
            [HttpPost]
            [Route("InsertEmployeeDepartment")]
            public EmployeeDepartment InsertEmployeeAndDepartment(object employeedepartment)
            {
                var empDept =  JsonConvert.DeserializeObject<EmployeeDepartment>(employeedepartment.ToString());

                //insert the new department records
               DepartmentRecords = InsertDepartment(empDept.Departments);

                //fill the employee record with the associated Coreration Id

               Here I have to join the Employee and Department Records by the CorelationID

                //insert employee records
               EmployeeRecords = InsertEmployee(empDept.Employees);             
            }

        }
    }

JS文件如下

var guid = GenerateGuid();           

            //build the employee object
            var Employee = {
                EmployeeId: $scope.EmployeeId,
                EmployeeName: $scope.EmployeeName,
                Age: $scope.Age,
                Salary: $scope.Salary,
                CorrelationID: guid
            };

            //build the department object
            var Department = {
                Deptid: $scope.DepartmentID,
                Deptname: $scope.DepartmentName,
                CorrelationID: guid
            };

我该怎么做?

对象看起来像(一个例子)

编辑

我正在尝试使用 Expando Object,如下所示

public EmployeeDepartment InsertEmployeeAndDepartment(ExpandoObject 员工)

如何从这里获取员工和部门数组?

【问题讨论】:

  • 部门记录长什么样?插入后 CorrelationId 会改变吗?
  • 这取决于插入类型。如果成功,则相关性将保持不变,否则它将改变。基于此,我需要匹配员工,并且只有具有匹配 corelationID 的员工才有资格进行有效插入
  • 我在我的回答中输入了这个查询,它将只接受与 CorrelationId 匹配的员工。

标签: javascript c# asp.net-web-api


【解决方案1】:

如果我正确理解了您的问题,您只需要在相关 ID 上使用联接即可在部门列表中获取具有匹配相关 ID 的员工。

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {

        var employees = new List<Employee> { new Employee {Name = "MY_NAME1", CorrelationID = "CID1"}, new Employee {Name = "MY_NAME2", CorrelationID = "CID2"}, new Employee {Name = "MY_NAME3", CorrelationID = "CID3"}};
        var depts = new List<Department> { new Department {Name = "DEPT_NAME1", CorrelationID = "CID2"}, new Department {Name = "DEPT_NAME2", CorrelationID = "CID1"}};

        var joined = employees.Join(depts, x => x.CorrelationID, x => x.CorrelationID, (employee, dept) => new {EmployeeName = employee.Name, DepartmentName = dept.Name});

        foreach(var pair in joined){
            Console.WriteLine("EmployeeName: {0}, DepartmentName: {1}", pair.EmployeeName, pair.DepartmentName);
        }
    }

    public class Employee
    {
        public string Name {get;set;}
        public string CorrelationID {get;set;}
    }

    public class Department
    {
        public string Name {get;set;}
        public string CorrelationID {get;set;}
    }

    public class SqlBuilder{

        private static string _baseSql = "select * from customers";

        public static string GetSql(string customerId){
            var final = _baseSql;
            if(customerId != null)
                final += " WHERE custId=@custId";
            return final;
        }
    }
}

这将输出:

> EmployeeName: MY_NAME1, DepartmentName: DEPT_NAME2 
> EmployeeName: MY_NAME2, DepartmentName: DEPT_NAME1

【讨论】:

  • 请参阅,Employee 没有 CorelationId,也没有 Department。这是动态创建的。
  • 您是否有理由不能将它们添加到对象的定义中并在其余时间忽略它们? C# 不能很好地处理动态生成的参数。在序列化期间,相关对象将被忽略。
  • 是的...我们需要在客户端创建关联ID...它不能成为域模型的一部分...我们需要根据关联ID匹配记录服务器端
  • 我可以想到两种解决方案。一种是使用 Json.Decode(employeeDepartment) 它将返回一个动态对象,您将能够从中创建值。另一种是创建一个包含 CorrelationID 的新域模型,仅用于反序列化目的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 2011-05-04
  • 2014-10-30
  • 2021-09-13
  • 2015-06-05
  • 1970-01-01
相关资源
最近更新 更多