Exemplo n.º 1
0
        public ClientApiGenerator(object sourceSchema, EdmItemCollection edmItemCollection, EntityClassGenerator generator, List <EdmSchemaError> errors, string namespacePrefix)
        {
            Debug.Assert(edmItemCollection != null, "edmItemCollection is null");
            Debug.Assert(generator != null, "generator is null");
            Debug.Assert(errors != null, "errors is null");

            _edmItemCollection = edmItemCollection;
            _generator         = generator;
            _errors            = errors;
            _attributeEmitter  = new AttributeEmitter(_typeReference);
            _namespacePrefix   = namespacePrefix;

            // generate map for inherited types and prefixed types
            _namespaceMap = new Dictionary <string, string>();

            _onTypeGenerated     = new EventHandler <TypeGeneratedEventArgs>(TypeGeneratedEventHandler);
            _onPropertyGenerated = new EventHandler <PropertyGeneratedEventArgs>(PropertyGeneratedEventHandler);

            //// This constructor can be called multiple times with the same EntityClassGenerator. That is, many instances
            //// of the ClientApiGenerator can add handlers to one instance of the EntityClassGenerator.
            //// Make sure the handlers are not left on the EntityClassGenerator after the ClientApiGenerator is no longer in use.
            //// See the Dispose method

            _generator.OnTypeGenerated     += _onTypeGenerated;
            _generator.OnPropertyGenerated += _onPropertyGenerated;
        }
        public ClientApiGenerator(Schema sourceSchema, EdmItemCollection edmItemCollection, EntityClassGenerator generator, List <EdmSchemaError> errors)
        {
            Debug.Assert(sourceSchema != null, "sourceSchema is null");
            Debug.Assert(edmItemCollection != null, "edmItemCollection is null");
            Debug.Assert(generator != null, "generator is null");
            Debug.Assert(errors != null, "errors is null");

            _edmItemCollection = edmItemCollection;
            _sourceSchema      = sourceSchema;
            _generator         = generator;
            _errors            = errors;
            _attributeEmitter  = new AttributeEmitter(_typeReference);
        }
Exemplo n.º 3
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);
        }