Reputation: 4692
I am using WCF in ASP.Net 4.0 with EntityFramework 4.1 and MVC3 with Razor.
The DataAnnotations work fine in the following scenario:
Having the POCO class in another DLL with EF and then filling the model with a direct call from my presentation tier. If I try and edit an email address on the front end grid and purposely put in a bogus email, the DataAnnotation error appears for that property.
However, if I use the approach with a WCF middle tier call (instead of a direct call), the data comes back and gets updated just fine except for the fact that the DataAnnotations don't work in this kind of scenario. If I try and edit an email address on the front end grid and purposely put in a bogus email, the DataAnnotation does not appear for that property.
How can I get it to work with using WCF? There must be some kind of WCF attribute I'm missing where it is not recognizing the DataAnnotations.
Below, is the pertinent code:
2 tier scenario:
Model
using System;
using System.Collections.Generic;
//using System.ServiceModel;
//using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;
namespace YeagerTechModel
{
public class Customer
{
public Customer()
{
this.Projects = new HashSet<Project>();
}
public short CustomerID { get; set; }
[Required]
[StringLength(50)]
[DataType(DataType.EmailAddress)]
[Email]
public string Email { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string Company { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string FirstName { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string LastName { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string Address1 { get; set; }
[StringLength(50)]
[DataType(DataType.Text)]
public string Address2 { get; set; }
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string City { get; set; }
[StringLength(2, MinimumLength = 2, ErrorMessage = "Must have a length of 2.")]
[DataType(DataType.Text)]
public string State { get; set; }
[StringLength(10)]
[DataType(DataType.Text)]
[RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip")]
public string Zip { get; set; }
[StringLength(12)]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Invalid Phone")]
public string HomePhone { get; set; }
[StringLength(12)]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Invalid Phone")]
public string CellPhone { get; set; }
[StringLength(100)]
[DataType(DataType.Url)]
[Url]
public string Website { get; set; }
[StringLength(50)]
[DataType(DataType.EmailAddress)]
[Email]
public string IMAddress { get; set; }
public System.DateTime CreatedDate { get; set; }
public Nullable<System.DateTime> UpdatedDate { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
}
Controller:
[GridAction]
public ActionResult Index()
{
ViewData["ErrCode"] = string.Empty;
if (HttpContext.User.IsInRole("Admin"))
{
try
{
//IEnumerable<YeagerTechWcfService.Customer> customerList = db.GetCustomers();
//IEnumerable<YeagerTechModel.Customer> customerList = db.GetCustomers();
DbContext.Configuration.ProxyCreationEnabled = false;
IEnumerable<Customer> customerList = DbContext.Customers.Where(p => p.CustomerID > 0);
if (DbContext.Database.Connection != null)
{
if (DbContext.Database.Connection.State != System.Data.ConnectionState.Closed)
{
DbContext.Database.Connection.Close();
DbContext.Database.Connection.Dispose();
}
}
return View(new GridModel<YeagerTechModel.Customer>
{
Data = customerList
});
}
catch (Exception ex)
{
throw ex;
}
}
else
{
//HttpCookie cn = Request.Cookies["strCookieName"];
//if (cn != null)
//{
// YeagerTechWcfService.Customer cust = db.GetCustomerID(Convert.ToInt16(cn.Value),false);
// if (cust != null)
// {
// return View(new GridModel<YeagerTechWcfService.Customer>
// {
// //Data = cust
// });
// }
// else
// return View(new GridModel<YeagerTechWcfService.Customer>());
//}
//else
//{
// TempData["ErrCode"] = "CustView";
return RedirectToAction("Index", "Home");
//}
}
}
View:
@model Telerik.Web.Mvc.GridModel<YeagerTechModel.Customer>
@{
ViewBag.Title = "Customer Index";
}
<h2>
Customer Index</h2>
@( Html.Telerik().Grid<YeagerTechModel.Customer>(Model.Data)
.Name("Customers")
.DataKeys(dataKeys => dataKeys.Add(o => o.CustomerID)
.RouteKey("CustomerID"))
.ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text).ImageHtmlAttributes(new { style = "margin-left:0" }))
.Columns(columns =>
{
columns.Bound(o => o.CustomerID).Hidden(true);
columns.Command(commands =>
{
commands.Edit().ButtonType(GridButtonType.Text);
}).Width(200).Title("Command");
columns.Bound(o => o.Email).Width(200).Filterable(false);
columns.Bound(o => o.Company).Width(200).Filterable(false);
columns.Bound(o => o.FirstName).Width(100).Title("FName").Filterable(false);
columns.Bound(o => o.LastName).Width(100).Title("LName").Filterable(false);
columns.Bound(o => o.Address1).Width(200).Title("Addr1").Filterable(false).Sortable(false);
columns.Bound(o => o.Address2).Width(100).Title("Addr2").Filterable(false).Sortable(false);
columns.Bound(o => o.City).Width(100);
columns.Bound(o => o.State).Width(40).Title("ST");
columns.Bound(o => o.Zip).Width(60);
columns.Bound(o => o.HomePhone).Width(120).Filterable(false).Sortable(false);
columns.Bound(o => o.CellPhone).Width(120).Filterable(false).Sortable(false);
columns.Bound(o => o.Website).Width(100).Filterable(false).Sortable(false);
columns.Bound(o => o.IMAddress).Width(100).Filterable(false).Sortable(false);
columns.Bound(o => o.CreatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120).Filterable(false).Sortable(false);
columns.Bound(o => o.UpdatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120).Filterable(false).Sortable(false);
}).DataBinding(dataBinding =>
dataBinding.Ajax()
.Insert("_InsertAjaxEditing", "Customer")
.Update("_SaveAjaxEditing", "Customer"))
.Editable(editing => editing.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Filterable()
.Scrollable()
)
NTier scenario Model:
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;
namespace YeagerTechModel
{
[Serializable]
[DataContract(IsReference = true)]
public class Customer
{
public Customer()
{
this.Projects = new HashSet<Project>();
}
[DataMember]
public short CustomerID { get; set; }
[DataMember]
[Required]
[StringLength(50)]
[DataType(DataType.EmailAddress)]
[Email]
public string Email { get; set; }
[DataMember]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string Company { get; set; }
[DataMember]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string FirstName { get; set; }
[DataMember]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string LastName { get; set; }
[DataMember]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string Address1 { get; set; }
[DataMember]
[StringLength(50)]
[DataType(DataType.Text)]
public string Address2 { get; set; }
[DataMember]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must have a minimum length of 3.")]
[DataType(DataType.Text)]
public string City { get; set; }
[DataMember]
[StringLength(2, MinimumLength = 2, ErrorMessage = "Must have a length of 2.")]
[DataType(DataType.Text)]
public string State { get; set; }
[DataMember]
[StringLength(10)]
[DataType(DataType.Text)]
[RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip")]
public string Zip { get; set; }
[DataMember]
[StringLength(12)]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Invalid Phone")]
public string HomePhone { get; set; }
[DataMember]
[StringLength(12)]
[DataType(DataType.PhoneNumber)]
[RegularExpression(@"^\s*([\(]?)\[?\s*\d{3}\s*\]?[\)]?\s*[\-]?[\.]?\s*\d{3}\s*[\-]?[\.]?\s*\d{4}$", ErrorMessage = "Invalid Phone")]
public string CellPhone { get; set; }
[DataMember]
[StringLength(100)]
[DataType(DataType.Url)]
[Url]
public string Website { get; set; }
[DataMember]
[StringLength(50)]
[DataType(DataType.EmailAddress)]
[Email]
public string IMAddress { get; set; }
[DataMember]
public System.DateTime CreatedDate { get; set; }
[DataMember]
public Nullable<System.DateTime> UpdatedDate { get; set; }
[DataMember]
public virtual ICollection<Project> Projects { get; set; }
}
}
WCF Call:
public IEnumerable<Customer> GetCustomers()
{
YeagerTechEntities DbContext = new YeagerTechEntities();
DbContext.Configuration.ProxyCreationEnabled = false;
IQueryable<Customer> customer = DbContext.Customers.Where(p => p.CustomerID > 0);
CloseConnection(DbContext);
return customer;
}
Controller:
[GridAction]
public ActionResult Index()
{
ViewData["ErrCode"] = string.Empty;
if (HttpContext.User.IsInRole("Admin"))
{
try
{
IEnumerable<YeagerTechWcfService.Customer> customerList = db.GetCustomers();
return View(new GridModel<YeagerTechWcfService.Customer>
{
Data = customerList
});
}
catch (Exception ex)
{
throw ex;
}
}
else
{
return RedirectToAction("Index", "Home");
}
}
View:
@model Telerik.Web.Mvc.GridModel<YeagerTech.YeagerTechWcfService.Customer>
@{
ViewBag.Title = "Customer Index";
}
<h2>
Customer Index</h2>
@( Html.Telerik().Grid<YeagerTech.YeagerTechWcfService.Customer>(Model.Data)
.Name("Customers")
.DataKeys(dataKeys => dataKeys.Add(o => o.CustomerID)
.RouteKey("CustomerID"))
.ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text).ImageHtmlAttributes(new { style = "margin-left:0" }))
.Columns(columns =>
{
columns.Bound(o => o.CustomerID).Hidden(true);
columns.Command(commands =>
{
commands.Edit().ButtonType(GridButtonType.Text);
}).Width(200).Title("Command");
columns.Bound(o => o.Email).Width(200).Filterable(false);
columns.Bound(o => o.Company).Width(200).Filterable(false);
columns.Bound(o => o.FirstName).Width(100).Title("FName").Filterable(false);
columns.Bound(o => o.LastName).Width(100).Title("LName").Filterable(false);
columns.Bound(o => o.Address1).Width(200).Title("Addr1").Filterable(false).Sortable(false);
columns.Bound(o => o.Address2).Width(100).Title("Addr2").Filterable(false).Sortable(false);
columns.Bound(o => o.City).Width(100);
columns.Bound(o => o.State).Width(40).Title("ST");
columns.Bound(o => o.Zip).Width(60);
columns.Bound(o => o.HomePhone).Width(120).Filterable(false).Sortable(false);
columns.Bound(o => o.CellPhone).Width(120).Filterable(false).Sortable(false);
columns.Bound(o => o.Website).Width(100).Filterable(false).Sortable(false);
columns.Bound(o => o.IMAddress).Width(100).Filterable(false).Sortable(false);
columns.Bound(o => o.CreatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120).Filterable(false).Sortable(false);
columns.Bound(o => o.UpdatedDate).Format("{0:MM/dd/yyyy}").ReadOnly(true).Width(120).Filterable(false).Sortable(false);
}).DataBinding(dataBinding =>
dataBinding.Ajax()
.Insert("_InsertAjaxEditing", "Customer")
.Update("_SaveAjaxEditing", "Customer"))
.Editable(editing => editing.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Filterable()
.Scrollable()
)
Upvotes: 3
Views: 4751
Reputation: 364249
No you are not missing anything except some theory behind technologies you are using. WCF is technology for exposing services and it does it in interoperable way. Data contract exposed by the service is just create for data. It doesn't matter how many fancy attributes you use on the contract or how many custom logic you put inside get
and set
methods of the property. On the client side you always see this:
[DataContract(IsReference = true)]
public partial class Customer
{
[DataMember]
public short CustomerID { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public string Company { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public string Address1 { get; set; }
[DataMember]
public string Address2 { get; set; }
[DataMember]
public string City { get; set; }
[DataMember]
public string State { get; set; }
[DataMember]
public string Zip { get; set; }
[DataMember]
public string HomePhone { get; set; }
[DataMember]
public string CellPhone { get; set; }
[DataMember]
public string Website { get; set; }
[DataMember]
public string IMAddress { get; set; }
[DataMember]
public System.DateTime CreatedDate { get; set; }
[DataMember]
public Nullable<System.DateTime> UpdatedDate { get; set; }
[DataMember]
public Project[] Projects { get; set; }
}
The reason for this is that once you expose the service it exposes all its contracts in interoperable way - service and operation contracts are described by WSDL and data contracts are described by XSD. XSD can describe only structure of data but no logic. The validation itself can be in some limited way be described in XSD but .NET XSD generator doesn't do this. Once you add service reference to your WCF service the proxy generator will take WSDL and XSD as a source and create your classes again without all that attributes.
If you want to have client side validation you should in the first place implement that validation on the client side - it can be done by using buddy classes for partial classes used by WCF proxy. If you don't want to use this way you must share assembly with your entities between your WCF client and WCF service and reuse those types when adding service reference. This will create tight coupling between your service and ASP.NET MVC application.
Upvotes: 5