/// <summary> /// Generate the code. /// </summary> /// <param name="data">The linq model container.</param> /// <returns>The code unit.</returns> public CodeCompileUnit Generate(LinqToSqlModelContainer data) { // Make sure the page reference exists. if (data == null) { throw new ArgumentNullException("data"); } // If not null. if (data != null) { _dataBaseName = data.Database; _companyName = data.NamespaceCompanyName; _className = data.ClassName; _extendedName = data.NamespaceExtendedName; // Create the namespace. InitialiseNamespace(); } // If not null. if (data != null) { _data = data; // Add the classes. AddClasses(); } // Return the complie unit. _targetUnit.Namespaces.Add(_samples); return(_targetUnit); }
/// <summary> /// Create the code file specified in the linq model object. /// </summary> /// <param name="fileName">The full path where the file is to be created.</param> /// <param name="data">The linq model container.</param> public static void CreateFile(string fileName, LinqToSqlModelContainer data) { // Make sure the page reference exists. if (fileName == null) { throw new ArgumentNullException("fileName"); } if (data == null) { throw new ArgumentNullException("data"); } LinqToSqlModel model = new LinqToSqlModel(); CodeCompileUnit code = model.Generate(data); model.CreateCodeFile(fileName, code); }
/// <summary> /// Create a new instance of the model containing the property items. /// </summary> /// <param name="data">The linq model container.</param> /// <returns>The instantiated object containing the property items</returns> public static object CreateInstance(LinqToSqlModelContainer data) { // Make sure the page reference exists. if (data == null) { throw new ArgumentNullException("data"); } // If not null. if (data != null) { List <Nequeo.Reflection.DynamicProperty> prop = new List <Nequeo.Reflection.DynamicProperty>(); // For each column found in the table // iterate through the list and create // each property. for (int i = 0; i < data.PropertyName.Count(); i++) { Type propertyType; bool isNullable = false; if (data.PropertyIsNullable != null) { isNullable = data.PropertyIsNullable[i]; } // If the table column is nullable and // the data type is not a reference type // then apply the nullable generic base. if (isNullable && ((!Nequeo.DataType.GetSystemType(data.PropertyType[i]).IsArray) && !Nequeo.DataType.GetSystemType(data.PropertyType[i]).IsClass)) { // Assign the data type for the property if // the data type is not a reference type // then create a nullable type property. Type propertyIntType = Nequeo.DataType.GetSystemType(data.PropertyType[i]); Type propertyGenericType = typeof(System.Nullable <>); Type[] typeArgs = { propertyIntType }; Type nullableTypeConstructor = propertyGenericType.MakeGenericType(typeArgs); propertyType = nullableTypeConstructor; } else { // Create the type. propertyType = Nequeo.DataType.GetSystemType(data.PropertyType[i]); } // Add the type to the collection. prop.Add(new Nequeo.Reflection.DynamicProperty(data.PropertyName[i], propertyType)); } // Return the new instance of the object. object instance = Nequeo.Reflection.TypeAccessor.CreateInstance(prop); PropertyInfo[] infos = instance.GetType().GetProperties(); // Assign each type value foreach (PropertyInfo info in infos) { info.SetValue(instance, data.PropertyName.First(u => u == info.Name), null); } // Return the instance with values. return(instance); } // No instance return(null); }