Click here to Skip to main content
15,895,799 members
Articles / Web Development

Service Based Membership Providers for ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.93/5 (45 votes)
4 May 2014CPOL29 min read 132.1K   8.2K   173  
Service based, multi-application ASP.NET custom membership, role and profile providers with a hierarchical role system.
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool from CryptoGateway Software Inc.
//     Tool name: CGW X-Script RDB visual Layer Generator
//
//     Archymeta Information Technologies Co., Ltd.
//
//     Changes to this file, could be overwritten if the code is re-generated.
//     Add (if not yet) a code-manager node to the generator to specify 
//     how existing files are processed.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;

namespace CryptoGateway.RDB.Data.AspNetMember
{
    /// <summary>
    /// A entity in "Roles" data set. The set is a self-referencing hierarchic one.
    /// </summary>
    /// <remarks>
    ///  <para>
    ///    Properties of the entity are categorized in the following:
    ///  </para>
    ///  <list type="table">
    ///    <listheader>
    ///       <term>Primary keys</term><description>Description</description>
    ///    </listheader>
    ///    <item>
    ///      <term>ID</term>
    ///      <description>See <see cref="Role.ID" />. Auto-gen primary key; fixed; not null.</description>
    ///    </item>
    ///  </list>
    ///  <list type="table">
    ///    <listheader>
    ///       <term>Intrinsic Identifiers</term><description>Description</description>
    ///    </listheader>
    ///    <item>
    ///      <term>RoleName</term>
    ///      <description>See <see cref="Role.RoleName" />. Intrinsic id; fixed; not null; max-length = 80 characters.</description>
    ///    </item>
    ///    <item>
    ///      <term>ApplicationID</term>
    ///      <description>See <see cref="Role.ApplicationID" />. Intrinsic id; fixed; not null; foreign key.</description>
    ///    </item>
    ///    <item>
    ///      <term>ParentID</term>
    ///      <description>See <see cref="Role.ParentID" />. Intrinsic id; fixed; nullable; foreign key.</description>
    ///    </item>
    ///  </list>
    ///  <list type="table">
    ///    <listheader>
    ///       <term>Editable properties</term><description>Description</description>
    ///    </listheader>
    ///    <item>
    ///      <term>Description</term>
    ///      <description>See <see cref="Role.Description" />. Editable; nullable; load delayed; max-length = 256 characters.</description>
    ///    </item>
    ///    <item>
    ///      <term>DisplayName</term>
    ///      <description>See <see cref="Role.DisplayName" />. Editable; nullable; max-length = 128 characters.</description>
    ///    </item>
    ///  </list>
    ///  <list type="table">
    ///    <listheader>
    ///       <term>Foreign keys</term><description>Description</description>
    ///    </listheader>
    ///    <item>
    ///      <term>ApplicationID</term>
    ///      <description>See <see cref="Role.ApplicationID" />. Intrinsic id; fixed; not null; foreign key.</description>
    ///    </item>
    ///    <item>
    ///      <term>ParentID</term>
    ///      <description>See <see cref="Role.ParentID" />. Intrinsic id; fixed; nullable; foreign key.</description>
    ///    </item>
    ///  </list>
    ///  <list type="table">
    ///    <listheader>
    ///       <term>This entity depends on</term><description>Description</description>
    ///    </listheader>
    ///    <item>
    ///      <term>Application_Ref</term>
    ///      <description>See <see cref="Role.Application_Ref" />, which is a member of the data set "Applications" for <see cref="Application_" />.</description>
    ///    </item>
    ///    <item>
    ///      <term>UpperRef</term>
    ///      <description>See <see cref="Role.UpperRef" />, which is a member of the data set "Roles" for <see cref="Role" />. Self-referencing. Nullable.</description>
    ///    </item>
    ///  </list>
    ///  <list type="table">
    ///    <listheader>
    ///       <term>The following entity sets depend on this entity</term><description>Description</description>
    ///    </listheader>
    ///    <item>
    ///      <term>Roles</term>
    ///      <description>See <see cref="Role.Roles" />, which is a sub-set of the data set "Roles" for <see cref="Role" />. Self-referencing.</description>
    ///    </item>
    ///    <item>
    ///      <term>UsersInRoles</term>
    ///      <description>See <see cref="Role.UsersInRoles" />, which is a sub-set of the data set "UsersInRoles" for <see cref="UsersInRole" />.</description>
    ///    </item>
    ///  </list>
    /// </remarks>
    [DataContract]
    public class Role : IDbEntity 
    {
        /// <summary>
        /// For internal use only.
        /// </summary>
        public bool IsOperationHandled = false;

        /// <summary>
        /// Used on the server side to return an unique key for caching purposes.
        /// </summary>
        public string CacheKey
        {
            get
            {
                return this.ID.ToString();
            }
        }

        /// <summary>
        /// Whether or not the entity was already persisted into to the data source. 
        /// </summary>
        [DataMember]
        public bool IsPersisted
        {
            get { return _isPersisted; }
            set { _isPersisted = value; }
        }
        private bool _isPersisted = false;

        /// <summary>
        /// Used internally.
        /// </summary>
        public bool IsInitializing
        {
            get { return _isInitializing; }
            set { _isInitializing = value; }
        }
        private bool _isInitializing = false;

        /// <summary>
        /// Used to matching entities in input adding or updating entity list and the returned ones, see <see cref="IRoleService.AddOrUpdateEntities" />.
        /// </summary>
        [DataMember]
        public int UpdateIndex
        {
            get { return _updateIndex; }
            set { _updateIndex = value; }
        }
        private int _updateIndex = -1;

        /// <summary>
        /// Configured at system generation step, its value provides a short, but characteristic summary of the entity.
        /// </summary>
        [DataMember]
        public string DistinctString
        {
            get 
            {
                if (_distinctString == null)
                    _distinctString = GetDistinctString(true);
                return _distinctString;
            }
            set
            {
                _distinctString = value;
            }
        }
        private string _distinctString = null;

        private string GetDistinctString(bool ShowPathInfo)
        {
            LoadUpperRef();
            LoadApplication_Ref();
            string str = String.Format(@"{0}>{1} in {2}", (UpperRef != null && ShowPathInfo ? UpperRef.GetFullEntityPath(">") : ""), RoleName.Trim(), (Application_Ref != null ? Application_Ref.DistinctString : ""));
            str = str.Trim(">".ToCharArray());
            return str;
        }

        /// <summary>
        /// Get the full path of an entity in a self-referencing set.
        /// </summary>
        /// <param name="sep">The node separator to be used.</param>
        /// <returns>
        ///   The full path.
        /// </returns>
        public string GetFullEntityPath(string sep)
        {
            string pstr = GetDistinctString(false);
            LoadUpperRef();
            Role e = UpperRef;
            while (e != null)
            {
                pstr = e.GetDistinctString(false) + sep + pstr.TrimStart(sep.ToCharArray());
                e = e.UpperRef;
            }
            return pstr;
        }

        /// <summary>
        /// Whether or not the entity was edited.
        /// </summary>
        [DataMember]
        public bool IsEntityChanged
        {
            get { return _isEntityChanged; }
            set { _isEntityChanged = value; }
        }
        private bool _isEntityChanged = true;

        /// <summary>
        /// Whether or not the entity was to be deleted.
        /// </summary>
        [DataMember]
        public bool IsDeleted
        {
            get { return _isDeleted; }
            set { _isDeleted = value; }
        }
        private bool _isDeleted = false;

        /// <summary>
        /// Meta-info: auto-gen primary key; fixed; not null.
        /// </summary>
        [Key]
        [Editable(false)]
        [DataMember(IsRequired = false)]
        public int ID
        { 
            get
            {
                return _ID;
            }
            set
            {
                if (_ID != value)
                {
                    _ID = value;
                }
            }
        }
        private int _ID = default(int);

