Click here to Skip to main content
15,896,727 members
Articles / Programming Languages / C# 4.0

Introduction to ADO.NET Data Services/RIA Services

Rate me:
Please Sign up or sign in to vote.
4.93/5 (51 votes)
25 Feb 2010CPOL26 min read 181.2K   2.5K   166  
An introduction to ADO.NET Data Services / RIA Services.
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.21006.1
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace RIADataServicesDemoApp
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web.Ria.Data;
    using System.Windows.Ria;
    using System.Windows.Ria.Services;
    using RIADataServicesDemoApp.Web;
    
    
    /// <summary>
    /// Context for the RIA application.
    /// </summary>
    /// <remarks>
    /// This context extends the base to make application services and types available
    /// for consumption from code and xaml.
    /// </remarks>
    public sealed partial class WebContext : WebContextBase
    {
        
        #region Extensibility Method Definitions

        /// <summary>
        /// This method is invoked from the constructor once initialization is complete and
        /// can be used for further object setup.
        /// </summary>
        partial void OnCreated();

        #endregion
        
        
        /// <summary>
        /// Initializes a new instance of the WebContext class.
        /// </summary>
        public WebContext()
        {
            this.OnCreated();
        }
        
        /// <summary>
        /// Gets the context that is registered as a lifetime object with the current application.
        /// </summary>
        /// <exception cref="InvalidOperationException"> is thrown if there is no current application,
        /// no contexts have been added, or more than one context has been added.
        /// </exception>
        /// <seealso cref="Application.ApplicationLifetimeObjects"/>
        public new static WebContext Current
        {
            get
            {
                return ((WebContext)(WebContextBase.Current));
            }
        }
        
        /// <summary>
        /// Gets a user representing the authenticated identity.
        /// </summary>
        public new User User
        {
            get
            {
                return ((User)(base.User));
            }
        }
    }
}
namespace RIADataServicesDemoApp.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Web.Ria.Data;
    using System.Windows.Ria;
    using System.Windows.Ria.Services;
    using RIADataServicesDemoApp.Web.Resources;
    
    
    public sealed partial class AuthenticationContext : global::System.Windows.Ria.ApplicationServices.AuthenticationDomainContextBase
    {
        
        #region Extensibility Method Definitions

        /// <summary>
        /// This method is invoked from the constructor once initialization is complete and
        /// can be used for further object setup.
        /// </summary>
        partial void OnCreated();

        #endregion
        
        
        /// <summary>
        /// Default constructor.
        /// </summary>
        public AuthenticationContext() : 
                this(new WebDomainClient<IAuthenticationServiceContract>(new Uri("RIADataServicesDemoApp-Web-AuthenticationService.svc", UriKind.Relative)))
        {
        }
        
        /// <summary>
        /// Constructor used to specify a data service URI.
        /// </summary>
        /// <param name="serviceUri">
        /// The AuthenticationService data service URI.
        /// </param>
        public AuthenticationContext(Uri serviceUri) : 
                this(new WebDomainClient<IAuthenticationServiceContract>(serviceUri))
        {
        }
        
        /// <summary>
        /// Constructor used to specify a DomainClient instance.
        /// </summary>
        /// <param name="domainClient">
        /// The DomainClient instance the DomainContext should use.
        /// </param>
        public AuthenticationContext(DomainClient domainClient) : 
                base(domainClient)
        {
            this.OnCreated();
        }
        
        public EntitySet<User> Users
        {
            get
            {
                return base.EntityContainer.GetEntitySet<User>();
            }
        }
        
        /// <summary>
        /// Returns an EntityQuery for query operation 'GetUser'.
        /// </summary>
        public EntityQuery<User> GetUserQuery()
        {
            this.ValidateMethod("GetUserQuery", null);
            return base.CreateQuery<User>("GetUser", null, false, false);
        }
        
        /// <summary>
        /// Returns an EntityQuery for query operation 'Login'.
        /// </summary>
        public EntityQuery<User> LoginQuery(string userName, string password, bool isPersistent, string customData)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("userName", userName);
            parameters.Add("password", password);
            parameters.Add("isPersistent", isPersistent);
            parameters.Add("customData", customData);
            this.ValidateMethod("LoginQuery", parameters);
            return base.CreateQuery<User>("Login", parameters, true, false);
        }
        
        /// <summary>
        /// Returns an EntityQuery for query operation 'Logout'.
        /// </summary>
        public EntityQuery<User> LogoutQuery()
        {
            this.ValidateMethod("LogoutQuery", null);
            return base.CreateQuery<User>("Logout", null, true, false);
        }
        
        protected override EntityContainer CreateEntityContainer()
        {
            return new AuthenticationContextEntityContainer();
        }
        
        [ServiceContract()]
        public interface IAuthenticationServiceContract
        {
            
            [FaultContract(typeof(DomainServiceFault), Action="http://tempuri.org/AuthenticationService/LoginDomainServiceFault", Name="DomainServiceFault", Namespace="DomainServices")]
            [OperationContract(AsyncPattern=true, Action="http://tempuri.org/AuthenticationService/Login", ReplyAction="http://tempuri.org/AuthenticationService/LoginResponse")]
            IAsyncResult BeginLogin(string userName, string password, bool isPersistent, string customData, AsyncCallback callback, object asyncState);
            
            QueryResult<User> EndLogin(IAsyncResult result);
            
            [FaultContract(typeof(DomainServiceFault), Action="http://tempuri.org/AuthenticationService/LogoutDomainServiceFault", Name="DomainServiceFault", Namespace="DomainServices")]
            [OperationContract(AsyncPattern=true, Action="http://tempuri.org/AuthenticationService/Logout", ReplyAction="http://tempuri.org/AuthenticationService/LogoutResponse")]
            IAsyncResult BeginLogout(AsyncCallback callback, object asyncState);
            
            QueryResult<User> EndLogout(IAsyncResult result);
            
            [FaultContract(typeof(DomainServiceFault), Action="http://tempuri.org/AuthenticationService/GetUserDomainServiceFault", Name="DomainServiceFault", Namespace="DomainServices")]
            [OperationContract(AsyncPattern=true, Action="http://tempuri.org/AuthenticationService/GetUser", ReplyAction="http://tempuri.org/AuthenticationService/GetUserResponse")]
            IAsyncResult BeginGetUser(AsyncCallback callback, object asyncState);
            
            QueryResult<User> EndGetUser(IAsyncResult result);
            
            [FaultContract(typeof(DomainServiceFault), Action="http://tempuri.org/AuthenticationService/SubmitChangesDomainServiceFault", Name="DomainServiceFault", Namespace="DomainServices")]
            [OperationContract(AsyncPattern=true, Action="http://tempuri.org/AuthenticationService/SubmitChanges", ReplyAction="http://tempuri.org/AuthenticationService/SubmitChangesResponse")]
            IAsyncResult BeginSubmitChanges(IEnumerable<ChangeSetEntry> changeSet, AsyncCallback callback, object asyncState);
            
            IEnumerable<ChangeSetEntry> EndSubmitChanges(IAsyncResult result);
        }
        
        internal sealed class AuthenticationContextEntityContainer : EntityContainer
        {
            
            public AuthenticationContextEntityContainer()
            {
                this.CreateEntitySet<User>(EntitySetOperations.Edit);
            }
        }
    }
    
    [DataContract(Namespace="http://schemas.datacontract.org/2004/07/RIADataServicesDemoApp.Web")]
    public sealed partial class RegistrationData : Entity
    {
        
        private string _answer;
        
        private string _email;
        
        private string _friendlyName;
        
        private string _password;
        
        private string _question;
        
        private string _userName;
        
        #region Extensibility Method Definitions

        /// <summary>
        /// This method is invoked from the constructor once initialization is complete and
        /// can be used for further object setup.
        /// </summary>
        partial void OnCreated();
        partial void OnAnswerChanging(string value);
        partial void OnAnswerChanged();
        partial void OnEmailChanging(string value);
        partial void OnEmailChanged();
        partial void OnFriendlyNameChanging(string value);
        partial void OnFriendlyNameChanged();
        partial void OnPasswordChanging(string value);
        partial void OnPasswordChanged();
        partial void OnQuestionChanging(string value);
        partial void OnQuestionChanged();
        partial void OnUserNameChanging(string value);
        partial void OnUserNameChanged();

        #endregion
        
        
        /// <summary>
        /// Default constructor.
        /// </summary>
        public RegistrationData()
        {
            this.OnCreated();
        }
        
        [DataMember()]
        [Display(Name="SecurityAnswerLabel", Order=6, ResourceType=typeof(RegistrationDataResources))]
        [Required(ErrorMessageResourceName="ValidationErrorRequiredField", ErrorMessageResourceType=typeof(ErrorResources))]
        public string Answer
        {
            get
            {
                return this._answer;
            }
            set
            {
                if ((this._answer != value))
                {
                    this.ValidateProperty("Answer", value);
                    this.OnAnswerChanging(value);
                    this.RaiseDataMemberChanging("Answer");
                    this._answer = value;
                    this.RaiseDataMemberChanged("Answer");
                    this.OnAnswerChanged();
                }
            }
        }
        
        [DataMember()]
        [Display(Name="EmailLabel", Order=2, ResourceType=typeof(RegistrationDataResources))]
        [Key()]
        [RegularExpression("^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4" +
            "}|[0-9]{1,3})(\\]?)$", ErrorMessageResourceName="ValidationErrorInvalidEmail", ErrorMessageResourceType=typeof(ErrorResources))]
        [Required(ErrorMessageResourceName="ValidationErrorRequiredField", ErrorMessageResourceType=typeof(ErrorResources))]
        public string Email
        {
            get
            {
                return this._email;
            }
            set
            {
                if ((this._email != value))
                {
                    this.ValidateProperty("Email", value);
                    this.OnEmailChanging(value);
                    this.RaiseDataMemberChanging("Email");
                    this._email = value;
                    this.RaiseDataMemberChanged("Email");
                    this.OnEmailChanged();
                }
            }
        }
        
        [DataMember()]
        [Display(Description="FriendlyNameDescription", Name="FriendlyNameLabel", Order=1, ResourceType=typeof(RegistrationDataResources))]
        public string FriendlyName
        {
            get
            {
                return this._friendlyName;
            }
            set
            {
                if ((this._friendlyName != value))
                {
                    this.ValidateProperty("FriendlyName", value);
                    this.OnFriendlyNameChanging(value);
                    this.RaiseDataMemberChanging("FriendlyName");
                    this._friendlyName = value;
                    this.RaiseDataMemberChanged("FriendlyName");
                    this.OnFriendlyNameChanged();
                }
            }
        }
        
        [DataMember()]
        [Display(Description="PasswordDescription", Name="PasswordLabel", Order=3, ResourceType=typeof(RegistrationDataResources))]
        [RegularExpression("^.*[^a-zA-Z0-9].*$", ErrorMessageResourceName="ValidationErrorBadPasswordStrength", ErrorMessageResourceType=typeof(ErrorResources))]
        [Required(ErrorMessageResourceName="ValidationErrorRequiredField", ErrorMessageResourceType=typeof(ErrorResources))]
        [StringLength(50, ErrorMessageResourceName="ValidationErrorBadPasswordLength", ErrorMessageResourceType=typeof(ErrorResources), MinimumLength=7)]
        public string Password
        {
            get
            {
                return this._password;
            }
            set
            {
                if ((this._password != value))
                {
                    this.ValidateProperty("Password", value);
                    this.OnPasswordChanging(value);
                    this.RaiseDataMemberChanging("Password");
                    this._password = value;
                    this.RaiseDataMemberChanged("Password");
                    this.OnPasswordChanged();
                }
            }
        }
        
        [DataMember()]
        [Display(Name="SecurityQuestionLabel", Order=5, ResourceType=typeof(RegistrationDataResources))]
        [Required(ErrorMessageResourceName="ValidationErrorRequiredField", ErrorMessageResourceType=typeof(ErrorResources))]
        public string Question
        {
            get
            {
                return this._question;
            }
            set
            {
                if ((this._question != value))
                {
                    this.ValidateProperty("Question", value);
                    this.OnQuestionChanging(value);
                    this.RaiseDataMemberChanging("Question");
                    this._question = value;
                    this.RaiseDataMemberChanged("Question");
                    this.OnQuestionChanged();
                }
            }
        }
        
        [DataMember()]
        [Display(Name="UserNameLabel", Order=0, ResourceType=typeof(RegistrationDataResources))]
        [Key()]
        [RegularExpression("^[a-zA-Z0-9_]*$", ErrorMessageResourceName="ValidationErrorInvalidUserName", ErrorMessageResourceType=typeof(ErrorResources))]
        [Required(ErrorMessageResourceName="ValidationErrorRequiredField", ErrorMessageResourceType=typeof(ErrorResources))]
        public string UserName
        {
            get
            {
                return this._userName;
            }
            set
            {
                if ((this._userName != value))
                {
                    this.ValidateProperty("UserName", value);
                    this.OnUserNameChanging(value);
                    this.RaiseDataMemberChanging("UserName");
                    this._userName = value;
                    this.RaiseDataMemberChanged("UserName");
                    this.OnUserNameChanged();
                }
            }
        }
        
        public override object GetIdentity()
        {
            return EntityKey.Create(this._email, this._userName);
        }
    }
    
    [DataContract(Namespace="http://schemas.datacontract.org/2004/07/RIADataServicesDemoApp.Web")]
    public sealed partial class User : Entity, global::System.Security.Principal.IIdentity, global::System.Security.Principal.IPrincipal
    {
        
        private string _friendlyName;
        
        private string _name;
        
        private IEnumerable<string> _roles;
        
        #region Extensibility Method Definitions

        /// <summary>
        /// This method is invoked from the constructor once initialization is complete and
        /// can be used for further object setup.
        /// </summary>
        partial void OnCreated();
        partial void OnFriendlyNameChanging(string value);
        partial void OnFriendlyNameChanged();
        partial void OnNameChanging(string value);
        partial void OnNameChanged();
        partial void OnRolesChanging(IEnumerable<string> value);
        partial void OnRolesChanged();

        #endregion
        
        
        /// <summary>
        /// Default constructor.
        /// </summary>
        public User()
        {
            this.OnCreated();
        }
        
        [DataMember()]
        public string FriendlyName
        {
            get
            {
                return this._friendlyName;
            }
            set
            {
                if ((this._friendlyName != value))
                {
                    this.ValidateProperty("FriendlyName", value);
                    this.OnFriendlyNameChanging(value);
                    this.RaiseDataMemberChanging("FriendlyName");
                    this._friendlyName = value;
                    this.RaiseDataMemberChanged("FriendlyName");
                    this.OnFriendlyNameChanged();
                }
            }
        }
        
        [DataMember()]
        [Key()]
        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                if ((this._name != value))
                {
                    this.ValidateProperty("Name", value);
                    this.OnNameChanging(value);
                    this.RaiseDataMemberChanging("Name");
                    this._name = value;
                    this.RaiseDataMemberChanged("Name");
                    this.OnNameChanged();
                    this.RaisePropertyChanged("IsAuthenticated");
                }
            }
        }
        
        [DataMember()]
        [Editable(false)]
        public IEnumerable<string> Roles
        {
            get
            {
                return this._roles;
            }
            set
            {
                if ((this._roles != value))
                {
                    this.ValidateProperty("Roles", value);
                    this.OnRolesChanging(value);
                    this._roles = value;
                    this.RaisePropertyChanged("Roles");
                    this.OnRolesChanged();
                }
            }
        }
        
        string global::System.Security.Principal.IIdentity.AuthenticationType
        {
            get
            {
                return string.Empty;
            }
        }
        
        /// <summary>
        /// Gets a value indicating whether the identity is authenticated.
        /// </summary>
        /// <remarks>
        /// This value is <c>true</c> if <see cref="Name"/> is not <c>null</c> or empty.
        /// </remarks>
        public bool IsAuthenticated
        {
            get
            {
                return (true != string.IsNullOrEmpty(this.Name));
            }
        }
        
        string global::System.Security.Principal.IIdentity.Name
        {
            get
            {
                return this.Name;
            }
        }
        
        global::System.Security.Principal.IIdentity global::System.Security.Principal.IPrincipal.Identity
        {
            get
            {
                return this;
            }
        }
        
        public override object GetIdentity()
        {
            return this._name;
        }
        
        /// <summary>
        /// Return whether the principal is in the role.
        /// </summary>
        /// <remarks>
        /// Returns whether the specified role is contained in the roles.
        /// This implementation is case sensitive.
        /// </remarks>
        /// <param name="role">The name of the role for which to check membership.</param>
        /// <returns>Whether the principal is in the role.</returns>
        public bool IsInRole(string role)
        {
            if ((this.Roles == null))
            {
                return false;
            }
            return global::System.Linq.Enumerable.Contains(this.Roles, role);
        }
    }
    
    public sealed partial class UserRegistrationContext : DomainContext
    {
        
        #region Extensibility Method Definitions

        /// <summary>
        /// This method is invoked from the constructor once initialization is complete and
        /// can be used for further object setup.
        /// </summary>
        partial void OnCreated();

        #endregion
        
        
        /// <summary>
        /// Default constructor.
        /// </summary>
        public UserRegistrationContext() : 
                this(new WebDomainClient<IUserRegistrationServiceContract>(new Uri("RIADataServicesDemoApp-Web-UserRegistrationService.svc", UriKind.Relative)))
        {
        }
        
        /// <summary>
        /// Constructor used to specify a data service URI.
        /// </summary>
        /// <param name="serviceUri">
        /// The UserRegistrationService data service URI.
        /// </param>
        public UserRegistrationContext(Uri serviceUri) : 
                this(new WebDomainClient<IUserRegistrationServiceContract>(serviceUri))
        {
        }
        
        /// <summary>
        /// Constructor used to specify a DomainClient instance.
        /// </summary>
        /// <param name="domainClient">
        /// The DomainClient instance the DomainContext should use.
        /// </param>
        public UserRegistrationContext(DomainClient domainClient) : 
                base(domainClient)
        {
            this.OnCreated();
        }
        
        public EntitySet<RegistrationData> RegistrationDatas
        {
            get
            {
                return base.EntityContainer.GetEntitySet<RegistrationData>();
            }
        }
        
        /// <summary>
        /// Returns an EntityQuery for query operation 'GetUsers'.
        /// </summary>
        public EntityQuery<RegistrationData> GetUsersQuery()
        {
            this.ValidateMethod("GetUsersQuery", null);
            return base.CreateQuery<RegistrationData>("GetUsers", null, false, true);
        }
        
        protected override EntityContainer CreateEntityContainer()
        {
            return new UserRegistrationContextEntityContainer();
        }
        
        [ServiceContract()]
        public interface IUserRegistrationServiceContract
        {
            
            [FaultContract(typeof(DomainServiceFault), Action="http://tempuri.org/UserRegistrationService/GetUsersDomainServiceFault", Name="DomainServiceFault", Namespace="DomainServices")]
            [OperationContract(AsyncPattern=true, Action="http://tempuri.org/UserRegistrationService/GetUsers", ReplyAction="http://tempuri.org/UserRegistrationService/GetUsersResponse")]
            IAsyncResult BeginGetUsers(AsyncCallback callback, object asyncState);
            
            QueryResult<RegistrationData> EndGetUsers(IAsyncResult result);
            
            [FaultContract(typeof(DomainServiceFault), Action="http://tempuri.org/UserRegistrationService/SubmitChangesDomainServiceFault", Name="DomainServiceFault", Namespace="DomainServices")]
            [OperationContract(AsyncPattern=true, Action="http://tempuri.org/UserRegistrationService/SubmitChanges", ReplyAction="http://tempuri.org/UserRegistrationService/SubmitChangesResponse")]
            IAsyncResult BeginSubmitChanges(IEnumerable<ChangeSetEntry> changeSet, AsyncCallback callback, object asyncState);
            
            IEnumerable<ChangeSetEntry> EndSubmitChanges(IAsyncResult result);
        }
        
        internal sealed class UserRegistrationContextEntityContainer : EntityContainer
        {
            
            public UserRegistrationContextEntityContainer()
            {
                this.CreateEntitySet<RegistrationData>(EntitySetOperations.Add);
            }
        }
    }
}
namespace RIADataServicesDemoApp.Web.CustomRIAServices
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.ServiceModel;
    using System.Web.Ria.Data;
    using System.Windows.Ria;
    using System.Windows.Ria.Services;
    using RIADataServicesDemoApp.Web.Model;
    
    
    public sealed partial class RIADataContext : DomainContext
    {
        
        #region Extensibility Method Definitions

        /// <summary>
        /// This method is invoked from the constructor once initialization is complete and
        /// can be used for further object setup.
        /// </summary>
        partial void OnCreated();

        #endregion
        
        
        /// <summary>
        /// Default constructor.
        /// </summary>
        public RIADataContext() : 
                this(new WebDomainClient<IRIADataServiceContract>(new Uri("RIADataServicesDemoApp-Web-CustomRIAServices-RIADataService.svc", UriKind.Relative)))
        {
        }
        
        /// <summary>
        /// Constructor used to specify a data service URI.
        /// </summary>
        /// <param name="serviceUri">
        /// The RIADataService data service URI.
        /// </param>
        public RIADataContext(Uri serviceUri) : 
                this(new WebDomainClient<IRIADataServiceContract>(serviceUri))
        {
        }
        
        /// <summary>
        /// Constructor used to specify a DomainClient instance.
        /// </summary>
        /// <param name="domainClient">
        /// The DomainClient instance the DomainContext should use.
        /// </param>
        public RIADataContext(DomainClient domainClient) : 
                base(domainClient)
        {
            this.OnCreated();
        }
        
        public EntitySet<Address> Addresses
        {
            get
            {
                return base.EntityContainer.GetEntitySet<Address>();
            }
        }
        
        public EntitySet<Customer> Customers
        {
            get
            {
                return base.EntityContainer.GetEntitySet<Customer>();
            }
        }
        
        /// <summary>
        /// Returns an EntityQuery for query operation 'GetAddresses'.
        /// </summary>
        public EntityQuery<Address> GetAddressesQuery()
        {
            this.ValidateMethod("GetAddressesQuery", null);
            return base.CreateQuery<Address>("GetAddresses", null, false, true);
        }
        
        /// <summary>
        /// Returns an EntityQuery for query operation 'GetCustomers'.
        /// </summary>
        public EntityQuery<Customer> GetCustomersQuery()
        {
            this.ValidateMethod("GetCustomersQuery", null);
            return base.CreateQuery<Customer>("GetCustomers", null, false, true);
        }
        
        protected override EntityContainer CreateEntityContainer()
        {
            return new RIADataContextEntityContainer();
        }
        
        [ServiceContract()]
        public interface IRIADataServiceContract
        {
            
            [FaultContract(typeof(DomainServiceFault), Action="http://tempuri.org/RIADataService/GetAddressesDomainServiceFault", Name="DomainServiceFault", Namespace="DomainServices")]
            [OperationContract(AsyncPattern=true, Action="http://tempuri.org/RIADataService/GetAddresses", ReplyAction="http://tempuri.org/RIADataService/GetAddressesResponse")]
            IAsyncResult BeginGetAddresses(AsyncCallback callback, object asyncState);
            
            QueryResult<Address> EndGetAddresses(IAsyncResult result);
            
            [FaultContract(typeof(DomainServiceFault), Action="http://tempuri.org/RIADataService/GetCustomersDomainServiceFault", Name="DomainServiceFault", Namespace="DomainServices")]
            [OperationContract(AsyncPattern=true, Action="http://tempuri.org/RIADataService/GetCustomers", ReplyAction="http://tempuri.org/RIADataService/GetCustomersResponse")]
            IAsyncResult BeginGetCustomers(AsyncCallback callback, object asyncState);
            
            QueryResult<Customer> EndGetCustomers(IAsyncResult result);
            
            [FaultContract(typeof(DomainServiceFault), Action="http://tempuri.org/RIADataService/SubmitChangesDomainServiceFault", Name="DomainServiceFault", Namespace="DomainServices")]
            [OperationContract(AsyncPattern=true, Action="http://tempuri.org/RIADataService/SubmitChanges", ReplyAction="http://tempuri.org/RIADataService/SubmitChangesResponse")]
            IAsyncResult BeginSubmitChanges(IEnumerable<ChangeSetEntry> changeSet, AsyncCallback callback, object asyncState);
            
            IEnumerable<ChangeSetEntry> EndSubmitChanges(IAsyncResult result);
        }
        
        internal sealed class RIADataContextEntityContainer : EntityContainer
        {
            
            public RIADataContextEntityContainer()
            {
                this.CreateEntitySet<Address>(EntitySetOperations.All);
                this.CreateEntitySet<Customer>(EntitySetOperations.All);
            }
        }
    }
}
namespace RIADataServicesDemoApp.Web.Model
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Web.Ria.Data;
    using System.Windows.Ria;
    using System.Windows.Ria.Services;
    using System.Xml.Serialization;
    
    
    [DataContract(Namespace="http://schemas.datacontract.org/2004/07/RIADataServicesDemoApp.Web.Model")]
    public sealed partial class Address : Entity
    {
        
        private int _addressId;
        
        private string _city;
        
        private EntityCollection<Customer> _customers;
        
        private EntityCollection<Customer> _customers1;
        
        private string _line1;
        
        private string _line2;
        
        private string _postCode;
        
        #region Extensibility Method Definitions

        /// <summary>
        /// This method is invoked from the constructor once initialization is complete and
        /// can be used for further object setup.
        /// </summary>
        partial void OnCreated();
        partial void OnAddressIdChanging(int value);
        partial void OnAddressIdChanged();
        partial void OnCityChanging(string value);
        partial void OnCityChanged();
        partial void OnLine1Changing(string value);
        partial void OnLine1Changed();
        partial void OnLine2Changing(string value);
        partial void OnLine2Changed();
        partial void OnPostCodeChanging(string value);
        partial void OnPostCodeChanged();

        #endregion
        
        
        /// <summary>
        /// Default constructor.
        /// </summary>
        public Address()
        {
            this.OnCreated();
        }
        
        [DataMember()]
        [Key()]
        public int AddressId
        {
            get
            {
                return this._addressId;
            }
            set
            {
                if ((this._addressId != value))
                {
                    this.ValidateProperty("AddressId", value);
                    this.OnAddressIdChanging(value);
                    this.RaiseDataMemberChanging("AddressId");
                    this._addressId = value;
                    this.RaiseDataMemberChanged("AddressId");
                    this.OnAddressIdChanged();
                }
            }
        }
        
        [DataMember()]
        [Required()]
        [StringLength(20)]
        public string City
        {
            get
            {
                return this._city;
            }
            set
            {
                if ((this._city != value))
                {
                    this.ValidateProperty("City", value);
                    this.OnCityChanging(value);
                    this.RaiseDataMemberChanging("City");
                    this._city = value;
                    this.RaiseDataMemberChanged("City");
                    this.OnCityChanged();
                }
            }
        }
        
        [Association("Address_Customer", "AddressId", "ShippingAddress")]
        [XmlIgnore()]
        public EntityCollection<Customer> Customers
        {
            get
            {
                if ((this._customers == null))
                {
                    this._customers = new EntityCollection<Customer>(this, "Customers", this.FilterCustomers, this.AttachCustomers, this.DetachCustomers);
                }
                return this._customers;
            }
        }
        
        [Association("Address_Customer1", "AddressId", "AlternativeShippingAddress")]
        [XmlIgnore()]
        public EntityCollection<Customer> Customers1
        {
            get
            {
                if ((this._customers1 == null))
                {
                    this._customers1 = new EntityCollection<Customer>(this, "Customers1", this.FilterCustomers1, this.AttachCustomers1, this.DetachCustomers1);
                }
                return this._customers1;
            }
        }
        
        [DataMember()]
        [Required()]
        [StringLength(20)]
        public string Line1
        {
            get
            {
                return this._line1;
            }
            set
            {
                if ((this._line1 != value))
                {
                    this.ValidateProperty("Line1", value);
                    this.OnLine1Changing(value);
                    this.RaiseDataMemberChanging("Line1");
                    this._line1 = value;
                    this.RaiseDataMemberChanged("Line1");
                    this.OnLine1Changed();
                }
            }
        }
        
        [DataMember()]
        public string Line2
        {
            get
            {
                return this._line2;
            }
            set
            {
                if ((this._line2 != value))
                {
                    this.ValidateProperty("Line2", value);
                    this.OnLine2Changing(value);
                    this.RaiseDataMemberChanging("Line2");
                    this._line2 = value;
                    this.RaiseDataMemberChanged("Line2");
                    this.OnLine2Changed();
                }
            }
        }
        
        [DataMember()]
        [Required()]
        [StringLength(20)]
        public string PostCode
        {
            get
            {
                return this._postCode;
            }
            set
            {
                if ((this._postCode != value))
                {
                    this.ValidateProperty("PostCode", value);
                    this.OnPostCodeChanging(value);
                    this.RaiseDataMemberChanging("PostCode");
                    this._postCode = value;
                    this.RaiseDataMemberChanged("PostCode");
                    this.OnPostCodeChanged();
                }
            }
        }
        
        private void AttachCustomers(Customer entity)
        {
            entity.Address = this;
        }
        
        private void DetachCustomers(Customer entity)
        {
            entity.Address = null;
        }
        
        private bool FilterCustomers(Customer entity)
        {
            return (entity.ShippingAddress == this.AddressId);
        }
        
        private void AttachCustomers1(Customer entity)
        {
            entity.Address1 = this;
        }
        
        private void DetachCustomers1(Customer entity)
        {
            entity.Address1 = null;
        }
        
        private bool FilterCustomers1(Customer entity)
        {
            return (entity.AlternativeShippingAddress == this.AddressId);
        }
        
        public override object GetIdentity()
        {
            return this._addressId;
        }
    }
    
    [DataContract(Namespace="http://schemas.datacontract.org/2004/07/RIADataServicesDemoApp.Web.Model")]
    public sealed partial class Customer : Entity
    {
        
        private EntityRef<Address> _address;
        
        private EntityRef<Address> _address1;
        
        private Nullable<int> _alternativeShippingAddress;
        
        private int _customerId;
        
        private string _email;
        
        private string _name;
        
        private string _phone;
        
        private int _shippingAddress;
        
        #region Extensibility Method Definitions

        /// <summary>
        /// This method is invoked from the constructor once initialization is complete and
        /// can be used for further object setup.
        /// </summary>
        partial void OnCreated();
        partial void OnAlternativeShippingAddressChanging(Nullable<int> value);
        partial void OnAlternativeShippingAddressChanged();
        partial void OnCustomerIdChanging(int value);
        partial void OnCustomerIdChanged();
        partial void OnEmailChanging(string value);
        partial void OnEmailChanged();
        partial void OnNameChanging(string value);
        partial void OnNameChanged();
        partial void OnPhoneChanging(string value);
        partial void OnPhoneChanged();
        partial void OnShippingAddressChanging(int value);
        partial void OnShippingAddressChanged();

        #endregion
        
        
        /// <summary>
        /// Default constructor.
        /// </summary>
        public Customer()
        {
            this.OnCreated();
        }
        
        [Association("Address_Customer", "ShippingAddress", "AddressId", IsForeignKey=true)]
        [XmlIgnore()]
        public Address Address
        {
            get
            {
                if ((this._address == null))
                {
                    this._address = new EntityRef<Address>(this, "Address", this.FilterAddress);
                }
                return this._address.Entity;
            }
            set
            {
                Address previous = this.Address;
                if ((previous != value))
                {
                    this.ValidateProperty("Address", value);
                    if ((previous != null))
                    {
                        this._address.Entity = null;
                        previous.Customers.Remove(this);
                    }
                    if ((value != null))
                    {
                        this.ShippingAddress = value.AddressId;
                    }
                    else
                    {
                        this.ShippingAddress = default(int);
                    }
                    this._address.Entity = value;
                    if ((value != null))
                    {
                        value.Customers.Add(this);
                    }
                    this.RaisePropertyChanged("Address");
                }
            }
        }
        
        [Association("Address_Customer1", "AlternativeShippingAddress", "AddressId", IsForeignKey=true)]
        [XmlIgnore()]
        public Address Address1
        {
            get
            {
                if ((this._address1 == null))
                {
                    this._address1 = new EntityRef<Address>(this, "Address1", this.FilterAddress1);
                }
                return this._address1.Entity;
            }
            set
            {
                Address previous = this.Address1;
                if ((previous != value))
                {
                    this.ValidateProperty("Address1", value);
                    if ((previous != null))
                    {
                        this._address1.Entity = null;
                        previous.Customers1.Remove(this);
                    }
                    if ((value != null))
                    {
                        this.AlternativeShippingAddress = value.AddressId;
                    }
                    else
                    {
                        this.AlternativeShippingAddress = default(Nullable<int>);
                    }
                    this._address1.Entity = value;
                    if ((value != null))
                    {
                        value.Customers1.Add(this);
                    }
                    this.RaisePropertyChanged("Address1");
                }
            }
        }
        
        [DataMember()]
        public Nullable<int> AlternativeShippingAddress
        {
            get
            {
                return this._alternativeShippingAddress;
            }
            set
            {
                if ((this._alternativeShippingAddress != value))
                {
                    this.ValidateProperty("AlternativeShippingAddress", value);
                    this.OnAlternativeShippingAddressChanging(value);
                    this.RaiseDataMemberChanging("AlternativeShippingAddress");
                    this._alternativeShippingAddress = value;
                    this.RaiseDataMemberChanged("AlternativeShippingAddress");
                    this.OnAlternativeShippingAddressChanged();
                }
            }
        }
        
        [DataMember()]
        [Key()]
        public int CustomerId
        {
            get
            {
                return this._customerId;
            }
            set
            {
                if ((this._customerId != value))
                {
                    this.ValidateProperty("CustomerId", value);
                    this.OnCustomerIdChanging(value);
                    this.RaiseDataMemberChanging("CustomerId");
                    this._customerId = value;
                    this.RaiseDataMemberChanged("CustomerId");
                    this.OnCustomerIdChanged();
                }
            }
        }
        
        [DataMember()]
        [StringLength(10)]
        public string Email
        {
            get
            {
                return this._email;
            }
            set
            {
                if ((this._email != value))
                {
                    this.ValidateProperty("Email", value);
                    this.OnEmailChanging(value);
                    this.RaiseDataMemberChanging("Email");
                    this._email = value;
                    this.RaiseDataMemberChanged("Email");
                    this.OnEmailChanged();
                }
            }
        }
        
        [DataMember()]
        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                if ((this._name != value))
                {
                    this.ValidateProperty("Name", value);
                    this.OnNameChanging(value);
                    this.RaiseDataMemberChanging("Name");
                    this._name = value;
                    this.RaiseDataMemberChanged("Name");
                    this.OnNameChanged();
                }
            }
        }
        
        [DataMember()]
        public string Phone
        {
            get
            {
                return this._phone;
            }
            set
            {
                if ((this._phone != value))
                {
                    this.ValidateProperty("Phone", value);
                    this.OnPhoneChanging(value);
                    this.RaiseDataMemberChanging("Phone");
                    this._phone = value;
                    this.RaiseDataMemberChanged("Phone");
                    this.OnPhoneChanged();
                }
            }
        }
        
        [DataMember()]
        public int ShippingAddress
        {
            get
            {
                return this._shippingAddress;
            }
            set
            {
                if ((this._shippingAddress != value))
                {
                    this.ValidateProperty("ShippingAddress", value);
                    this.OnShippingAddressChanging(value);
                    this.RaiseDataMemberChanging("ShippingAddress");
                    this._shippingAddress = value;
                    this.RaiseDataMemberChanged("ShippingAddress");
                    this.OnShippingAddressChanged();
                }
            }
        }
        
        private bool FilterAddress(Address entity)
        {
            return (entity.AddressId == this.ShippingAddress);
        }
        
        private bool FilterAddress1(Address entity)
        {
            return (entity.AddressId == this.AlternativeShippingAddress);
        }
        
        public override object GetIdentity()
        {
            return this._customerId;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions