开发者

Hide id property when creating object via wcf?

开发者 https://www.devze.com 2023-02-23 00:01 出处:网络
If I have simple add method like this public void AddUser(User user) { user.Id = Guid.NewGuid(); _userList.Add(user);

If I have simple add method like this

public void AddUser(User user)
{
        user.Id = Guid.NewGuid();
        _userList.Add(user);
}

and User class like

[DataContract]
public class User
{

    [DataMember]
    public Guid Id { get; set; }
    [DataMember]
    public string FirstName { get; set; }
    [DataMember]
    public string LastName { get; set; }
    [DataMember]
    public DateTime DateCreated { get; set; }        
}

Is it possible to hide user id when adding new object. I know I can achieve this by parameterized input method (sending each object property as input parameter), but I don't think that fits with wcf model. And what if I have m开发者_开发技巧any properties? This is just a simple example.

Hide id property when creating object via wcf?


Is it possible to hide user id when adding new object. I know I can achieve this by parameterized input method (sending each object property as input parameter), but I don't think that fits with wcf model.

Instead of using the same DTO or even an Entity object itself for service Get method and Add method, it is better to have

a separate UserResult DTO for GetUser method:

public class UserResult
    public ID as Guid
    public FirstName as String
    public LastName as String
    public DateCreated as DateTime
end class

public class UserService
    public function GetUser(ID as Guid) as UserResult
end class

and a separate UserAddCommand DTO for AddUser method:

public class UserAddCommand
    public FirstName as String
    public LastName as String
end class

public class UserService
    public function AddUser(Command as UserAddCommand) as Response
end class

User's ID and even DateCreated are created on service side in AddUser service method.


hope my opinion make sense for you, or your dev scope.

well, i think that your guid should be generated inside your add method, instead to be passed inside a object property. i gives to the method the possibility to decide if the generated guid is valid or not, and you can try with new guid until you get a valid one.

i don't know if is possible to hide this in this tool, because you have marked the id as DataMember an it means that your property becomes availabe.

if previous sugestion makes sense to you, just forget about this value in this method. if not the only solution that i can see is create a new class without this member.

hope it helps you


If you remove the [DataMember] attribute, then the GUID will not be available to WCF as it will not be serialized. However, would this present a problem when consuming the user objects on either side of the service, if they are tied to a back-end database? Wouldn't you just be creating new user objects all the time?

0

精彩评论

暂无评论...
验证码 换一张
取 消