数据提供者切换/单例?
我有一个网站,想从 MySql 过渡到 SqlServer。
在我的网站中,我使用 BE 层(业务实体)、BLL(业务逻辑层)、DAL(数据访问层),当然还有 Web 层(网站)
我在 web.config(应用程序设置)中创建了一个交换机)告诉网站使用MySql或SqlServer作为DataProvider。
我使用以下代码....它工作得很好,但我的问题是...
- 这是使用多 DAL 层的正确方法吗?
- 该线程安全吗?或者我必须在工厂类中实现单例?
请让我知道您应该做什么,或者您的意见是什么。
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) {
_ProductManager = CreateProductManager();
}
return _ProductManager;
}
}
}
}
/// <summary>
/// for example ASP.NET page
/// </summary>
class MyPage {
public MyPage() {
List<Product> productList = Factory.ProductManager.GetProductList();
}
}
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) {
_ProductManager = CreateProductManager();
}
return _ProductManager;
}
}
}
}
/// <summary>
/// for example ASP.NET page
/// </summary>
class MyPage {
public MyPage() {
List<Product> productList = Factory.ProductManager.GetProductList();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用控制反转 (IoC) 容器。 Microsoft Unity 中可以找到这样一种 IoC 容器。
I recommend that you use an Inversion of Control (IoC) container. One such IoC container can be found in Microsoft Unity.