public string GetDefaultFileName() { return("_{0}_{1}_.dll"._Format( GetNamespace(), DaoAssembly.GetTypes() .Where(t => t.HasCustomAttributeOfType <TableAttribute>()) .ToInfoHash() )); // this fluent stuff is setting the fileName to the Md5 hash of all the table names comma delimited }
/// <summary> /// Write dto source code into the specified namespace placing generated files into the specified directory /// </summary> /// <param name="nameSpace"></param> /// <param name="writeSourceTo"></param> public void WriteDtoSource(string nameSpace, string writeSourceTo) { Args.ThrowIfNull(DaoAssembly, "DaoAssembly"); foreach (Type daoType in DaoAssembly.GetTypes() .Where(t => t.HasCustomAttributeOfType <TableAttribute>())) { Dto.WriteRenderedDto(nameSpace, writeSourceTo, daoType, pi => pi.HasCustomAttributeOfType <ColumnAttribute>()); } }
public GeneratedAssemblyInfo GenerateDtoAssembly(string nameSpace, string fileName) { Type oneDao = DaoAssembly.GetTypes().FirstOrDefault(t => t.HasCustomAttributeOfType <TableAttribute>()); string writeSourceTo = Path.Combine(RuntimeSettings.AppDataFolder, "DtoTemp_{0}"._Format(Dao.ConnectionName(oneDao))); DirectoryInfo sourceDir = SetSourceDir(writeSourceTo); WriteDtoSource(nameSpace, writeSourceTo); sourceDir.ToAssembly(fileName, out CompilerResults results); GeneratedAssemblyInfo result = new GeneratedAssemblyInfo(fileName, results); result.Save(); return(result); }
private string GetNamespace() { Args.ThrowIfNull(DaoAssembly, "DaoToDtoGenerator.DaoAssembly"); Type oneTable = DaoAssembly.GetTypes().FirstOrDefault(t => t.HasCustomAttributeOfType <TableAttribute>()); if (oneTable == null) { oneTable = DaoAssembly.GetTypes().FirstOrDefault(); if (oneTable == null) { Args.Throw <InvalidOperationException>("The specified DaoAssembly has no types defined"); } } string nameSpace = oneTable.Namespace; return(nameSpace); }
protected internal void CorrectForeignKeys(HashSet <OldToNewIdMapping> oldToNewIdMappings, Database source, Database destination) { Dictionary <string, OldToNewIdMapping> byUuid = oldToNewIdMappings.ToDictionary(otn => otn.Uuid); IParameterBuilder oldParameterBuilder = source.GetService <IParameterBuilder>(); SqlStringBuilder committer = destination.GetService <SqlStringBuilder>(); List <Type> tableTypes = DaoAssembly.GetTypes().Where(type => type.HasCustomAttributeOfType <TableAttribute>()).ToList(); List <PropertyInfo> foreignKeyProperties = new List <PropertyInfo>(); foreach (Type type in tableTypes) { foreignKeyProperties.AddRange(type.GetProperties().Where(prop => prop.HasCustomAttributeOfType <ForeignKeyAttribute>()).ToList()); } // for every foreign key foreach (PropertyInfo prop in foreignKeyProperties) { ForeignKeyAttribute fk = prop.GetCustomAttributeOfType <ForeignKeyAttribute>(); // get the old Dao instances that represents the referenced table Type referencedDaoType = DaoTypes.First(t => { TableAttribute table; t.HasCustomAttributeOfType <TableAttribute>(out table); return(table.TableName.Equals(fk.ReferencedTable)); }); SqlStringBuilder oldSelector = source.GetService <SqlStringBuilder>(); oldSelector.Select(fk.ReferencedTable); List <object> oldReferencedDaos = source.GetDataTable(oldSelector, CommandType.Text, oldParameterBuilder.GetParameters(oldSelector)).ToListOf(referencedDaoType); foreach (object oldReferenced in oldReferencedDaos) { Dao oldReferencedDao = (Dao)oldReferenced; // get the old Dao instances that represent the referencing table where the referencing column value = the old table id Type referencingDaoType = DaoTypes.First(t => { TableAttribute table; t.HasCustomAttributeOfType <TableAttribute>(out table); return(table.TableName.Equals(fk.Table)); }); SqlStringBuilder oldReferencerSelector = source.GetService <SqlStringBuilder>(); oldReferencerSelector.Select(fk.Table).Where(new AssignValue(fk.Name, oldReferencedDao.IdValue)); List <object> oldReferencingDaoInstances = source.GetDataTable(oldReferencerSelector, CommandType.Text, oldParameterBuilder.GetParameters(oldReferencerSelector)).ToListOf(referencingDaoType); if (oldReferencingDaoInstances.Count > 0) { List <string> oldReferencingDaoInstanceUuids = oldReferencingDaoInstances.Select(o => o.Property <string>("Uuid")).ToList(); long oldReferencedId = oldReferencedDao.IdValue.Value; // get the new referenced id long whatItShouldBeNow = byUuid[oldReferencedDao.Property <string>("Uuid")].NewId; // update the new referencing column to match the newId in the destination where the uuids in oldDaoInstances committer.Update(fk.Table, new AssignValue(fk.Name, whatItShouldBeNow)).Where(Query.Where("Uuid").In(oldReferencingDaoInstanceUuids.ToArray())); committer.Go(); } } } destination.ExecuteSql(committer, oldParameterBuilder); }