将强类型对象转换为匿名对象
Converting strongly typed object to anonymous one
我正在从事一个相对较大的项目,我们试图在可能的情况下暗示面向服务的体系结构,但由于这个事实,今天我遇到了以下问题。
在我的表示层 (ASP.NET Web Forms
) 我有一个 User
对象:
public class User
{
public int ID {get; set;}
public string Name {get; set;}
public string Email {get; set;}
Public string State {get; set;}
public DateTime CreatedOn {get; set;}
public string CreatedBy {get; set;}
}
原始项目中还有一些字段,但对于这种情况,我认为这并不重要。
所以在表现层我用这个对象在页面上显示用户信息,让使用应用程序的人执行增删改查操作。
问题是当我想创建一个新用户时。有一个单独的 Web Api 2
服务项目 - “新创建用户的 UserServiceso all calls are made to the dedicated action from the
UserServiceproject and the response is the
ID` 以及他创建时的初始状态。
所以要创建一个新用户,我会这样做:
public User InsertUser(string username, string email, string createdBy)
{
var user = new
{
Username = username,
Email = email,
CreatedBy = createdBy
}
var result = //make call to the user service passing the anonymous object
user newUser = new User
{
ID = result.ID,
Username = username,
Email = email,
CreatedBy = createdBy,
State = result.State
}
return newUser;
}
由于某些近期无法解决的原因,我无法引用某些 DTO
对象,并且该服务需要来自同一类型的对象或匿名对象,或者它无法反序列化数据。这里有两件事真正困扰我——第一件事是我创建了两次实例,理想情况下应该只是一个 User
类型的对象,在执行服务后我可以添加ID
和 State
像这样:
newUser.Id = result.Id
newUser.State = result.State
相反,我创建了两个远非理想的对象。其次,我认为可能的一件事是从表示层创建 User
的实例,但以某种方式转换它,以便服务操作能够反序列化它。此外,这似乎是非常标准的情况,不包括我无法引用 .dll
或其他内容的事实。但是,也许还有另一种我不知道的解决此问题的方法?
编辑
在 Web Api
部分,方法是这样的:
public HttpResponseMessage InsertUser([FromBody]UserDTO userToInsert)
{
var user = userToInsert;
//Call Stored Procedure to Insert the user
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new {UserId = user.Id, State = user.State});
return response;
}
在我的客户端中,我调用这个方法只是为了让它工作,我有一个嵌套的 class:
public class UserDetails
{
public int UserId {get; set;}
public string State {get; set;}
}
您是否考虑过序列化为 JSON 然后反序列化为匿名类型对象?看看JSON.NET (http://www.newtonsoft.com/json/help/html/DeserializeAnonymousType.htm)
我正在从事一个相对较大的项目,我们试图在可能的情况下暗示面向服务的体系结构,但由于这个事实,今天我遇到了以下问题。
在我的表示层 (ASP.NET Web Forms
) 我有一个 User
对象:
public class User
{
public int ID {get; set;}
public string Name {get; set;}
public string Email {get; set;}
Public string State {get; set;}
public DateTime CreatedOn {get; set;}
public string CreatedBy {get; set;}
}
原始项目中还有一些字段,但对于这种情况,我认为这并不重要。
所以在表现层我用这个对象在页面上显示用户信息,让使用应用程序的人执行增删改查操作。
问题是当我想创建一个新用户时。有一个单独的 Web Api 2
服务项目 - “新创建用户的 UserServiceso all calls are made to the dedicated action from the
UserServiceproject and the response is the
ID` 以及他创建时的初始状态。
所以要创建一个新用户,我会这样做:
public User InsertUser(string username, string email, string createdBy)
{
var user = new
{
Username = username,
Email = email,
CreatedBy = createdBy
}
var result = //make call to the user service passing the anonymous object
user newUser = new User
{
ID = result.ID,
Username = username,
Email = email,
CreatedBy = createdBy,
State = result.State
}
return newUser;
}
由于某些近期无法解决的原因,我无法引用某些 DTO
对象,并且该服务需要来自同一类型的对象或匿名对象,或者它无法反序列化数据。这里有两件事真正困扰我——第一件事是我创建了两次实例,理想情况下应该只是一个 User
类型的对象,在执行服务后我可以添加ID
和 State
像这样:
newUser.Id = result.Id
newUser.State = result.State
相反,我创建了两个远非理想的对象。其次,我认为可能的一件事是从表示层创建 User
的实例,但以某种方式转换它,以便服务操作能够反序列化它。此外,这似乎是非常标准的情况,不包括我无法引用 .dll
或其他内容的事实。但是,也许还有另一种我不知道的解决此问题的方法?
编辑
在 Web Api
部分,方法是这样的:
public HttpResponseMessage InsertUser([FromBody]UserDTO userToInsert)
{
var user = userToInsert;
//Call Stored Procedure to Insert the user
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new {UserId = user.Id, State = user.State});
return response;
}
在我的客户端中,我调用这个方法只是为了让它工作,我有一个嵌套的 class:
public class UserDetails
{
public int UserId {get; set;}
public string State {get; set;}
}
您是否考虑过序列化为 JSON 然后反序列化为匿名类型对象?看看JSON.NET (http://www.newtonsoft.com/json/help/html/DeserializeAnonymousType.htm)