I've read many articles about it, and all seems to be very complex solutions. But I believe there should be an easy way to solve my problem.
I've made the Entity Framework validation for my objects with Attributes which returns an Error Message from resources (but it's not matter it's the same like ErrorMessage = ...).
[MetadataType(typeof(UserMetadata))]
public partial class User
{
internal sealed class UserMetadata
{
[Required(AllowEmptyStrings = false, ErrorMessageResourceName = "UserNameRequired", ErrorMessageResourceType = typeof(ErrorMessage))]
[StringLength(150, ErrorMessageResourceName = "UserNameLength", ErrorMessageResourceType = typeof(ErrorMessage))]
public string UserName { get; set; }
}
}
In my WCF Service I have the contract:
[ServiceContract]
public interface IUser
{
[OperationContract]
User AddUser(User user);
}
And the implementation:
public class UserService: IUser
{
public User AddUser(User user)
{
//Here I think I should throw the ErrorMessage with a FaultException
//and to catch it in the client side, but how to do it !?
IUserRepository _user = new UserRepository(); //I've used EF Repository Pattern.
return _user.Add(user);
}
}