wcf asp.net mvc中的数据注释实现

时间:2017-09-27 20:27:08

标签: asp.net-mvc wcf

我的解决方案中包含以下内容(包含4个项目)

  1. 基础设施(模型类)
  2. 存储库
  3. 服务(WCF)
  4. 网络(演示)
  5. 我使用服务引用向导(添加服务引用)来使用我的Web服务(演示文稿)

    我在基础架构中运行以下操作来处理模型类:

    基础建设

    using System.Runtime.Serialization;
    namespace Infrastructure
    {
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading.Tasks;
    using DataAnnotationsExtensions;
    
    [Serializable]
    [DataContract(IsReference = true)]
    public partial class COUNTRIES
    {
    public COUNTRIES()
    {
        this.CITIES = new HashSet<CITIES>();
        this.LGA = new HashSet<LGA>();
        this.STATES = new HashSet<STATES>();
    }
    
    [DataMember]
    public int COUNTRY_ID { get; set; }
    
    // [DataMember(Name = "Country Code")]
    [DataMember]
    [Required(ErrorMessage = "Country Code is required")]
    [Display(Name = "Country Code")]
    [StringLength(2, ErrorMessage = "The {0} must be at least {1} characters long. Plese check again!", MinimumLength = 2)]
    //[Index(IsUnique = true)]
    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use letters only please")]
    public string COUNTRY_CODE { get; set; }
    
    [DataMember]
    [Required(ErrorMessage = "Country Name is required")]
    [Display(Name = "Country Name")]
    //[Index(IsUnique = true)]
    //[StringLength(50, ErrorMessage = "Too long. Plese check again!")]
    [StringLength(50, ErrorMessage = "The {0} must be at least {1} characters long. Plese check again!", MinimumLength = 2)]
    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use letters only please")]
    public string COUNTRY_NAME { get; set; }
    
    [DataMember]
    [Display(Name = "Action Status")]
    public int ACTION_STATUS { get; set; }
    
    [DataMember]
    [Display(Name = "Date Created")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public Nullable<System.DateTime> CREATED_DATE { get; set; }
    
    [DataMember]
    [Display(Name = "Created By")]
    public Nullable<int> CREATED_BY { get; set; }
    
    [DataMember]
    [Display(Name = "Last Update Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public Nullable<System.DateTime> LAST_UPDATE_DATE { get; set; }
    
    [DataMember]
    [Display(Name = "Last Update By")]
    public Nullable<int> LAST_UPDATE_BY { get; set; }
    
    [DataMember]
    [Display(Name = "Date Deleted")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public Nullable<System.DateTime> DELETED_DATE { get; set; }
    
    [DataMember]
    [Display(Name = "Deleted By")]
    public Nullable<int> DELETED_BY { get; set; }
    
    
    [DataMember]
    public virtual ICollection<CITIES> CITIES { internal get; set; }
    
    [DataMember]
    public virtual ICollection<LGA> LGA { internal get; set; }
    
    [DataMember]
    public virtual ICollection<STATES> STATES { internal get; set; }
     }
     }
    

    然后,我的服务:WCF

    namespace BPP.CCSP.Admin.Services.Services.Concrete
    {
    
    [ValidateDataAnnotationsBehavior]
    
    public class CountriesService : ICountriesService
    {
    //public void DoWork()
    //{
    //}
     private readonly ICountriesManager _countriesManager;
    
     public CountriesService(ICountriesManager countriesManager)
     {
        _countriesManager = countriesManager;
     }
    
     public COUNTRIES GetCountry(Int32 countryID)
     {
        return _countriesManager.Country(countryID);
     }
    
    public IEnumerable<COUNTRIES> GetCountries()
    {
        return _countriesManager.Countries();
    }
    
    public void AddCountry(COUNTRIES countries)
    {
        _countriesManager.AddCountry(countries);
    }
    
    public void RemoveCountry(int countryID)
    {
        _countriesManager.Country(countryID);
    }
    }
    }
    
      

    我遇到的问题是数据注释没有实现。

    这是流程: 基础设施(型号)=&gt;存储库=&gt;服务(WCF)=&gt;网络(演示文稿)

    请问,如何在表示层(视图)中实现数据注释和验证?

1 个答案:

答案 0 :(得分:0)

如果你想实现数据注释,那么你应该有模型参考你的演示文稿,一旦你将ref添加到你的表示层,那么你可以验证你的控制器内的模型,然后调用你的WCF服务。

将模型作为共享实体,将在所有存储库,服务(WCF),演示文稿中共享。

希望这会对你有所帮助