Skip to content

class members concerns the server side only won't be exposed to the client side

fonlow edited this page Jan 30, 2024 · 1 revision

In a typical Web API that deals with business logic rather than only visual effects of AJAX, the POCO classes of domain models may have some members for Entity Framework only, like this one:

    [DataContract(Namespace = Constants.DataNamespace)]
    [Serializable]
    public class Entity : ITrackableEntity
    {
        public Entity()
        {
            Sections = new List<Section>();
            Relationships = new List<Relationship>();
            Tags = new List<EntityTagJunction>();
        }

        [DataMember]
        DateTime createdUtc;

        public DateTime CreatedUtc
        {
            get { return createdUtc; }
            set
            {
                if (value.Kind != DateTimeKind.Utc)
                {
                    createdUtc = DateTime.SpecifyKind(value, DateTimeKind.Utc);
                }
                else
                {
                    createdUtc = value;
                }
            }
        }

        DateTime modifiedUtc;

        public DateTime ModifiedUtc
        {
            get { return modifiedUtc; }
            set
            {
                if (value.Kind != DateTimeKind.Utc)
                {
                    modifiedUtc = DateTime.SpecifyKind(value, DateTimeKind.Utc);
                }
                else
                {
                    modifiedUtc = value;
                }
            }
        }

        public long OwnerId { get; set; }

        public virtual Owner Owner { get; set; }

        [DataMember]
        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }

        [DataMember]
        public string Description { get; set; }


...
    } 

Obviously there are properties for foreign keys and time stamps of the database, which should not concern the client programs. The DataContractAttribute provides a clear semantic way of cherry picking class members for client proxy classes. This is a fairly basic practice in WCF, and WebApiClientGen endorse such practice for ASP.NET Web API.

Clone this wiki locally