0

I have an N-Tier solution. It has four projects as shown below:

  1. Infrastructure (Model class)
  2. Repository
  3. Service (WCF)
  4. Web (Presentation)

The infrastructure takes care of the model classes

Infrastructure

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; }
}
}

Service: 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);
    }
}
}

The question is, why is the data annotation and validation not being implemented in the presentation layer (view)?

krlzlx
  • 5,752
  • 14
  • 47
  • 55
Gbenga
  • 115
  • 1
  • 4
  • 15

1 Answers1

0

A key bit of information you have left out is how you imported your service reference into you Presentation project.

I am going to assume for the time being that you used the Service Reference wizard - this is what is causing your issue. When you use the provided wizard, visual studio looks at the hosted WSDL definition for your WCF service and auto generates new proxy and data contracts in the project you are working in. WSDLs do not support the data annotations you used and therefor are not copied over to the new contracts defined in the presentation project.

To fix this you have two options.

1) Navigate to the auto generated classes in your Presentation project and mark them up. Obviously this will lead to duplication of code in the long run and is not the most ideal.

2) Reference your data contracts and service contracts by DLL and write your own proxy class that inherits from ClientBase. You can find more details about that here: Create WCF Client without auto generated proxy.

Nick A.
  • 16
  • 1
  • Yes you are correct. I used **Service Reference wizard**. The question is, how do I apply option 2 that you stated **Reference your data contracts and service contracts by DLL and write your own proxy class that inherits from ClientBase. You can find more details about that here**. Please I dont know it. – Gbenga Sep 25 '17 at 13:10