开发者

DataProvider Switch / Singleton ?

I have a website and want to step over from MySql to SqlServer.

In my website I use a BE layer (Business Entities), BLL (Business Logic Layer), DAL (Data Access Layer), and of course a Web Layer (Website)

I've created a switch in the web.config (app settings) to tell the website to use MySql or SqlServer as DataProvider.

I use the folowing code.... which works perfectly, but my question is ...

  • Is this the right way to use a multi DAL tier?
  • Is this thread Safe ? or I have to implement Singleton at the Factory class ?

Please let me know what you should do, or what your opionion is.

namespace MyNameSpace.BE {
    public class Product {
        public int Id { get; set; }
        public int Description { get; set; }
    }
}
namespace MyNameSpace.DAL.Base {
    public abstract class ProductManager {
        public abstract List<Product> GetProductList();
    }
}
namespace MyNameSpace.DAL.MySql {
    public class ProductManager : Base.ProductManager {
        public override List<Product> GetProductList() {
            return new List<Product>();
        }
    }
}
namespace MyNameSpace.DAL.SqlServer {
    public class ProductManager : Base.ProductManager {
        public override List<Product> GetProductList() {
            return new List<Product>();
        }
    }
}

namespace MyNameSpace.Bll {

    /// do I have to use Singleton here ?? or not ?
    public static class Factory {

        private static ProductManager CreateProductManager() {

            if (Config.Settings.Switches.DataProvider == DataProvider.MySql) {
                return new DAL.MySql.ProductManager();
            } else if (Config.Settings.Switches.DataProvider == 
                                 DataProvider.SqlServer) {
                return new DAL.SqlServer.ProductManager();
            }
            return null;
        }

        private static ProductManager _ProductManager;
        public static ProductManager ProductManager {
            get {
                if (_ProductManager == null) {
                    _ProductMan开发者_Python百科ager = CreateProductManager();
                }
                return _ProductManager;
            }
        }
    }
}

/// <summary>
/// for example ASP.NET page
/// </summary>
class MyPage {

    public MyPage() {
        List<Product> productList = Factory.ProductManager.GetProductList();
    }

}


I recommend that you use an Inversion of Control (IoC) container. One such IoC container can be found in Microsoft Unity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