Click here to Skip to main content
15,896,512 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 50K   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 ProductModel : PocoBase, IExtensibleDataObject
    {
   		// Class Name
		public const string PRODUCT_MODEL = "ProductModel";
		
		// Property Names
        public const string PRODUCT_MODEL_ID = "ProductModelID";
        public const string CATALOG_DESCRIPTION = "CatalogDescription";
        public const string INSTRUCTIONS = "Instructions";
        public const string MODIFIED_DATE = "ModifiedDate";
        public const string NAME = "Name";
        public const string ROWGUID = "rowguid";

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

		// Data Members
        [DataMember(EmitDefaultValue = false)] public String CatalogDescription { get; set; }
        [DataMember(EmitDefaultValue = false)] public String Instructions { get; set; }
        [DataMember(EmitDefaultValue = false)] public DateTime ModifiedDate { get; set; }
        [DataMember(EmitDefaultValue = false)] public String Name { get; set; }
        [DataMember(EmitDefaultValue = false)] public Guid rowguid { get; set; }

		// References back to ProductModel
        [DataMember(EmitDefaultValue = false)] public IList<Product> Product { get; set; }
        [DataMember(EmitDefaultValue = false)] public IList<ProductModelIllustration> ProductModelIllustration { 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>(PRODUCT_MODEL_ID, ProductModelID)
			};
    	}

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

		public static PocoReference<ProductModel> GetReference(Int32 productModelID)
		{
			List<KeyValuePair<string, object>> pairs = new List<KeyValuePair<string, object>> 
			{ 
				new KeyValuePair<string, object>(PRODUCT_MODEL_ID, productModelID)
			};

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

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

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

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

			if (Product != null)
			{
				foreach (Product product in Product)
				{
					if (product.ProductModel != null && product.ProductModel.ObjectID == ObjectID)
						product.ProductModelReference = reference;
				}
			}
			
			if (ProductModelIllustration != null)
			{
				foreach (ProductModelIllustration productModelIllustration in ProductModelIllustration)
				{
					if (productModelIllustration.ProductModel != null && productModelIllustration.ProductModel.ObjectID == ObjectID)
						productModelIllustration.ProductModelReference = reference;
				}
			}
			
			return key;
    	}
    	
		public override void Update()
    	{
			Repository.Update<ProductModel, EntityModel.ProductModel>(this);
    	}
    	
		public override void Delete()
		{
			Repository.Delete<ProductModel, EntityModel.ProductModel>(this);
	
			if (Product != null)
			{
				foreach (Product product in Product)
				{
					if (product.ProductModel == this)
					{
						product.ProductModel = null;
						product.ProductModelReference = null;
					}
				}
			}
	
			if (ProductModelIllustration != null)
			{
				foreach (ProductModelIllustration productModelIllustration in ProductModelIllustration)
				{
					if (productModelIllustration.ProductModel == this)
					{
						productModelIllustration.ProductModel = null;
						productModelIllustration.ProductModelReference = 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