12
As part of a microplataforma ORM I’m developing, I’m defining a generic class that implements exclusively tight Coupling (1 record x 1 object).
public class Course : MicroEntity<Course>
{
public string fullname { get; set; }
public string shortname { get; set; }
public string summary { get; set; }
public string format { get; set; }
[...]
}
To define the behavior of this class, I have a Attribute which contains all boot characteristics:
[MicroEntity(
TableName = "mdl_course",
IdentifierColumnName = "ID",
IsReadOnly = true,
UseDistributedCaching = true)]
public class Course : MicroEntity<Course>
{
public string fullname { get; set; }
public string shortname { get; set; }
public string summary { get; set; }
public string format { get; set; }
[...]
}
Recently I implemented a Databasedapter mechanism to allow agnostic connection to different databases:
public abstract class BaseAdapter
{
internal abstract void CheckDatabaseEntities<T>() where T : MicroEntity<T>;
internal abstract void SetSqlStatements<T>() where T : MicroEntity<T>;
internal abstract void SetConnectionString<T>() where T : MicroEntity<T>;
internal abstract void RenderSchemaMicroEntityNames<T>() where T : MicroEntity<T>;
internal abstract BaseDynamicParameters Parameters<T>(object obj) where T : MicroEntity<T>;
internal abstract DbConnection Connection(string connectionString);
}
From there, I declare Adapters for different banks. At the moment, I have adapters for Oracle and Mysql.
Question
I wish I could declare the adapter as a property of the Attribute:
[MicroEntity(
TableName = "mdl_course",
IdentifierColumnName = "ID",
IsReadOnly = true,
Adapter = new InternalAdapters.MySql.Adapter();
UseDistributedCaching = true)]
public class Course : MicroEntity<Course>
{
[...]
But the use of new()
is not allowed. Which model would best suit this type of behavior?
I think it depends a little on how you’re going to use the attribute. I need to focus more on the problem. Anyway, I can’t wait until you can publish this whole thing. It looks like it’s gonna get really interesting.
– Maniero
@bigown would only be during the constructor (once initialized it would not be possible to change the Adapter). I also intend to use this engine for the distributed caching engine - by default Redis, but any other Key/Value Storage could be used. However this would be a global Setting. Imagine the zone of 2 or more cache used at the same time.
– OnoSendai
I don’t know if it would suit your purpose but I guess I’d have to use one
enum
or another way to identify and have some method that treats this to create the instance when needed. This has some difficulties but has to automate so you don’t have to keep changing the code whenever you have a new adapter. It is not a simple and perfect solution, but it meets some needs. I need to think of others.– Maniero
@bigown To tell the truth, at the moment I’m using an Enum - internally I have a switch, and instant dynamism the type needed. But my doubt is exactly this, using new adapters without having to update the construction method.
– OnoSendai
One of the solutions I think is to write down these types and with reflection "discover" all existing. Another way is to have a way to "register" the adapters for this method to know where to look. Both require the adapter to be written in a way that informs it exists. Obviously the
enum
does not take care of this. http://answall.com/q/21997/101– Maniero
@bigown I have an interface that serves as contract for Addons, and any new Adapter need to implement this interface. I dynamically load all assemblies when the application is initialized, and so can identify new Adapters. I made an XGH implementation that checks a literal string against an interface property. Ugly to give do, I know, but 'works'.
– OnoSendai
I think you’re on the right track. It’s still a form of note. I’ll think about if I find something better.
– Maniero
@Bigown I appreciate any help!
– OnoSendai