Codoxide.Common Library Version 0.0.1 (Concept Phase): Database Wrapper, Basic Design Patterns, Configuration Handlers

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:

SharedInstance<User>.Instance.Login("admin", "password");

User employee1 = new User();
employee1.Username = "emp001";
employee1.Password = "pass001";
employee1.Save();

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:

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

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:

using (IDatabase db = Factory.Build<IDatabase>())
{
..
}

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/

7 thoughts on “Codoxide.Common Library Version 0.0.1 (Concept Phase): Database Wrapper, Basic Design Patterns, Configuration Handlers

  1. Hi,

    Thanks for the great project …

    I noticed one strange behavior in the Codoxine.Common.Data.Database class or the way .Net handles connections to Sql Server …

    Once the user is logged in the Dispose() method has been called ( becase of the using ( Database = new Database… { .. } …
    the sql server keeps a process for the very same connection … and when this process is forcely killed the inproc session on IIS dies and the user is logout …

    We have a very complicated authentication mechanism … so it might be due to my code also … unless you have noticed the same kind of cashing on Sql Server …

    .. yet event I added connectio.Close () in the method

        /// &lt;summary&gt;Disposes the resources associated with the current database connection.&lt;/summary&gt; 
        public virtual void Dispose() 
        { 
            if (null != internal_currentConnection) 
            { 
                internal_currentConnection.Close (); //even this line was added ...  
                internal_currentConnection.Dispose(); 
                internal_currentConnection = null; 
            } 
        } 
    

    Any thoughts ?!

    Like

  2. HI,
    Not quite sure what exactly's going on. Few questions:
    – Did you see this behavior going away once you've added the connection.Close() method?
    – Could it be just that what you see is the SQL server connection pool allocated by IIS (or specifically ASP.NET process)? This is probably not the case if the answer to the first one is 'yes'

    Like

  3. *** – Did you see this behavior going away once you've added the connection.Close() method?
    – Yes the Session goes away even after calling the Close method
    *** – Could it be just that what you see is the SQL server connection pool allocated by IIS (or specifically ASP.NET process)?

    Probably this the right guess … There might be some configuration possibility to discover also … The reason why we started this investigation were the strange locks happening on the database , after some complicated callings via xp_cmdshell to executables .. .in 2% of the login actions …

    Sometimes it feels frustrating when after writing code , which "should work exactly as expected" the underlaying environment wipes out all expectations …

    Like

  4. [quote]Sometimes it feels frustrating when after writing code , which "should work exactly as expected" the underlaying environment wipes out all expectations …[/quote]
    Yeah, I know what you mean 🙂

    Anyway, I'm not really sure I properly understand the problem. Are you saying that when you do the Close() (or Dispose()), the user gets logged out?
    Or are you just worried about a process dedicated to each connection seems to be hogging up your server resources?

    Like

  5. **** Are you saying that when you do the Close() (or Dispose()), the user gets logged out ?
    Nope. But when admin does forcely kill th e process dedicated for this user on the Sql Server

    In 2% of the time the sql logins get locked, but it most probably has to do something with the way we authenticate users – via external IS and long chain of events ..

    Anyway thanks for the reply.

    I have build a generic layer for calling the old Abstract Database , which relies heavily on a user object , so if you have time …. are interested in I could share those … or should I may be try to submit something to this library as

    What might be of interest ( plus for this library ) is the error handler in the this "generic layer" – e.g. each db access method has mutiple catch statements which do debug the most common exceptions straight to Sql server ( a call with procedure which alwyas works as long as db connection is presnet) , thus online debugging is possible
    I use also userObj.ConnectionStr = "Data Source={O:G};Initial Catalog={1:G};Persist Security Info = False;User ID={2;G};Password={3:G};MultipleResultSets=True" , which provides trully dynamic connection string – e.g. only swithing of db , user , pass etc.

    So drop me e-mail if you are interested in .. .

    Like

  6. Hi Georg,

               Kindly explain the code below and how it works and let me know if i want to change the Database name,Will it be possible? 
    

    "Data Source={O:G};Initial Catalog={1:G};Persist Security Info = False;User ID={2;G};Password={3:G};MultipleResultSets=True"

    Thanks in advance.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.