        /// <summary>
        /// Meta-info: intrinsic id; fixed; not null; max-length = 80 characters.
        /// </summary>
        [Key]
        [Required]
        [Editable(false)]
        [StringLength(80)]
        [DataMember(IsRequired = true)]
        public string RoleName
        { 
            get
            {
                return _RoleName;
            }
            set
            {
                if (_RoleName != value)
                {
                    _RoleName = value;
                }
            }
        }
        private string _RoleName = default(string);

        /// <summary>
        /// Meta-info: editable; nullable; load delayed; max-length = 256 characters.
        /// </summary>
        [Editable(true)]
        [StringLength(256)]
        [DataMember(IsRequired = false)]
        public string Description
        { 
            get
            {
                return _Description;
            }
            set
            {
                if (_Description != value)
                {
                    _Description = value;
                    if (!IsInitializing)
                        IsDescriptionModified = true;
                }
            }
        }
        private string _Description = default(string);

        /// <summary>
        /// Wether or not the value of <see cref="Description" /> was changed compared to what it was loaded last time. 
        /// Note: the backend data source updates the changed <see cref="Description" /> only if this is set to true no matter what
        /// the actual value of <see cref="Description" /> is.
        /// </summary>
        [DataMember]
        public bool IsDescriptionModified
        { 
            get
            {
                return _isDescriptionModified;
            }
            set
            {
                _isDescriptionModified = value;
            }
        }
        private bool _isDescriptionModified = false;

        /// <summary>
        /// Wether or not the value of the delay loaded "Description" is Loaded. Clients are responsible for keeping 
        /// track of loading status of delay loading properties.
        /// </summary>
        [DataMember]
        public bool IsDescriptionLoaded
        { 
            get
            {
                return _isDescriptionLoaded;
            }
            set
            {
                _isDescriptionLoaded = value;
            }
        }
        private bool _isDescriptionLoaded = false;

        /// <summary>
        /// Meta-info: editable; nullable; max-length = 128 characters.
        /// </summary>
        [Editable(true)]
        [StringLength(128)]
        [DataMember(IsRequired = false)]
        public string DisplayName
        { 
            get
            {
                return _DisplayName;
            }
            set
            {
                if (_DisplayName != value)
                {
                    _DisplayName = value;
                    if (!IsInitializing)
                        IsDisplayNameModified = true;
                }
            }
        }
        private string _DisplayName = default(string);

        /// <summary>
        /// Wether or not the value of <see cref="DisplayName" /> was changed compared to what it was loaded last time. 
        /// Note: the backend data source updates the changed <see cref="DisplayName" /> only if this is set to true no matter what
        /// the actual value of <see cref="DisplayName" /> is.
        /// </summary>
        [DataMember]
        public bool IsDisplayNameModified
        { 
            get
            {
                return _isDisplayNameModified;
            }
            set
            {
                _isDisplayNameModified = value;
            }
        }
        private bool _isDisplayNameModified = false;

        /// <summary>
        /// Meta-info: intrinsic id; fixed; not null; foreign key.
        /// </summary>
        [Key]
        [Required]
        [Editable(false)]
        [DataMember(IsRequired = true)]
        public string ApplicationID
        { 
            get
            {
                return _ApplicationID;
            }
            set
            {
                if (_ApplicationID != value)
                {
                    _ApplicationID = value;
                }
            }
        }
        private string _ApplicationID = default(string);

        /// <summary>
        /// Meta-info: intrinsic id; fixed; nullable; foreign key.
        /// </summary>
        [Key]
        [Editable(false)]
        [DataMember(IsRequired = false)]
        public System.Nullable<int> ParentID
        { 
            get
            {
                return _ParentID;
            }
            set
            {
                if (_ParentID != value)
                {
                    _ParentID = value;
                }
            }
        }
        private System.Nullable<int> _ParentID = default(System.Nullable<int>);

        /// <summary>
        /// Entity in data set "Applications" for <see cref="Application_" /> that this entity depend upon.
        /// The corresponding foreign key set is { <see cref="Role.ApplicationID" /> }.
        /// </summary>
        [DataMember]
        public Application_ Application_Ref
        {
            get 
            {
                if (_Application_Ref == null && AutoLoadApplication_Ref != null)
                    _Application_Ref = AutoLoadApplication_Ref();
                return _Application_Ref; 
            }
            set 
            { 
                _Application_Ref = value; 
            }
        }
        private Application_ _Application_Ref = null;

