Today I'm so excited to announce the first release of the Codoxide.Common Library.
I haven't been over ambitious about the version numbering as I want you to treat this library to still be in concept stage.
Unfortunately, this release had taken longer than expected cos of my newly cramped up schedule. Worst part of that is I'm posting this code without proper documentation (yep, excuses!). Nevertheless, there's plenty of code in here to prove useful to many. So here goes!
Features/Components
Patterns
- SharedInstance<T>
- Singleton<T>
- Factory e.g. Factory.Build<T>()
Configuration
- ConfigurationManager<T>
- ConfigurationSectionBase
Data
- Database<CONNECTION_TYPE, COMMAND_TYPE, ADAPTER_TYPE>
- Map<T>
SharedInstance<T>
SharedInstance is actually the most basic type of generic Singleton implementation that's been around in the C# community. This implementation was both simple and efficient. But, it violated the definition of a Singleton by depending on the presence of a public constructor and thereby allowing additional instances to be created. Therefore, I have refrained from calling this a Singleton resorted to the term SharedInstance.
E.g:
[code:c#]
SharedInstance<User>.Instance.Login("admin", "password");
User employee1 = new User();
employee1.Username = "emp001";
employee1.Password = "pass001";
employee1.Save();
[/code]
Singleton<T>
More advanced implementation of the generic Singleton implementation. Uses reflection, custom attributes and "lazy initialization" to provide a powerful way to create and initialize your singletons.
E.g:
[code:c#]
public class UserPreferences : IXmlSerializable, ISupportLazyInitialization
{
..
public virtual void Initialize()
{
try
{
string location = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Codoxide\\preference.xml";
if (File.Exists(location))
using (XmlReader reader = XmlReader.Create(location))
{
reader.ReadToFollowing("Preferences");
this.ReadXml(reader);
reader.ReadEndElement();
}
}
catch (Exception ex)
{
throw new InvalidConfigurationException("Exception occurred while loading the preferences from disk", ex);
}
}
..
}
...
Console.WriteLine(Singleton<UserPreferences>.Instance.BackgroundColor);
[/code]
Factory.Build<T>
This one's another powerful and unique implementation. Coolest feature of this class is that you can do something like the following:
[code:c#]
using (IDatabase db = Factory.Build<IDatabase>())
{
..
}
[/code]
This allows developers of class libraries to write their code using interfaces or abstract classes, leaving the library users to specify the concrete classes via configuration files.
Grab the Code
There's of course, plenty more to be documented regarding the library. And you can expect the project space to be updated in coming (err..) weeks
.
Until then, you can:
Check out the code (using SVN): https://codoxidelib.svn.sourceforge.net/svnroot/codoxidelib/trunk
Browse the project space: http://codoxidelib.sourceforge.net/