/// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public override CodeTypeDeclarationCollection EmitApiClass()
        {
            Validate(); // emitter-specific validation

            CodeTypeReference baseType = this.GetBaseType();

            // raise the TypeGenerated event
            TypeGeneratedEventArgs eventArgs = new TypeGeneratedEventArgs(Item, baseType);

            this.Generator.RaiseTypeGeneratedEvent(eventArgs);

            // public [abstract] partial class ClassName
            CodeTypeDeclaration typeDecl = new CodeTypeDeclaration(Item.Name);

            typeDecl.IsPartial      = true;
            typeDecl.TypeAttributes = System.Reflection.TypeAttributes.Class;
            if (Item.Abstract)
            {
                typeDecl.TypeAttributes |= System.Reflection.TypeAttributes.Abstract;
            }

            SetTypeVisibility(typeDecl);

            EmitTypeAttributes(Item.Name, typeDecl, eventArgs.AdditionalAttributes);

            // : baseclass
            AssignBaseType(typeDecl, baseType, eventArgs.BaseType);

            AddInterfaces(Item.Name, typeDecl, eventArgs.AdditionalInterfaces);

            CommentEmitter.EmitSummaryComments(Item, typeDecl.Comments);

            // Since abstract types cannot be instantiated, skip the factory method for abstract types
            if ((typeDecl.TypeAttributes & System.Reflection.TypeAttributes.Abstract) == 0)
            {
                EmitFactoryMethod(typeDecl);
            }

            EmitProperties(typeDecl);

            // additional members, if provided by the event subscriber
            this.AddMembers(Item.Name, typeDecl, eventArgs.AdditionalMembers);

            CodeTypeDeclarationCollection typeDecls = new CodeTypeDeclarationCollection();

            typeDecls.Add(typeDecl);
            return(typeDecls);
        }
 /// <summary>
 /// Helper method that raises the TypeGenerated event
 /// </summary>
 /// <param name="eventArgs">The event arguments passed to the subscriber</param>
 internal void RaiseTypeGeneratedEvent(TypeGeneratedEventArgs eventArgs)
 {
     _generator.RaiseTypeGeneratedEvent(eventArgs);
 }
Example #3
0
        /// <summary>
        /// Creates the CodeTypeDeclarations necessary to generate the code for the EntityContainer schema element
        /// </summary>
        /// <returns></returns>
        public override CodeTypeDeclarationCollection EmitApiClass()
        {
            Validate(); // emitter-specific validation

            // declare the new class
            // public partial class LOBScenario : ObjectContext
            CodeTypeDeclaration typeDecl = new CodeTypeDeclaration(Item.Name);

            typeDecl.IsPartial = true;

            // raise the TypeGenerated event
            CodeTypeReference      objectContextTypeRef = TypeReference.ObjectContext;
            TypeGeneratedEventArgs eventArgs            = new TypeGeneratedEventArgs(Item, objectContextTypeRef);

            Generator.RaiseTypeGeneratedEvent(eventArgs);

            if (eventArgs.BaseType != null && !eventArgs.BaseType.Equals(objectContextTypeRef))
            {
                typeDecl.BaseTypes.Add(eventArgs.BaseType);
            }
            else
            {
                typeDecl.BaseTypes.Add(TypeReference.ObjectContext);
            }
            AddInterfaces(Item.Name, typeDecl, eventArgs.AdditionalInterfaces);

            CommentEmitter.EmitSummaryComments(Item, typeDecl.Comments);
            EmitTypeAttributes(Item.Name, typeDecl, eventArgs.AdditionalAttributes);

            CreateConstructors(typeDecl);
            // adding partial OnContextCreated method
            CreateContextPartialMethods(typeDecl);

            foreach (EntitySetBase entitySetBase in Item.BaseEntitySets)
            {
                if (MetadataUtil.IsEntitySet(entitySetBase))
                {
                    EntitySet          set          = (EntitySet)entitySetBase;
                    CodeMemberProperty codeProperty = CreateEntitySetProperty(set);
                    typeDecl.Members.Add(codeProperty);

                    CodeMemberField codeField = CreateEntitySetField(set);
                    typeDecl.Members.Add(codeField);
                }
            }

            foreach (EntitySetBase entitySetBase in Item.BaseEntitySets)
            {
                if (MetadataUtil.IsEntitySet(entitySetBase))
                {
                    EntitySet        set          = (EntitySet)entitySetBase;
                    CodeMemberMethod codeProperty = CreateEntitySetAddObjectProperty(set);
                    typeDecl.Members.Add(codeProperty);
                }
            }

            foreach (EdmFunction functionImport in Item.FunctionImports)
            {
                if (ShouldEmitFunctionImport(functionImport))
                {
                    CodeMemberMethod functionMethod = CreateFunctionImportStructuralTypeReaderMethod(functionImport);
                    typeDecl.Members.Add(functionMethod);
                }
            }

            // additional members, if provided by the event subscriber
            AddMembers(Item.Name, typeDecl, eventArgs.AdditionalMembers);

            CodeTypeDeclarationCollection typeDecls = new CodeTypeDeclarationCollection();

            typeDecls.Add(typeDecl);
            return(typeDecls);
        }
