Click here to Skip to main content
15,881,139 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 ProductDocument : PocoBase, IExtensibleDataObject
    {
   		// Class Name
		public const string PRODUCT_DOCUMENT = "ProductDocument";
		
		// Property Names
        public const string DOCUMENT_ID = "DocumentID";
        public const string PRODUCT_ID = "ProductID";
        public const string MODIFIED_DATE = "ModifiedDate";

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

		// Data Members
        [DataMember(EmitDefaultValue = false)] public DateTime ModifiedDate { get; set; }

		// Reference (FK) to other Table (PK)	
		[DataMember(EmitDefaultValue = false)] public Document Document { get; set; }
		[DataMember(EmitDefaultValue = false)] public PocoReference<Document> DocumentReference { get; set; }
		
		[DataMember(EmitDefaultValue = false)] public Product Product { get; set; }
		[DataMember(EmitDefaultValue = false)] public PocoReference<Product> ProductReference { 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>(DOCUMENT_ID, DocumentID),
				new KeyValuePair<string, object>(PRODUCT_ID, ProductID)
			};
    	}

    	public PocoReference<ProductDocument> GetReference()
		{
			PocoReference<ProductDocument> reference = GetReference(DocumentID, ProductID);
			reference.PocoKey.ID = ObjectID;
			return reference; 
		}

		public static PocoReference<ProductDocument> GetReference(Int32 documentID, Int32 productID)
		{
			List<KeyValuePair<string, object>> pairs = new List<KeyValuePair<string, object>> 
			{ 
				new KeyValuePair<string, object>(DOCUMENT_ID, documentID),
				new KeyValuePair<string, object>(PRODUCT_ID, productID)
			};

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

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

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

		public override PocoKey Add()
    	{
			PocoKey key = Repository.Add<ProductDocument, EntityModel.ProductDocument>(this);
			PocoReference<ProductDocument> reference = GetReference(key);
			
			DocumentID = (Int32)key.KeyValues.FirstOrDefault(k=> k.Key.Equals(DOCUMENT_ID)).Value;
			ProductID = (Int32)key.KeyValues.FirstOrDefault(k=> k.Key.Equals(PRODUCT_ID)).Value;

			return key;
    	}
    	
		public override void Update()
    	{
			Repository.Update<ProductDocument, EntityModel.ProductDocument>(this);
    	}
    	
		public override void Delete()
		{
			Repository.Delete<ProductDocument, EntityModel.ProductDocument>(this);
		}
	}
}

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