11,437,265 members (63,605 online)

The time machine

, 9 May 2012 CPOL
 Rate this:
Long time strategy of software design and development.

1. Introduction

Math contains levels of theories. First level arithmetic contains theorems about integers. Second level theorems (metatheorems) are theorems about first level theorems. There third arithmetic, forth one, etc. The duality theorem concerns to theorems and therefore it is metatheorem. In this chapter metadriver is discussed. Following code can be regarded as metadriver. This code is looking for singletons of objects which implement one interface:

```        /// <span class="code-SummaryComment"><summary>
</span>        /// Gets interfaces from assembly (Searching of driver assembly)
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><typeparam name="T">Interface type</typeparam>
</span>        /// <span class="code-SummaryComment"><param name="assembly">Assembly</param>
</span>        /// <span class="code-SummaryComment"><returns>Interface objects</returns>
</span>        public static IEnumerable<T> GetInterfaces<T>(this Assembly assembly) where T : class
{
Type[] types = assembly.GetTypes(); // Assembly types
string st = typeof(T).FullName;     // Interface full name
foreach (Type t in types)
{
Type ti = t.GetInterface(st);   // Gets interface
if (ti == null)
{
continue;
}
FieldInfo fi = t.GetField("Singleton"); // Gets Singleton field
if (fi != null)
{
yield return fi.GetValue(null) as T; // yeild Singleton
}
}
}
```

Following code snippet contains search of singletons in a set of assemblies

```         /// <span class="code-SummaryComment"><summary>
</span>        /// Gets interfaces
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><typeparam name="T">Interface type</typeparam>
</span>        /// <span class="code-SummaryComment"><param name="directory">Directory</param>
</span>        /// <span class="code-SummaryComment"><returns>Interface objects</returns>
</span>        public static IEnumerable<T> GetInterfaces<T>(this string directory) where T : class
{
string[] fn = Directory.GetFiles(directory, "*.dll");   // Dll files
Assembly[] ass = AppDomain.CurrentDomain.GetAssemblies(); // Current domain assemblies
List<string> l = new List<string>();
foreach (Assembly a in ass) // Looking for objects in loaded assemblies
{
IEnumerable<T> en = a.GetInterfaces<T>();
foreach (T t in en)
{
yield return t;
}

}
foreach (string f in fn) // Looking for objects in directory
{
if (!l.Contains(f))
{
IEnumerable<T> en = Assembly.LoadFile(f).GetInterfaces<T>();
foreach (T t in en)
{
yield return t;
}
}
}
}

/// <span class="code-SummaryComment"><summary>
</span>        /// Gets interfaces from base directory
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><typeparam name="T">Type</typeparam>
</span>        /// <span class="code-SummaryComment"><returns>Interfaces</returns>
</span>        public static IEnumerable<T> GetInterfacesFromBaseDirectory<T>() where T : class
{
return AppDomain.CurrentDomain.BaseDirectory.GetInterfaces<T>();
}
```

In many cases one driver only is needed. Following code contains looking for a single driver:

```         /// <span class="code-SummaryComment"><summary>
</span>        /// Gets first interface object
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><typeparam name="T">Type of interface</typeparam>
</span>        /// <span class="code-SummaryComment"><param name="directory">Directory</param>
</span>        /// <span class="code-SummaryComment"><returns>First object</returns>
</span>        public static T GetFirstInterfaceObject<T>(this string directory) where T : class
{
IEnumerable<T> en = directory.GetInterfaces<T>();
foreach (T t in en)
{
return t;
}
return null;
}

/// <span class="code-SummaryComment"><summary>
</span>        /// Gets first interface object
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><typeparam name="T">Type of interface</typeparam>
</span>        /// <span class="code-SummaryComment"><returns>First object</returns>
</span>        public static T GetFirstInterfaceObjectFromBaseDirectory<T>() where T : class
{
return AppDomain.CurrentDomain.BaseDirectory.GetFirstInterfaceObject<T>();
}
```

Instruction of above code usage is very simple. Dynamic link library file (files) should be copied to base directory of current domain.

