
Introduction
This article will show you how to create a simple code generator, plug-in based. A code generator can hugely improve your productivity as it does the repetitive work for you. Nowadays, computer systems are becoming bigger and complex. I worked on an ERP project in which there was about 470 tables. Suppose you have to map all the 470 tables to C# classes by hand. It is just a waste of time. Let the code generator work for you.
Background
The core of the code generator is the API (Application Programming Interface) in which is defined its skeleton. In order to write plug-ins for the code generator, you must implement or inherit the interfaces or basic classes, respectively. See the diagram below for better elucidation.

Classes and interfaces responsibilities
The IConnector
interface defines the contract for a connector plug-in. An implementation class is responsible for connecting to a data source and returning an ISchema
implementation based on that structure. The purpose of the ISchema
, ITable
, and IRelationShip
is abstracting a relational data structure. The ILanguageParser
defines the contract for a language parser plug-in. An implementation class is responsible for parsing an ISchema implementation. The IPluginManager
defines a contract for managing a collection of IPlugin
implementations as you can see in the diagram above.
Using the code
A full code implementation is provided with the demonstration project. In order to improve your understanding of how the code works, I will show you a high level code implementation.
namespace CSharpParser
{
public class CSharpParser : Worker.Core.API.ILanguageParser
{
StringBuilder sb = new StringBuilder();
public string Parse(Worker.Core.API.ISchema schema)
{
}
}
}
namespace SimpleConnector
{
public class SimpleConnector : Worker.Core.API.IConnector
{
public bool Connect(string pconnectionString)
{
}
public Worker.Core.API.ISchema Buildschema(String pdataBase)
{
}
}
}
namespace SimplePluginManager
{
public class SimplePluginManager : Worker.Core.API.IPluginManager
{
public List GetAll()
{
}
public object Find(string name)
{
}
public object Plug(IPlugin plugin,String type)
{
}
public bool Register(IPlugin info)
{
}
public void Persist(String source)
{
}
public void Load(String source)
{
}
}
}
IPluginManager pluginManager =
(IPluginManager)new SimplePluginManager.SimplePluginManager();
IPlugin pluginConnector = new Plugin();
pluginConnector.Name = "MySqlConnector";
pluginConnector.Path = @"MySqlPlugin\SimpleConnector.dll";
pluginConnector.Version = "1.0";
pluginManager.Register(pluginConnector);
IPlugin pluginCSharp = new Plugin();
pluginCSharp.Name = "CSharpParser";
pluginCSharp.Path = @"CsharpPlugin\CSharpParser.dll";
pluginCSharp.Version = "1.0";
pluginManager.Register(pluginCSharp);
IPlugin fpluginConnector =
(IPlugin)pluginManager.Find("MySqlConnector");
IPlugin fpluginParser = (IPlugin)pluginManager.Find("CSharpParser");
IConnector con =
(IConnector)pluginManager.Plug(fpluginConnector,"IConnector");
con.Connect("ConnectionString");
Ischema schema = con.Buildschema("DataBaseName");
ILanguageParser parser =
(ILanguageParser)pluginManager.Plug(fpluginParser, "ILanguageParser");
this.richTextBox1.Text = parser.Parse(schema);
Conclusion
The Worker.API can be used to create code generators for any language. All you have to do is to create a plug-in and register it in a PluginManager. You can have your staff working in parallel. One writes the Connector while the other writes the LanguageParser.