Click here to Skip to main content
15,885,979 members
Articles / Programming Languages / C#

The Power of a POC

Rate me:
Please Sign up or sign in to vote.
4.42/5 (6 votes)
26 Apr 2011CPOL13 min read 49.9K   358   19  
The importance of creating a POC as the first step to your next adventure
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Runtime.Serialization;

using EntityModel = AdventureWorksEntityModel;


namespace AdventureWorksModel
{
    [DataContract(IsReference = true)]
    public partial class Contact : PocoBase, IExtensibleDataObject
    {
   		// Class Name
		public const string CONTACT = "Contact";
		
		// Property Names
        public const string CONTACT_ID = "ContactID";
        public const string ADDITIONAL_CONTACT_INFO = "AdditionalContactInfo";
        public const string EMAIL_ADDRESS = "EmailAddress";
        public const string EMAIL_PROMOTION = "EmailPromotion";
        public const string FIRST_NAME = "FirstName";
        public const string LAST_NAME = "LastName";
        public const string MIDDLE_NAME = "MiddleName";
        public const string MODIFIED_DATE = "ModifiedDate";
        public const string NAME_STYLE = "NameStyle";
        public const string PASSWORD_HASH = "PasswordHash";
        public const string PASSWORD_SALT = "PasswordSalt";
        public const string PHONE = "Phone";
        public const string ROWGUID = "rowguid";
        public const string SUFFIX = "Suffix";
        public const string TITLE = "Title";

		// Data Members - Keys
        [DataMember(EmitDefaultValue = false)] public Int32 ContactID { get; private set; }

		// Data Members
        [DataMember(EmitDefaultValue = false)] public String AdditionalContactInfo { get; set; }
        [DataMember(EmitDefaultValue = false)] public String EmailAddress { get; set; }
        [DataMember(EmitDefaultValue = false)] public Int32 EmailPromotion { get; set; }
        [DataMember(EmitDefaultValue = false)] public String FirstName { get; set; }
        [DataMember(EmitDefaultValue = false)] public String LastName { get; set; }
        [DataMember(EmitDefaultValue = false)] public String MiddleName { get; set; }
        [DataMember(EmitDefaultValue = false)] public DateTime ModifiedDate { get; set; }
        [DataMember(EmitDefaultValue = false)] public Boolean NameStyle { get; set; }
        [DataMember(EmitDefaultValue = false)] public String PasswordHash { get; set; }
        [DataMember(EmitDefaultValue = false)] public String PasswordSalt { get; set; }
        [DataMember(EmitDefaultValue = false)] public String Phone { get; set; }
        [DataMember(EmitDefaultValue = false)] public Guid rowguid { get; set; }
        [DataMember(EmitDefaultValue = false)] public String Suffix { get; set; }
        [DataMember(EmitDefaultValue = false)] public String Title { get; set; }

		// References back to Contact
        [DataMember(EmitDefaultValue = false)] public IList<Individual> Individual { get; set; }
        [DataMember(EmitDefaultValue = false)] public IList<SalesOrderHeader> SalesOrderHeader { get; set; }

		public ExtensionDataObject ExtensionData { get; set; }

		//
		// Public Class Methods
		//

   		public override IList<KeyValuePair<string, object>> GetKeys()
    	{
			return new List<KeyValuePair<string, object>> 
			{ 
				new KeyValuePair<string, object>(CONTACT_ID, ContactID)
			};
    	}

    	public PocoReference<Contact> GetReference()
		{
			PocoReference<Contact> reference = GetReference(ContactID);
			reference.PocoKey.ID = ObjectID;
			return reference; 
		}

		public static PocoReference<Contact> GetReference(Int32 contactID)
		{
			List<KeyValuePair<string, object>> pairs = new List<KeyValuePair<string, object>> 
			{ 
				new KeyValuePair<string, object>(CONTACT_ID, contactID)
			};

			return GetReference(new PocoKey { Name = CONTACT, KeyValues = pairs });
		}		
    	
    	private static PocoReference<Contact> GetReference(PocoKey pocoKey)
		{
			return new PocoReference<Contact>
			{
				PocoKey = pocoKey
			};
		}

		//
		// Repository Calls
		//
		
		public override string EntitySetName()
    	{
			return Repository.EntitySetName<Contact, EntityModel.Contact>(false);
    	}

		public override IQueryable<PocoBase> Find()
    	{
			return Repository.Find<Contact, EntityModel.Contact>(SearchOptions, MergeOption.NoTracking).Cast<PocoBase>();
    	}

		public override PocoKey Add()
    	{
			PocoKey key = Repository.Add<Contact, EntityModel.Contact>(this);
			PocoReference<Contact> reference = GetReference(key);
			
			ContactID = (Int32)key.KeyValues.FirstOrDefault(k=> k.Key.Equals(CONTACT_ID)).Value;

			if (Individual != null)
			{
				foreach (Individual individual in Individual)
				{
					if (individual.Contact != null && individual.Contact.ObjectID == ObjectID)
						individual.ContactReference = reference;
				}
			}
			
			if (SalesOrderHeader != null)
			{
				foreach (SalesOrderHeader salesOrderHeader in SalesOrderHeader)
				{
					if (salesOrderHeader.Contact != null && salesOrderHeader.Contact.ObjectID == ObjectID)
						salesOrderHeader.ContactReference = reference;
				}
			}
			
			return key;
    	}
    	
		public override void Update()
    	{
			Repository.Update<Contact, EntityModel.Contact>(this);
    	}
    	
		public override void Delete()
		{
			Repository.Delete<Contact, EntityModel.Contact>(this);
	
			if (Individual != null)
			{
				foreach (Individual individual in Individual)
				{
					if (individual.Contact == this)
					{
						individual.Contact = null;
						individual.ContactReference = null;
					}
				}
			}
	
			if (SalesOrderHeader != null)
			{
				foreach (SalesOrderHeader salesOrderHeader in SalesOrderHeader)
				{
					if (salesOrderHeader.Contact == this)
					{
						salesOrderHeader.Contact = null;
						salesOrderHeader.ContactReference = null;
					}
				}
			}
		}
	}
}

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) Webbert Solutions
United States United States
Dave is an independent consultant working in a variety of industries utilizing Microsoft .NET technologies.

Comments and Discussions