Reading and manipulating a llblgen project file programmatically

Posts   
 
    
costab
User
Posts: 36
Joined: 21-Aug-2010
# Posted on: 31-Aug-2011 02:40:20   

Is it possible to read an llblgen project programmatically in memory using some object model, manipulate objects/properties and then serialize it back?

I want to add some validation attributes to all the string fields. It is is very tedious to do it by hand so I was thinking that one can do it programmatically. In the end the llblgen project file is an xml file.

Also do you provide an xsd file or xml schema file that defines completely the structure of the project?

Thanks

Update: Never mind, I found myself the libraries that allow me to edit the Project. Here is the source code to do whatever I wanted to do, in case someone else needs it. Make sure of few things: you add the following dlls:

SD.LLBLGen.Pro.ApplicationCore.dll SD.LLBLGen.Pro.Core.dll SD.LLBLGen.Pro.DBDriverCore.dll SD.LLBLGen.Pro.DBDrivers.SqlServerDBDriver.dll SD.Tools.Algorithmia.dll SD.Tools.BCLExtensions.dll

and these:

<Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Data.Linq" /> <Reference Include="System.Data.SqlXml" /> <Reference Include="System.Windows.Forms" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" />

then you also add your license file to the project and set it to copy it always in properties window.

using System;
using System.Collections;
using System.IO;
using System.Linq;

using SD.LLBLGen.Pro.ApplicationCore;
using SD.LLBLGen.Pro.ApplicationCore.EntityModel;
using SD.LLBLGen.Pro.DBDriverCore;
using SD.LLBLGen.Pro.ApplicationCore.ProjectClasses;
using SD.LLBLGen.Pro.ApplicationCore.Configuration;
using SD.LLBLGen.Pro.ApplicationCore.Extensibility;
using SD.Tools.Algorithmia.Commands;
using SD.Tools.BCLExtensions.SystemRelated;
using System.Windows.Forms;
using SD.LLBLGen.Pro.ApplicationCore.MessageReporting;
using SD.Tools.Algorithmia.GeneralDataStructures.EventArguments;
using SD.Tools.BCLExtensions.CollectionsRelated;

namespace LlblgenPro
{
  class Program
  {
    static void Main(string[] args)
    {

      Console.WriteLine("Initializing...");
      Action<string, string> messageFunc = (message, caption) => Console.WriteLine("{0}:: {1}", caption, message);
      CoreStateSingleton.GetInstance().Initialize(Application.StartupPath, messageFunc, messageFunc, messageFunc);
      if (!CoreStateSingleton.GetInstance().IsInitialized)
      {
        return;
      }
      CoreStateSingleton.GetInstance().ConfigurationSettings = new ApplicationConfiguration(Application.StartupPath);
      CoreStateSingleton.GetInstance().LoadObjectsAndConfigData();

      Project p = Project.Load(@"C:\projects\csharp\TestIoC\TestIoC\NorthWind.llblgenproj");

      foreach (EntityDefinition entity in p.GetAllEntities())
      {
        Console.WriteLine(entity.Name);
        foreach (var field in entity.GetAllFieldsInElement(false))
        {
          Console.WriteLine(" {0} {1}", field.FullName, field.FieldType);
          if (field.FieldType.RepresentedType == typeof(string))
          {
            field.OutputSettingValues.Attributes.Add("Length($length)");
          }

        }
      }

      p.Save();
      Console.ReadKey();
    }
  }
}
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 31-Aug-2011 12:02:37   

Thanks for the feedback.