Example #4
0
        private void TypeGeneratedEventHandler(object sender, TypeGeneratedEventArgs eventArgs)
        {
            if (!this.UseDataServiceCollection)
            {
                return;
            }

            if (eventArgs.TypeSource.BuiltInTypeKind != BuiltInTypeKind.EntityType &&
                eventArgs.TypeSource.BuiltInTypeKind != BuiltInTypeKind.ComplexType)
            {
                return;
            }

            if (eventArgs.TypeSource.BuiltInTypeKind == BuiltInTypeKind.EntityType)
            {
                // Generate EntitySetAttribute only if there is exactly one entity set associated
                // with the entity type. The DataServiceEntitySetAttribute is not generated for ComplexType(s).
                EntitySetBase entitySet = this.GetUniqueEntitySetForType((EntityType)eventArgs.TypeSource);
                if (entitySet != null)
                {
                    List <CodeAttributeDeclaration> additionalAttributes = eventArgs.AdditionalAttributes;
                    CodeAttributeDeclaration        attribute            = new CodeAttributeDeclaration(
                        new CodeTypeReference(typeof(System.Data.Services.Common.EntitySetAttribute),
                                              CodeTypeReferenceOptions.GlobalReference), new CodeAttributeArgument(new CodePrimitiveExpression(entitySet.Name)));
                    additionalAttributes.Add(attribute);
                }
            }

            //// Determine if type being generated has a base type
            if (eventArgs.BaseType != null && !String.IsNullOrEmpty(eventArgs.BaseType.BaseType))
            {
                if (this.GetSourceTypes().Where(x => x.BuiltInTypeKind == BuiltInTypeKind.EntityType).Where(x => ((EntityType)x).Name == eventArgs.BaseType.BaseType).Count() != 0)
                {
                    //// Don't generate the PropertyChanged event and OnPropertyChanged method for derived type classes
                    return;
                }
            }

            //// Add the INotifyPropertyChanged interface

            List <Type> additionalInterfaces = eventArgs.AdditionalInterfaces;

            additionalInterfaces.Add(typeof(System.ComponentModel.INotifyPropertyChanged));

            //// Add the implementation of the INotifyPropertyChanged interface

            //// Generate this code:
            ////
            //// CSharp:
            ////
            //// public event global::System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
            ////
            //// protected virtual void OnPropertyChanged(string property) {
            ////     if ((this.PropertyChanged != null)) {
            ////         this.PropertyChanged(this, new global::System.ComponentModel.PropertyChangedEventArgs(property));
            ////     }
            //// }
            ////
            //// Visual Basic:
            ////
            //// Public Event PropertyChanged As Global.System.ComponentModel.PropertyChangedEventHandler Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
            //// Protected Overridable Sub OnPropertyChanged(ByVal [property] As String)
            ////     If (Not (Me.PropertyChangedEvent) Is Nothing) Then
            ////         RaiseEvent PropertyChanged(Me, New Global.System.ComponentModel.PropertyChangedEventArgs([property]))
            ////     End If
            //// End Sub


            CodeMemberEvent propertyChangedEvent = new CodeMemberEvent();

            propertyChangedEvent.Type       = new CodeTypeReference(typeof(System.ComponentModel.PropertyChangedEventHandler), CodeTypeReferenceOptions.GlobalReference);
            propertyChangedEvent.Name       = "PropertyChanged";
            propertyChangedEvent.Attributes = MemberAttributes.Public;
            propertyChangedEvent.ImplementationTypes.Add(typeof(System.ComponentModel.INotifyPropertyChanged));

            AttributeEmitter.AddGeneratedCodeAttribute(propertyChangedEvent);

            eventArgs.AdditionalMembers.Add(propertyChangedEvent);

            CodeMemberMethod propertyChangedMethod = new CodeMemberMethod();

            propertyChangedMethod.Name = "OnPropertyChanged";
            propertyChangedMethod.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(System.String), CodeTypeReferenceOptions.GlobalReference), "property"));
            propertyChangedMethod.ReturnType = new CodeTypeReference(typeof(void));

            AttributeEmitter.AddGeneratedCodeAttribute(propertyChangedMethod);

            propertyChangedMethod.Statements.Add(
                new CodeConditionStatement(
                    new CodeBinaryOperatorExpression(
                        new CodeEventReferenceExpression(new CodeThisReferenceExpression(), "PropertyChanged"),
                        CodeBinaryOperatorType.IdentityInequality,
                        new CodePrimitiveExpression(null)),
                    new CodeExpressionStatement(
                        new CodeDelegateInvokeExpression(
                            new CodeEventReferenceExpression(new CodeThisReferenceExpression(), "PropertyChanged"),
                            new CodeExpression[] {
                new CodeThisReferenceExpression(),
                new CodeObjectCreateExpression(new CodeTypeReference(typeof(System.ComponentModel.PropertyChangedEventArgs), CodeTypeReferenceOptions.GlobalReference), new CodeArgumentReferenceExpression("property"))
            }))));
            propertyChangedMethod.Attributes = MemberAttributes.Family;
            eventArgs.AdditionalMembers.Add(propertyChangedMethod);
        }
