Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using meta-data in .NET
Message
From
24/10/2007 14:10:56
 
 
To
19/10/2007 08:53:55
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01262116
Message ID:
01263242
Views:
10
> In other words, how can I create and refer to properties of the Business Object at runtime based on the content of meta data tables?

Alex,

there are two possibilites to add properties to business objects in .NET : using TypeDescriptors or using Dynamic compiling.
I had a long thread about it in microsoft.public.dotnet.languages.csharp new based newsgroup.
The following sample shows hot to add properties to type at runtime using dynamic compiling. I can post you sample which uses typdescriptor also but typedescriptor may not work in some ill frameworks.
It uses Castle Activerecord (castleproject.org) as sample.

Main program:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Castle.ActiveRecord;
using System.Collections;
using Castle.ActiveRecord.Framework.Config;
using System.Reflection;
using System.CodeDom.Compiler;
using ModelEntity;

static class Program {
static void Main() {
try {

Hashtable properties = new Hashtable();
properties.Add("hibernate.connection.driver_class",
"NHibernate.Driver.NpgsqlDriver");
properties.Add("hibernate.dialect",
"NHibernate.Dialect.PostgreSQL81Dialect");

properties.Add("hibernate.connection.provider",
"NHibernate.Connection.DriverConnectionProvider");

properties.Add("hibernate.default_schema", "public");
properties.Add("hibernate.connection.connection_string",
"Encoding=UNICODE;Server=localhost;CommandTimeout=60;Database=eeva;User Id=admin;Password=pa");

properties.Add("hibernate.cache.provider_class",
"NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache");
properties.Add("hibernate.show_sql", true);

InPlaceConfigurationSource source = new InPlaceConfigurationSource();
source.Add(typeof(ActiveRecordBase), properties);

ActiveRecordStarter.Initialize(source);
Customer.RegisterType();

Array customers = Customer.GetFilteredList();
Form f = new Form();
DataGridView dgv = new DataGridView();
dgv.Dock = DockStyle.Fill;
f.Controls.Add(dgv);
dgv.DataSource = customers;
Application.Run(f);
}
catch (Exception e) {
MessageBox.Show(e.ToString());
}
}
}


ModelEntity.dll source:

using Castle.ActiveRecord;
using System.CodeDom.Compiler;
using System;

namespace ModelEntity {
[ActiveRecord("klient", Schema = "firma1", DynamicUpdate = true, DynamicInsert = true, Lazy = true, Cache = CacheEnum.NonStrictReadWrite)]
public class Customer : ModelGenericBase {

string kood;
[PrimaryKey("kood", Length = 12)]
public virtual string Kood {
get { return kood; }
set {
kood = value;
}
}
}

public abstract class ModelGenericBase :
ActiveRecordValidationBase where T : class {

static Type extendedType;

public static Array GetFilteredList() {
return FindAll(extendedType);
}


public static void RegisterType() {
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters compilerParameters = new CompilerParameters();
compilerParameters.ReferencedAssemblies.Add("Castle.ActiveRecord.dll");
compilerParameters.ReferencedAssemblies.Add("ModelEntity.dll");
compilerParameters.GenerateInMemory = true;
CompilerResults compilerResults =
provider.CompileAssemblyFromSource(compilerParameters,
@"
using Castle.ActiveRecord;
using ModelEntity;

[ActiveRecord(""klient"", Schema = ""firma1"", DynamicUpdate = true, DynamicInsert = true, Lazy = true)]
public class ExtendedCustomer:Customer {

string name;
[Property]
public virtual string Name {
get { return name; }
set {
name = value;
}
}
}
");

if (compilerResults.Errors.HasErrors) {
string msg;
msg = compilerResults.Errors.Count.ToString() + " Errors:";

for (int x = 0; x < compilerResults.Errors.Count; x++)
msg = msg + "\r\nLine: " +
compilerResults.Errors[x].Line.ToString() + " - " +
compilerResults.Errors[x].ErrorText;
throw new ApplicationException(msg);
}
extendedType = compilerResults.CompiledAssembly.GetType("ExtendedCustomer");

ActiveRecordStarter.RegisterTypes(extendedType);
}
}
}
Andrus
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform