Exemplo n.º 1
0
        private void ImplementIUser(CodeTypeDeclaration typeDeclaration, bool generic, bool supportGeneric)
        {
            if (_identityUser == null)
            {
                return;
            }

            string keyTypeName                   = generic ? _identityUser.KeyTypeName : typeof(string).FullName;
            var    iuserCodeTypeReference        = new CodeTypeReference("Microsoft.AspNet.Identity.IUser");
            var    iuserGenericCodeTypeReference = new CodeTypeReference("Microsoft.AspNet.Identity.IUser");

            iuserGenericCodeTypeReference.TypeArguments.Add(keyTypeName);
            if (generic)
            {
                iuserCodeTypeReference.TypeArguments.Add(keyTypeName);
            }

            CodeDomUtilities.SetInterface(iuserCodeTypeReference);
            CodeDomUtilities.SetInterface(iuserGenericCodeTypeReference);
            typeDeclaration.BaseTypes.Add(iuserCodeTypeReference);

            CodeMemberProperty idProperty = new CodeMemberProperty();

            idProperty.Type   = new CodeTypeReference(keyTypeName);
            idProperty.Name   = "Id";
            idProperty.HasSet = false;
            idProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), generic ? _identityUser.KeyPropertyName : "EntityKey")));

            CodeMemberProperty userNameProperty = new CodeMemberProperty();

            userNameProperty.Type = new CodeTypeReference(typeof(string));
            userNameProperty.Name = "UserName";
            userNameProperty.SetStatements.Add(new CodeAssignStatement(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), _identityUser.UserNameProperty.Name), new CodePropertySetValueReferenceExpression()));
            userNameProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), _identityUser.UserNameProperty.Name)));

            if (generic || supportGeneric)
            {
                idProperty.PrivateImplementationType = GetPrivateImplementationGenericType(InputProducer.LanguageCode, "Microsoft.AspNet.Identity.IUser", keyTypeName);
                idProperty.ImplementationTypes.Add(iuserGenericCodeTypeReference);
                userNameProperty.PrivateImplementationType = GetPrivateImplementationGenericType(InputProducer.LanguageCode, "Microsoft.AspNet.Identity.IUser", keyTypeName);
                userNameProperty.ImplementationTypes.Add(iuserGenericCodeTypeReference);
            }
            else
            {
                idProperty.PrivateImplementationType = iuserCodeTypeReference;
                idProperty.ImplementationTypes.Add(iuserCodeTypeReference);
                userNameProperty.PrivateImplementationType = iuserCodeTypeReference;
                userNameProperty.ImplementationTypes.Add(iuserCodeTypeReference);
            }

            typeDeclaration.Members.Add(idProperty);
            typeDeclaration.Members.Add(userNameProperty);
        }
        private CodeTypeReference GetGenericInterfaceType(string interfaceFullTypeName, string genericType)
        {
            CodeTypeReference typeReference;

            if (genericType != null)
            {
                typeReference = CodeDomUtilities.GetGenericType(interfaceFullTypeName, IdentityRole.ClrFullTypeName, genericType);
            }
            else
            {
                typeReference = CodeDomUtilities.GetGenericType(interfaceFullTypeName, IdentityRole.ClrFullTypeName);
            }

            CodeDomUtilities.SetInterface(typeReference);
            return(typeReference);
        }