        /// <summary>
        /// <see cref="Role.Application_Ref" /> is not initialized when the entity is created. Clients could call this method to load it provided a proper delegate <see cref="Role.DelLoadApplication_Ref" /> was setup
        /// before calling it.
        /// </summary>
        public void LoadApplication_Ref()
        {
            if (_Application_Ref != null)
                return;
            if (DelLoadApplication_Ref != null)
                _Application_Ref = DelLoadApplication_Ref();
        }

        /// <summary>
        /// A delegate to load <see cref="Role.Application_Ref" />.
        /// </summary>
        public Func<Application_> DelLoadApplication_Ref = null;

        /// <summary>
        /// A delegate to load <see cref="Role.Application_Ref" /> automatically when it is referred to at the first time.
        /// </summary>
        public Func<Application_> AutoLoadApplication_Ref = null;

        /// <summary>
        /// Entity in data set "Roles" for <see cref="Role" /> that this entity depend upon.
        /// The corresponding foreign key set is { <see cref="Role.ParentID" /> }.
        /// This is the hierarchic parent entity of the current one for this self-referencing data set.
        /// </summary>
        [DataMember]
        public Role UpperRef
        {
            get 
            {
                if (ParentID == null)
                    return null;
                else if (_UpperRef == null && AutoLoadUpperRef != null)
                    _UpperRef = AutoLoadUpperRef();
                return _UpperRef; 
            }
            set 
            { 
                _UpperRef = value; 
            }
        }
        private Role _UpperRef = null;

        /// <summary>
        /// <see cref="Role.UpperRef" /> is not initialized when the entity is created. Clients could call this method to load it provided a proper delegate <see cref="Role.DelLoadUpperRef" /> was setup
        /// before calling it.
        /// </summary>
        public void LoadUpperRef()
        {
            if (ParentID == null || _UpperRef != null)
                return;
            if (DelLoadUpperRef != null)
                _UpperRef = DelLoadUpperRef();
        }

        /// <summary>
        /// A delegate to load <see cref="Role.UpperRef" />.
        /// </summary>
        public Func<Role> DelLoadUpperRef = null;

        /// <summary>
        /// A delegate to load <see cref="Role.UpperRef" /> automatically when it is referred to at the first time.
        /// </summary>
        public Func<Role> AutoLoadUpperRef = null;

        /// <summary>
        /// Entitity set <see cref="RoleSet" /> for data set "Roles" of <see cref="Role" /> that depend on the current entity.
        /// This is the hierarchic child entity set for this self-referencing data set.
        /// </summary>
        [DataMember]
		public RoleSet Roles
		{
			get
			{
                if (_Roles == null)
                    _Roles = new RoleSet();
				return _Roles;
			}
            set
            {
                _Roles = value;
            }
		}
		private RoleSet _Roles = null;

        /// <summary>
        /// Entitites enumeration expression for data set "Roles" of <see cref="Role" /> that depend on the current entity.
        /// This is the hierarchic child enumeration for this self-referencing data set.
        /// </summary>
		public IEnumerable<Role> RoleEnum
		{
			get;
            set;
		}

        /// <summary>
        /// A list of <see cref="Role" /> that is to be added or updated to the data source, together with the current entity.
        /// </summary>
        [DataMember]
		public Role[] ChangedRoles
		{
			get;
            set;
		}

        /// <summary>
        /// Entitity set <see cref="UsersInRoleSet" /> for data set "UsersInRoles" of <see cref="UsersInRole" /> that depend on the current entity.
        /// </summary>
        [DataMember]
		public UsersInRoleSet UsersInRoles
		{
			get
			{
                if (_UsersInRoles == null)
                    _UsersInRoles = new UsersInRoleSet();
				return _UsersInRoles;
			}
            set
            {
                _UsersInRoles = value;
            }
		}
		private UsersInRoleSet _UsersInRoles = null;

