/// <summary>
        /// Generates the object layer for the given model
        /// </summary>
        /// <param name="compileUnit">The compile unit to add code to</param>
        /// <param name="model">The model to base the object layer on</param>
        public void GenerateObjectLayer(CodeCompileUnit compileUnit, EntityModelSchema model)
        {
            var namespaces = model.EntityTypes.Select(et => et.NamespaceName).Union(model.ComplexTypes.Select(ct => ct.NamespaceName)).Union(model.Functions.Select(f => f.NamespaceName)).Distinct().ToList();

            foreach (string namespaceName in namespaces)
            {
                CodeNamespace codeNamespace = Code.AddNamespace(compileUnit, namespaceName);

                foreach (string ns in this.GetNamespaceImports())
                {
                    codeNamespace.ImportNamespace(ns);
                }

                foreach (ComplexType type in model.ComplexTypes.Where(ct => ct.NamespaceName == namespaceName))
                {
                    if (this.ShouldGenerateTypeDefinition(type))
                    {
                        this.DeclareComplexType(type, codeNamespace.DeclareType(type.Name));
                    }
                }

                foreach (EntityType type in model.EntityTypes.Where(ct => ct.NamespaceName == namespaceName))
                {
                    if (this.ShouldGenerateTypeDefinition(type))
                    {
                        this.DeclareEntityType(type, codeNamespace.DeclareType(type.Name));
                    }
                }

                this.AddFunctionsInNamespaceIfRequired(namespaceName, codeNamespace, model.Functions);
            }
        }