3 Applications

3D Graphics

Following code represents common 3D Graphics interface:

```     /// <span class="code-SummaryComment"><summary>
</span>    /// Factory of 3D objects and cameras
/// <span class="code-SummaryComment"></summary>
</span>    public interface IPositionObjectFactory
{
/// <span class="code-SummaryComment"><summary>
</span>        /// Creates  3D object
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="type">Object's type</param>
</span>        /// <span class="code-SummaryComment"><returns>The object</returns>
</span>        object CreateObject(string type);

/// <span class="code-SummaryComment"><summary>
</span>        /// Creates new camera
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><returns></returns>
</span>        Camera NewCamera();

/// <span class="code-SummaryComment"><summary>
</span>        /// Creates editor of properties of camera
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="camera">The camera</param>
</span>        /// <span class="code-SummaryComment"><returns>The property editor</returns>
</span>        object CreateForm(Camera camera);

/// <span class="code-SummaryComment"><summary>
</span>        /// Creates editor of properties of visible object
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="position">Position of object</param>
</span>        /// <span class="code-SummaryComment"><param name="visible">Visible object</param>
</span>        /// <span class="code-SummaryComment"><returns>Editor of properties of visible object</returns>
</span>        object CreateForm(IPosition position, IVisible visible);

/// <span class="code-SummaryComment"><summary>
</span>        /// Type of camera
/// <span class="code-SummaryComment"></summary>
</span>        Type CameraType
{
get;
}

/// <span class="code-SummaryComment"><summary>
</span>        /// Creates label on desktop
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="obj">Object for label</param>
</span>        /// <span class="code-SummaryComment"><returns>The label</returns>
</span>        IObjectLabel CreateLabel(object obj);

/// <span class="code-SummaryComment"><summary>
</span>        /// Creates label of visible object
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="position">Position of object</param>
</span>        /// <span class="code-SummaryComment"><param name="visible">Visible object</param>
</span>        /// <span class="code-SummaryComment"><returns>Editor of properties of visible object</returns>
</span>        object CreateLabel(IPosition position, IVisible visible);
```

OpenGL and WPF implementations of this interface are developed at present day. Following class diagram represents these implementations:

Every implementation class contains `Singleton` field as it is presented below:

```     /// <span class="code-SummaryComment"><summary>
</span>    /// WPF implementation of 3D Graphics
/// <span class="code-SummaryComment"></summary>
</span>    public class WpfFactory : PositionObjectFactory
{
#region Fields

/// <span class="code-SummaryComment"><summary>
</span>        /// Singleton
/// <span class="code-SummaryComment"></summary>
</span>        public static readonly WpfFactory Singleton =
new WpfFactory();

#endregion

#region Ctor

/// <span class="code-SummaryComment"><summary>
</span>        /// Private default constructor
/// <span class="code-SummaryComment"></summary>
</span>        private WpfFactory()
{
}

#endregion
```

Both these implementations are contained in different dynamic link libraries.

 N Technology Implementation class Dynamic link library file 1 OpenGL OpenGLFactory InterfaceOpenGL.dll 2 WPF WpfFactory WpfInterface.UI.dll

Following code snippet represents selection of 3D Graphics interface:

```         /// <span class="code-SummaryComment"><summary>
</span>        /// Global factory
/// <span class="code-SummaryComment"></summary>
</span>        public static IPositionObjectFactory BaseFactory
{
get
{
if (baseFactory == null)
{
baseFactory =
AssemblyService.StaticExtensionAssemblyService.GetFirstInterfaceObjectFromBaseDirectory<IPositionObjectFactory>();
}
return baseFactory;
}
}
```

So if we would like use OpenGL technology then `InterfaceOpenGL.dll` file should be copied to base directory etc. More information about applications of 3D Graphics is contained in article devoted to virtual reality.

3.2 Scientific data

3.2.1 Scientific data processing

Database can be good warehouse of scientific data. Otherwise user of software should have good interface for data access. Let us consider sample both data access and data processing. In this sample we have following two nonlinear functions f1, f2:

Parameters a, b, c, d, g of these functions are not known. Values of functions and variables x, y, z are stored in database which have following scheme:

Parameters with "id" prefix are id of values of variables. Meaning of database parameters is clear. Following SQL Query provides retrieving of values of variables and functions.

```SELECT x.x, y.y, z.z, f1.f1, f2.f2 FROM f1, f2, x, y, z WHERE x.Id = f1.Idx AND y.Id = f1.Idy AND z.Id = f1.Idz AND f1.Idx = f2.Idx AND f1.Idy = f2.Idy AND f1.Idz = f2.Idz
```

Now let us consider full solution of this task. Following picture contains components of this task:

.

The Data component performs Database query. Properties of Data are presented below:

Properties of this component are clear. The Sql Server driver is used. Other properties are connection string and SQL Query. The Iterator component can be regarded as cursor. The Formula component contains definition of f1, f2. Properties of Formula are presented below:

Parameters a, b, c, d, g are constants of formulae. Variables x, y, z correspond to retrieved from database real numbers. The Processor component performs definition of above constants. Properties of Processor are presented below:

Left panel of above picture means that we would like to define constants a, b, c, d, g of Formula component. Right panel means that we would like approximate retrieved from database f1, f2 by Formula_1 and Formula_2 of Formula. Left and right parts of below picture contain prior and posterior values.

3.2.2 Database drivers

In above sample Sql Server has been used. It is clear that scientist can use Oracle for same purpose. Software should support following options:

Drivers should be contained in different assemblies. Low coupled software should separate Oracle driver from SQL Server one since good software should support following options:

• Support of Oracle;
• Support of SQL Server;
• Support of both Oracle and SQL Server;
• Do not support any database;

So soft should contain abstract database layer. Following code represent this abstract layer

```    /// <span class="code-SummaryComment"><summary>
</span>    /// Factory for creating data set metadata from connection
/// <span class="code-SummaryComment"></summary>
</span>    public interface IDataSetFactory
{
/// <span class="code-SummaryComment"><summary>
</span>        /// Name of factory
/// <span class="code-SummaryComment"></summary>
</span>        string FactoryName
{
get;
}

/// <span class="code-SummaryComment"><summary>
</span>        /// Creates connection
/// <span class="code-SummaryComment"></summary>
</span>        DbConnection Connection
{
get;
}

/// <span class="code-SummaryComment"><summary>
</span>        /// Command
/// <span class="code-SummaryComment"></summary>
</span>        DbCommand Command
{
get;
}

/// <span class="code-SummaryComment"><summary>
</span>        /// Gets metadata data set from connection
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="connection">The connection</param>
</span>        /// <span class="code-SummaryComment"><returns>The metadata data set</returns>
</span>        DataSet GetData(DbConnection connection);

/// <span class="code-SummaryComment"><summary>
</span>        /// Gets metadata data set from connection string
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="connectionString">The connection</param>
</span>        /// <span class="code-SummaryComment"><returns>The metadata data set</returns>
</span>        DataSet GetData(string connectionString);

/// <span class="code-SummaryComment"><summary>
</span>        /// Data adapter
/// <span class="code-SummaryComment"></summary>
{
get;
}

/// <span class="code-SummaryComment"><summary>
</span>        /// Creates type from metadata row
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="row">The metadata row</param>
</span>        /// <span class="code-SummaryComment"><returns>The type</returns>
</span>        Type GetTypeFromRow(DataRow row);

/// <span class="code-SummaryComment"><summary>
</span>        /// Table name in metadata
/// <span class="code-SummaryComment"></summary>
</span>        string TableName
{
get;
}

/// <span class="code-SummaryComment"><summary>
</span>        /// Column name in metadata
/// <span class="code-SummaryComment"></summary>
</span>        string ColumnName
{
get;
}

/// <span class="code-SummaryComment"><summary>
</span>        /// Is nullable in metadata
/// <span class="code-SummaryComment"></summary>
</span>        string IsNullable
{
get;
}

/// <span class="code-SummaryComment"><summary>
</span>        /// Gets string representation of type
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="type">The type</param>
</span>        /// <span class="code-SummaryComment"><returns>The string representation</returns>
</span>        string GetStringFromType(Type type);

/// <span class="code-SummaryComment"><summary>
</span>        /// Generates statement from desktop
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="desktop">The desktop</param>
</span>        /// <span class="code-SummaryComment"><returns>The statement</returns>
</span>        string GenerateStatement(IDataSetDesktop desktop);

/// <span class="code-SummaryComment"><summary>
</span>        /// Gets object type
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="column">Column</param>
</span>        /// <span class="code-SummaryComment"><returns>The type</returns>
</span>        object GetObjectType(DataColumn column);

/// <span class="code-SummaryComment"><summary>
</span>        /// Generates script
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="column">Column</param>
</span>        /// <span class="code-SummaryComment"><returns>Script</returns>
</span>        string GenerateScript(IColumn column);

/// <span class="code-SummaryComment"><summary>
</span>        /// Generates create script
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="table">Table</param>
</span>        /// <span class="code-SummaryComment"><returns>Script lines</returns>
</span>        List<string> GenerateScript(ITable table);

/// <span class="code-SummaryComment"><summary>
</span>        /// Generates creation script from metadata
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="metaData">Meatadata data set</param>
</span>        /// <span class="code-SummaryComment"><returns>Creation script</returns>
</span>        List<string> GenerateScript(DataSet metaData);

}        ```