Example #5
0
        /// <summary>
        /// Creates the CodeTypeDeclarations necessary to generate the code for the EntityContainer schema element
        /// </summary>
        /// <returns></returns>
        public override CodeTypeDeclarationCollection EmitApiClass()
        {
            Validate(); // emitter-specific validation

            // declare the new class
            // public partial class LOBScenario : ObjectContext
            CodeTypeDeclaration typeDecl = new CodeTypeDeclaration(Item.Name);

            typeDecl.IsPartial = true;

            // raise the TypeGenerated event
            CodeTypeReference      objectContextTypeRef = TypeReference.ObjectContext;
            TypeGeneratedEventArgs eventArgs            = new TypeGeneratedEventArgs(Item, objectContextTypeRef);

            Generator.RaiseTypeGeneratedEvent(eventArgs);

            if (eventArgs.BaseType != null && !eventArgs.BaseType.Equals(objectContextTypeRef))
            {
                typeDecl.BaseTypes.Add(eventArgs.BaseType);
            }
            else
            {
                typeDecl.BaseTypes.Add(TypeReference.ObjectContext);
            }

            AddInterfaces(Item.Name, typeDecl, eventArgs.AdditionalInterfaces);

            CommentEmitter.EmitSummaryComments(Item, typeDecl.Comments);
            EmitTypeAttributes(Item.Name, typeDecl, eventArgs.AdditionalAttributes);

            bool needTypeMapper = (0 < this.Generator.NamespaceMap.Count);

            var q = from a in this.Generator.EdmItemCollection.GetItems <StructuralType>()
                    where (a.BaseType != null) &&
                    (a.BuiltInTypeKind == BuiltInTypeKind.ComplexType || a.BuiltInTypeKind == BuiltInTypeKind.EntityType)
                    select a;
            bool hasInheritance = (null != q.FirstOrDefault());

            CreateConstructors(typeDecl, needTypeMapper, hasInheritance);
            // adding partial OnContextCreated method
            CreateContextPartialMethods(typeDecl);

            if (needTypeMapper || hasInheritance)
            {
                CreateTypeMappingMethods(typeDecl, needTypeMapper, hasInheritance);
            }

            foreach (EntitySetBase entitySetBase in Item.BaseEntitySets)
            {
                if (Helper.IsEntitySet(entitySetBase))
                {
                    EntitySet          set          = (EntitySet)entitySetBase;
                    CodeMemberProperty codeProperty = CreateEntitySetProperty(set);
                    typeDecl.Members.Add(codeProperty);

                    CodeMemberField codeField = CreateEntitySetField(set);
                    typeDecl.Members.Add(codeField);
                }
            }

            foreach (EntitySetBase entitySetBase in Item.BaseEntitySets)
            {
                if (Helper.IsEntitySet(entitySetBase))
                {
                    EntitySet        set          = (EntitySet)entitySetBase;
                    CodeMemberMethod codeProperty = CreateEntitySetAddObjectMethod(set);
                    typeDecl.Members.Add(codeProperty);
                }
            }

            // additional members, if provided by the event subscriber
            AddMembers(Item.Name, typeDecl, eventArgs.AdditionalMembers);

            CodeTypeDeclarationCollection typeDecls = new CodeTypeDeclarationCollection();

            typeDecls.Add(typeDecl);
            return(typeDecls);
        }