具有2个数据访问层的Dot Net应用程序一次使用1个 [英] Dot Net application having 2 data Access Layer use 1 at a time

查看:53
本文介绍了具有2个数据访问层的Dot Net应用程序一次使用1个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dir Dir


我正在设计一个具有2个DataAccessLayer且使用相同方法的数据库应用程序
1个用于MSSQL,第二个用于Oracle.


(Sql女士,具有相同数据库名称和表结构的Oracle)


主要DAL是MS SQL Server,但我想问一下如何将我的应用程序从MSSSQL切换到Oracle,只需单击一下即可.


问候

[edit]删除了虚假代码块,禁用了忽略HTML ..."选项-OriginalGriff [/edit]

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]

推荐答案

耦合业务和数据层.您可以在这里很好地利用依赖注入.这应该很清楚:
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) { }
        }



考虑上面的代码.在这里,数据访问层可以适应任何实现IDbConnection,IDbCommand等接口的提供程序.当用户在屏幕上选择提供程序时,特定的提供程序将分配给数据访问层.

您也可以为此使用Unity应用程序块.这应该可以帮助您通过配置文件来进行配置.您也可以自己执行此操作.

希望这可以帮助. :)



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. :)


这篇关于具有2个数据访问层的Dot Net应用程序一次使用1个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