        /// <summary>
        /// Entitites enumeration expression for data set "UsersInRoles" of <see cref="UsersInRole" /> that depend on the current entity.
        /// </summary>
		public IEnumerable<UsersInRole> UsersInRoleEnum
		{
			get;
            set;
		}

        /// <summary>
        /// A list of <see cref="UsersInRole" /> that is to be added or updated to the data source, together with the current entity.
        /// </summary>
        [DataMember]
		public UsersInRole[] ChangedUsersInRoles
		{
			get;
            set;
		}

        /// <summary>
        /// Whether or not the present entity is identitical to <paramref name="other" />, in the sense that they have the same (set of) primary key(s).
        /// </summary>
        /// <param name="other">The entity to be compared to.</param>
        /// <returns>
        ///   The result of comparison.
        /// </returns>
        public bool IsEntityIdentical(Role other)
        {
            if (other == null)
                return false;
            if (ID != other.ID)
                return false;
            return true;
        }              

        /// <summary>
        /// Whether or not the present entity is identitical to <paramref name="other" />, in the sense that they have the same (set of) intrinsic identifiers.
        /// </summary>
        /// <param name="other">The entity to be compared to.</param>
        /// <returns>
        ///   The result of comparison.
        /// </returns>
        public bool IsEntityTheSame(Role other)
        {
            if (other == null)
                return false;
            else
                return RoleName == other.RoleName &&  ApplicationID == other.ApplicationID &&  ParentID == other.ParentID;
        }              

        /// <summary>
        /// Merge changes inside entity <paramref name="from" /> to the entity <paramref name="to" />. Any changes in <paramref name="from" /> that is not changed in <paramref name="to" /> is updated inside <paramref name="to" />.
        /// </summary>
        /// <param name="from">The "old" entity acting as merging source.</param>
        /// <param name="to">The "new" entity which inherits changes made in <paramref name="from" />.</param>
        /// <returns>
        /// </returns>
        public static void MergeChanges(Role from, Role to)
        {
            if (to.IsPersisted)
            {
                if (from.IsDescriptionModified && !to.IsDescriptionModified)
                {
                    to.Description = from.Description;
                    to.IsDescriptionModified = true;
                }
                if (from.IsDisplayNameModified && !to.IsDisplayNameModified)
                {
                    to.DisplayName = from.DisplayName;
                    to.IsDisplayNameModified = true;
                }
            }
            else
            {
                to.IsPersisted = from.IsPersisted;
                to.ID = from.ID;
                to.RoleName = from.RoleName;
                to.Description = from.Description;
                to.IsDescriptionModified = from.IsDescriptionModified;
                to.DisplayName = from.DisplayName;
                to.IsDisplayNameModified = from.IsDisplayNameModified;
                to.ApplicationID = from.ApplicationID;
                to.ParentID = from.ParentID;
            }
        }

        /// <summary>
        /// Update changes to the current entity compared to an input <paramref name="newdata" /> and set the entity to a proper state for updating.
        /// </summary>
        /// <param name="newdata">The "new" entity acting as the source of the changes, if any.</param>
        /// <returns>
        /// </returns>
        public void UpdateChanges(Role newdata)
        {
            int cnt = 0;
            if (Description != newdata.Description)
            {
                Description = newdata.Description;
                IsDescriptionModified = true;
                cnt++;
            }
            if (DisplayName != newdata.DisplayName)
            {
                DisplayName = newdata.DisplayName;
                IsDisplayNameModified = true;
                cnt++;
            }
            IsEntityChanged = cnt > 0;
        }

        /// <summary>
        /// Internal use
        /// </summary>
        public void NormalizeValues()
        {
            IsInitializing = true;
            if (RoleName == null)
                RoleName = "";
            if (ApplicationID == null)
                ApplicationID = "";
            if (!IsEntityChanged)
                IsEntityChanged = IsDescriptionModified || IsDisplayNameModified;
            if (IsDescriptionModified && !IsDescriptionLoaded)
                IsDescriptionLoaded = true;
            IsInitializing = false;
        }

