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());
}
}
}
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);
}
}
}