AjGenesis: Generating the model from assemblies

 Darío Quintana have rewrote his program that generates an  AjGenesis model starting from compiled .NET assemblies. The last version is described at (Spanish post):

Generando con AjGenesis desde los assemblies

The image shows the main project directory content. There are other projects, with NUnit tests, examples, and compiled code.

It is interesting to see how a model (there is no fixed model in AjGenesis) could be obtained from different sources. In a previous post I described a short example, showing how to obtain a model from the database structure:

AjGenesis: Generating the model from the database

Darío, in his post, shows how he uses his project from an AjGenesis tasks:

function GetEntities(path) AssemblyManager.LoadFrom(AjGenesisFromAssemblyPath) obj = new AjGenesis.FromAssembly.Collector() list = obj.GetEntities(path,null) return list end function

This code uses the new AssemblyManager to load the library AjGenesis.FromAssembly.

The classes Model, Entity, Property are the model, like in other AjGenesis examples. The class Collector creates a Generator object. This object is in charge of the hard work, finding the entities inside an assembly:

using System.Collections; namespace AjGenesis.FromAssembly { public class Collector { public ArrayList GetEntities(string assemblyPath, string ns) { Generator g = new Generator(); g.Path = assemblyPath; g.AssemblyToObjects(); ArrayList array = new ArrayList(); foreach (Entity entity in g.Model.Entities) { array.Add(entity); } return array; } } }

Un fragmento de Generator:


public void AssemblyToObjects() { Assembly assembly = Assembly.LoadFrom(Path); Type[] types; types = assembly.GetTypes(); //Get all types from the assembly for (int i = 0; i < types.Length; i++) { //Veo que interfaces está implementando bool ImplementaIEntity = false; Type[] interfacesTypes = types[i].GetInterfaces(); ImplementaIEntity = true; if (ImplementaIEntity) { Entity entity = new Entity(); entity.Name = GetTypeName(types[i].Name); entity.FullName = types[i].FullName; if (types[i].BaseType != null) { entity.Inherits = GetTypeName(types[i].BaseType.Name); entity.InheritsFullName = types[i].BaseType.FullName; } PropertyInfo[] propertyInfo = types[i].GetProperties(); foreach (PropertyInfo pi in propertyInfo) { Property prop = new Property(); prop.Name = pi.Name; prop.TypeName = pi.PropertyType.Name; if (pi.PropertyType.FullName != null) { prop.TypeFullName = pi.PropertyType.FullName; } else prop.TypeFullName = string.Empty; entity.Properties.Add(prop); } _model.Entities.Add(entity); } } }

This is only a fragment of the code, see the complete project to have more detail. It uses reflection to obtain the types in an assembly, and its properties. I guess Darío wants to improve the way of detecting when a type is an entity, using interfaces and attributes.

Angel “Java” Lopez

1 thought on “AjGenesis: Generating the model from assemblies

  1. Pingback: Models for Code Generation in AjGenesis « Angel “Java” Lopez on Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s