public override void GenerateEntities(string filePrefix, string nameSpace, Data.DbSyncScopeDescription desc, Dictionary <string, Dictionary <string, string> > colsMappingInfo, System.IO.DirectoryInfo dirInfo, CodeLanguage option, string serviceUri)
        {
            // First generate the custom Context file
            CodeCompileUnit compileUnit = GenerateContextFile(filePrefix, nameSpace, desc, serviceUri);

            CodeDomUtility.SaveCompileUnitToFile(compileUnit, option, CodeDomUtility.GenerateFileName(desc.ScopeName, dirInfo, filePrefix, "OfflineContext", option));

            // Then generate the file containing the actual entities
            compileUnit = GenerateEntitiesFile(nameSpace, desc, colsMappingInfo);
            CodeDomUtility.SaveCompileUnitToFile(compileUnit, option, CodeDomUtility.GenerateFileName(desc.ScopeName, dirInfo, filePrefix, "Entities", option));
        }
        private CodeCompileUnit GenerateEntitiesFile(string nameSpace, Data.DbSyncScopeDescription desc, Dictionary <string, Dictionary <string, string> > colsMappingInfo)
        {
            CodeCompileUnit entitiesCC = new CodeCompileUnit();

            CodeNamespace entityScopeNs = new CodeNamespace(nameSpace);

            // Generate the entities
            foreach (DbSyncTableDescription table in desc.Tables)
            {
                Dictionary <string, string> curTableMapping = null;
                colsMappingInfo.TryGetValue(table.UnquotedGlobalName, out curTableMapping);

                // Generate the actual entity
                CodeTypeDeclaration entityDecl = CodeDomUtility.GetSQLiteEntityForTableDescription(table, true, curTableMapping);

                // Add the base type for the entity
                entityDecl.BaseTypes.Add(new CodeTypeReference(Constants.SQLiteOfflineEntity));
                entityScopeNs.Types.Add(entityDecl);

                foreach (CodeTypeMember member in entityDecl.Members)
                {
                    CodeMemberProperty prop = member as CodeMemberProperty;
                    if (prop != null)
                    {
                        // For each setter add the OnPropertyChanging and OnPropertyChanged lines
                        CodeStatement stmt = prop.SetStatements[0];
                        prop.SetStatements.Clear();

                        prop.SetStatements.Add(new CodeMethodInvokeExpression(
                                                   new CodeBaseReferenceExpression(),
                                                   Constants.ClientCallOnPropertyChanging,
                                                   new CodeSnippetExpression(string.Format(Constants.StringifyProperty, prop.Name)))
                                               );
                        prop.SetStatements.Add(stmt);
                        prop.SetStatements.Add(new CodeMethodInvokeExpression(
                                                   new CodeBaseReferenceExpression(),
                                                   Constants.ClientCallOnPropertyChanged,
                                                   new CodeSnippetExpression(string.Format(Constants.StringifyProperty, prop.Name)))
                                               );
                    }
                }
            }

            entitiesCC.Namespaces.Add(entityScopeNs);
            return(entitiesCC);
        }
        public override void GenerateEntities(string filePrefix, string nameSpace, Data.DbSyncScopeDescription desc, Dictionary <string, Dictionary <string, string> > colsMappingInfo,
                                              System.IO.DirectoryInfo dirInfo, CodeLanguage option, string serviceUri)
        {
            CodeCompileUnit entitiesCC = new CodeCompileUnit();

            CodeNamespace entityScopeNs = new CodeNamespace(nameSpace);

            // Generate the base class for all the entities which will implement IOfflineEntity
            CodeTypeDeclaration baseEntity = CodeDomUtility.CreateIOfflineEntityCustomBaseClass(
                string.IsNullOrEmpty(filePrefix) ? desc.ScopeName : filePrefix,
                false /*isServer*/);

            // Set the base type
            // VB uses different keywords for class and interface inheritence. For it to emit the
            // right keyword it must inherit from object first before the actual interface.
            baseEntity.BaseTypes.Add(new CodeTypeReference(typeof(object)));
            baseEntity.BaseTypes.Add(new CodeTypeReference(Constants.ClientIOfflineEntity));

            entityScopeNs.Types.Add(baseEntity);

            // Generate the entities
            foreach (DbSyncTableDescription table in desc.Tables)
            {
                Dictionary <string, string> curTableMapping = null;
                colsMappingInfo.TryGetValue(table.UnquotedGlobalName, out curTableMapping);

                // Generate the actual entity
                CodeTypeDeclaration entityDecl = CodeDomUtility.GetEntityForTableDescription(table, true, curTableMapping);

                // Set the base type
                entityDecl.BaseTypes.Add(baseEntity.Name);

                //Add it to the overall scope
                entityScopeNs.Types.Add(entityDecl);
            }

            entitiesCC.Namespaces.Add(entityScopeNs);

            // Generate the files
            CodeDomUtility.SaveCompileUnitToFile(entitiesCC, option, CodeDomUtility.GenerateFileName(desc.ScopeName, dirInfo, filePrefix, "Entities", option));
        }
        private static CodeCompileUnit GenerateEntitiesCompileUnit(string prefix, string nameSpace, Data.DbSyncScopeDescription desc, Dictionary <string, Dictionary <string, string> > colsMappingInfo)
        {
            CodeCompileUnit cc = new CodeCompileUnit();

            CodeNamespace scopeNs = new CodeNamespace(nameSpace);

            // Generate the outer most entity
            CodeTypeDeclaration wrapperEntity = new CodeTypeDeclaration(
                string.Format(Constants.ServiceOuterEntityNameFormat, string.IsNullOrEmpty(prefix) ? desc.ScopeName : prefix)
                );

            wrapperEntity.CustomAttributes.Add(new CodeAttributeDeclaration(Constants.ServiceSyncScopeAttributeDefinition));

            // Generate the base class for all the entities which will implement IOfflineEntity
            CodeTypeDeclaration baseEntity = CodeDomUtility.CreateIOfflineEntityCustomBaseClass(
                string.IsNullOrEmpty(prefix) ? desc.ScopeName : prefix,
                true /*isServer*/);

            // Set the base type
            // VB uses different keywords for class and interface inheritence. For it to emit the
            // right keyword it must inherit from object first before the actual interface.
            baseEntity.BaseTypes.Add(new CodeTypeReference(typeof(object)));
            baseEntity.BaseTypes.Add(new CodeTypeReference(Constants.ServiceIOfflineEntity));

            scopeNs.Types.Add(baseEntity);

            // Generate the entities
            foreach (DbSyncTableDescription table in desc.Tables)
            {
                Dictionary <string, string> curTableMapping = null;
                colsMappingInfo.TryGetValue(table.UnquotedGlobalName, out curTableMapping);

                string            tableName      = CodeDomUtility.SanitizeName(table.UnquotedGlobalName);
                CodeTypeReference icollReference = new CodeTypeReference(typeof(ICollection <>));

                // Generate the private field
                CodeTypeReference entityReference = new CodeTypeReference(tableName);
                icollReference.TypeArguments.Clear();
                icollReference.TypeArguments.Add(entityReference);

                CodeMemberField itemCollectionField = new CodeMemberField(icollReference, string.Format(Constants.EntityFieldNameFormat, tableName));
                itemCollectionField.Attributes = MemberAttributes.Private;
                wrapperEntity.Members.Add(itemCollectionField);

                // Generate the actual entity
                CodeTypeDeclaration entityDecl = CodeDomUtility.GetEntityForTableDescription(table, false /*addKeyAttributes*/, curTableMapping);
                entityDecl.BaseTypes.Add(baseEntity.Name);

                var syncEntityAttributeDeclaration = new CodeAttributeDeclaration(Constants.ServiceSyncEntityAttribute,
                                                                                  new CodeAttributeArgument("TableGlobalName", new CodeSnippetExpression("\"" + CodeDomUtility.SanitizeName(table.UnquotedGlobalName) + "\"")),
                                                                                  // table.LocalName is quoted and contains the schema name.
                                                                                  new CodeAttributeArgument("TableLocalName", new CodeSnippetExpression("\"" + table.LocalName + "\"")),
                                                                                  new CodeAttributeArgument("KeyFields",
                                                                                                            new CodeSnippetExpression("\"" +
                                                                                                                                      String.Join(",", table.PkColumns.
                                                                                                                                                  Select(e => CodeDomUtility.SanitizeName(GetKeyColumnName(e.UnquotedName, curTableMapping))).ToArray()) +
                                                                                                                                      "\"")));

                // Use the PkColumns property to generate the column list for the KeyFields property.
                // This is important as it is used later to generate SyncId's for rows and ordering is important.
                entityDecl.CustomAttributes.Add(syncEntityAttributeDeclaration);

                scopeNs.Types.Add(entityDecl);
            }

            scopeNs.Types.Add(wrapperEntity);
            cc.Namespaces.Add(scopeNs);
            return(cc);
        }