List of database drivers can be extended. Now ODBC, Sql Server and Oracle drivers are already developed. Following picture represents class diagram of these drivers.

Following code represents usage of these drivers:

```        /// <span class="code-SummaryComment"><summary>
</span>        /// Initialization of database drivers
/// <span class="code-SummaryComment"></summary>
</span>        /// <span class="code-SummaryComment"><param name="path">Path of drivers</param>
</span>        void Initialize(string path)
{
// Gets all database drivers from this directory
IEnumerable<IDataSetFactory> en = path.GetInterfaces<IDataSetFactory>();
List<string> l = new List<string>();
foreach (IDataSetFactory f in en)
{
factories[f.FactoryName] = f; // Dictinary of drivers
}
List<string> ln = new List<string>(factories.Keys);
ln.Sort();
names = ln.ToArray(); // Ordered names of drivers
}        ```
If Oracle driver is used instead Sql Server one then properties of Data would be changed by following way:

In above picture driver and connection string are changed. In some cases change of query is required.

3.3 Other samples

List of samples can be extended. However general idea is already clear. Other samples shall be described in future specific articles

Points of Interest

I do not know what is more useful, my code or inspiration of you for good work even if you are threatened with a madhouse.

Share

Architect
Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

 First Prev Next
 My vote of 5 Kanasz Robert28-Sep-12 8:19 Kanasz Robert 28-Sep-12 8:19
 My vote of 5 Mohammad A Rahman22-May-12 18:48 Mohammad A Rahman 22-May-12 18:48
 My vote of 5 Volynsky Alex21-May-12 7:38 Volynsky Alex 21-May-12 7:38
 Interesting, as always with Dr. Ivankov ZakPatat9-May-12 12:50 ZakPatat 9-May-12 12:50
 I'm not sure... ii_noname_ii9-May-12 4:49 ii_noname_ii 9-May-12 4:49
 Re: I'm not sure... Vitaly Tomilov9-May-12 8:10 Vitaly Tomilov 9-May-12 8:10
 Re: I'm not sure... Petr Ivankov9-May-12 9:13 Petr Ivankov 9-May-12 9:13
 Re: I'm not sure... bitterskittles9-May-12 23:51 bitterskittles 9-May-12 23:51
 stopped reading at "homo-"
 Re: I'm not sure... ZakPatat9-May-12 13:12 ZakPatat 9-May-12 13:12
 Re: I'm not sure... Vitaly Tomilov9-May-12 16:21 Vitaly Tomilov 9-May-12 16:21
 Last Visit: 31-Dec-99 19:00     Last Update: 6-May-15 1:57 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.