Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Dir


I am designing a database Application which has 2 DataAccessLayer with same method
1 for MSSQL and second For Oracle.


( Ms Sql , Oracle with Same database name and table structure )


Primary DAL is Ms SQL server but i want to ask how can i switch my application from MSSSQL to Oracle just 1 click thanks.


regards

[edit]Spurious code block removed, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 7-May-11 22:44pm
v2

1 solution

This is a very good need that asks for loosely coupled business and data layer. You can very well make use of Dependency Injection here. This should make it clear:
public class PresentationLayer
        {
            private void ClickDataAccessSelectionButton(object sender, EventArgs e)
            {

                Entity entity = new Entity();
                // Add data to entity object
                BusinessLayer businessLayer = new BusinessLayer();
                businessLayer.Provider = "user selection here";

                businessLayer.UpdateData(entity);
            }
        }

        public class BusinessLayer
        {
            private string _provider;
            public string Provider
            {
                get
                {
                    return _provider;
                }
                set
                {
                    _provider = value;
                    SetProvider();
                }
            }

            public DataAccess DataAccessLayer { get; set; }

            private void SetProvider()
            {
                if (_provider == "SQL")
                {
                    DataAccessLayer.Command = new SqlCommand();
                    DataAccessLayer.Connection = new SqlConnection();
                }
                else
                {
                    // Set oracle specific values
                }
            }

            public void UpdateData(Entity yourBusinessObject)
            {
                DataAccessLayer.Update(yourBusinessObject);
            }
        }

        public class DataAccess
        {
            public IDbConnection Connection
            {
                get;
                set;
            }

            public IDbCommand Command
            {
                get;
                set;
            }

            public void Update(Entity yourBusinessObject) { }
        }


Consider the code above. Here, the data access layer can adapt to any provider who implements IDbConnection, IDbCommand etc interfaces. When the user selects the provider on the screen, then the specific provider will be assigned to the data access layer.

You can make use of Unity application block too for this. This should help you get things configurable through the configuration files. You can do this on your own too.

Hope this helps. :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900