        /// <summary>
        /// Internal use
        /// </summary>
        public Role ShallowCopy(bool allData = false)
        {
            Role e = new Role();
            e.IsInitializing = true;
            e.ID = ID;
            e.RoleName = RoleName;
            e.DisplayName = DisplayName;
            e.ApplicationID = ApplicationID;
            e.ParentID = ParentID;
            if (allData)
            {
                e.Description = Description;
            }
            e.DistinctString = GetDistinctString(true);
            e.IsPersisted = true;
            e.IsEntityChanged = false;
            e.IsInitializing = false;
            return e;
        }

        /// <summary>
        /// A textual representation of the entity.
        /// </summary>
        public override string ToString()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"
----===== [[Role]] =====----
  ID = " + ID + @"
  RoleName = '" + RoleName + @"'");
            sb.Append(@" (natural id)");
            sb.Append(@"
  DisplayName = '" + (DisplayName != null ? DisplayName : "null") + @"'");
            if (IsDisplayNameModified)
                sb.Append(@" (modified)");
            else
                sb.Append(@" (unchanged)");
            sb.Append(@"
  ApplicationID = '" + ApplicationID + @"'");
            sb.Append(@" (natural id)");
            sb.Append(@"
  ParentID = " + (ParentID.HasValue ? ParentID.Value.ToString() : "null") + @"");
            sb.Append(@" (natural id)");
            sb.Append(@"
");
            return sb.ToString();
        }

    }

    ///<summary>
    ///The result of an add or update of type <see cref="Role" />.
    ///</summary>
    [DataContract]
    public class RoleUpdateResult : IUpdateResult
    {
        /// <summary>
        /// Status of add, update or delete operation
        /// </summary>
        [DataMember]
        public int OpStatus
        {
            get { return _opStatus; }
            set { _opStatus = value; }
        }
        private int _opStatus = (int)EntityOpStatus.Unknown;

        /// <summary>
        /// Parents or child operation status
        /// </summary>
        [DataMember]
        public int RelatedOpStatus
        {
            get { return _relatedOpStatus; }
            set { _relatedOpStatus = value; }
        }
        private int _relatedOpStatus = (int)EntityOpStatus.Unknown;

        /// <summary>
        /// The updated entity.
        /// </summary>
        [DataMember]
        public Role UpdatedItem
        {
            get;
            set;
        }

        /// <summary>
        /// If the relational data source has a way of detecting concurrent update conflicts, this is the item inside the
        /// data source that had been updated by other agents in between the load and update time interval of the present
        /// agent. The client software should resolve the conflict.
        /// </summary>
        [DataMember]
        public Role ConflictItem
        {
            get;
            set;
        }

        /// <summary>
        /// String representation of the entity.
        /// </summary>
        public string EntityInfo 
        { 
            get { return UpdatedItem == null ? "NULL" : UpdatedItem.ToString(); }
        }
    }

}

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
Founder CryptoGateway Software Inc.
Canada Canada


His interests in software research and development include security, domain specific meta-language development technologies and tools, meta-language driven code generation, generation of non-invasive and virtual SQL database for "un-structured" data (sqlization), automated relational data service production technologies, file system sqlization technologies and products, structured multi-lingual documentation tools and technologies, sqlization of user's personal data, like emails, etc..


He was a physicist before year 2000. He studied theoretical physics (the hidden symmetry between the past and future, quantum field theories, mirror universe, cosmological dark energies etc.) in which he think to had made fundamental breakthroughs[^] but the community is either not ready for it or is actively rejecting/ignoring it Smile | :) .



It struck me on Apr. 11, 2023 that the said theory above can even generate General Relativity naturally after a recent discussion in the Insider News group that triggers a rethinking of the subject on my side. The first stage of the work is completed in Sept. 2023, it is and will be continue to be published online

  • On Vacuum
  • Quantum and Gravity







    Most recent software system to share:



    • V-NET[^] - Full stack virtualization management system including networking, storage, virtual machines and containers, with an emphasis on networking ... to be released.

Comments and Discussions