问题描述
目前我的应用程序的UI层连接到我的DAL的DLL。达尔是这样初始化的:
为AS400 //初始化数据访问
达尔= JDE8Dal.Instance;
Dal.conString = Properties.Settings.Default.conAS400;
DAL是desinged为单例。我认为,迫使应用程序有一个实例是一个好主意。
DAL:
公共类JDE8Dal
{
公共字符串conString {获得;组; }
私人静态只读JDE8Dal _instance =新JDE8Dal();
私人JDE8Dal()
{
}
公共静态JDE8Dal实例
{
{返回_instance; }
}
//方法
}
我的BLL会是这个样子:
命名空间YLA.Bar code
{
公共静态类YlaBar codeUtil
{
公共静态字符串LotStripZeroes(字符串str)
{
VAR RET =海峡;
如果(str.Trim()。StartsWith(00))
{
RET = YlaGeneralUtilities.StripLeadingNumofChars(STR,2);
}
返回RET;
}
}
公共类酒吧codeBLL
{
// DAL INIT在这里?
}
}
现在,我需要建立更多的应用程序,我需要进入一个3层架构,并开始阅读DDD。
1)如何移动DAL处理中BLL?只需添加初始化我BLL节?
2)我应该让我的DAL设计为单身或没有?
您应该使用反向控制模式,以降低层间的依赖关系。你的情况我会用一个构造函数注入的数据上下文所要求的类:
public类吧codeBLL
{
私人JDE8Dal _context;
公共酒吧codeBLL(JDE8Dal上下文)
{
_context =背景;
}
}
数据方面应该是短暂的对象。如果您正在开发一个Web应用程序,你应该实例每个请求一个数据上下文。我也建议使用ORM(实体框架/ NHibernate的)。
Currently my application's UI layer is coupled to my DAL dll. Dal is initialized like this:
//Initialize Data Access for AS400
Dal = JDE8Dal.Instance;
Dal.conString = Properties.Settings.Default.conAS400;
DAL is desinged as singleton. I thought that forcing the application to have one instance is a good idea.
DAL:
public class JDE8Dal
{
public string conString { get; set; }
private static readonly JDE8Dal _instance = new JDE8Dal();
private JDE8Dal()
{
}
public static JDE8Dal Instance
{
get { return _instance; }
}
// Methods
}
My BLL will look something like this:
namespace YLA.Barcode
{
public static class YlaBarcodeUtil
{
public static string LotStripZeroes(string str)
{
var ret = str;
if (str.Trim().StartsWith("00"))
{
ret = YlaGeneralUtilities.StripLeadingNumofChars(str, 2);
}
return ret;
}
}
public class BarcodeBLL
{
//DAL INIT HERE?
}
}
Now that i need to build more applications i need to move into a 3 layer architecture and start reading on DDD.
1) How to move the DAL handling in BLL? Just add the initialization in my BLL section?
2) Should i keep my DAL design as singleton or not?
You should use the Inversion of Control pattern to reduce the dependency between your layers. In your case I would use a constructor injection as the data context is required by your class:
public class BarcodeBLL
{
private JDE8Dal _context;
public BarcodeBLL(JDE8Dal context)
{
_context = context;
}
}
Data context should be short lived objects. If you're developing a web application you should instantiate one data context per request. I would also recommend using an ORM (Entity Framework / NHibernate).
这篇关于从用户界面删除数据访问层耦合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!