DAO
BLL
Entity IDAO
Web
我个人觉得应该把BLL的IDAO给单独剥离出来作为一个项目。这样层次结构会清晰一些。
DAO实现IDAO的接口。BLL包含实体类,和相应的业务逻辑类,外加一个对象工厂类。
public bool SignIn(){
// Validate input
if ((UserId.Trim() == string.Empty) || (Password.Trim() == string.Empty)) return false;
// Try to sign in with the given credentials
return ((ISignOnDAO)ObjectFactory.GetInstance("SignOnDAO")).SignIn(this);
}
// Validate input
if ((UserId.Trim() == string.Empty) || (Password.Trim() == string.Empty)) return false;
// Try to sign in with the given credentials
return ((ISignOnDAO)ObjectFactory.GetInstance("SignOnDAO")).SignIn(this);
}
用对象工厂去获取DAO的实例,然后转化成借口,并且调用相应的登陆方法。
而对象工厂
using System;
using System.Web;
using Spring.Context;
using Spring.Context.Support;
namespace PetShop.BLL{
public class ObjectFactory{
private static IApplicationContext ctx;
static ObjectFactory() {
ctx = new XmlApplicationContext( "assembly://PetShop.DAO/PetShop/objects.xml" );
}
public static Object GetInstance(string name) {
return ctx.GetObject(name);
}
}
}
using System.Web;
using Spring.Context;
using Spring.Context.Support;
namespace PetShop.BLL{
public class ObjectFactory{
private static IApplicationContext ctx;
static ObjectFactory() {
ctx = new XmlApplicationContext( "assembly://PetShop.DAO/PetShop/objects.xml" );
}
public static Object GetInstance(string name) {
return ctx.GetObject(name);
}
}
}
也使用了一个静态的构造函数,来初始化PetShop.DAO项目的PetShop.objects.xml文件,已经初始化这些类。
还有一个静态的方法来返回依据名称而对应的对象。
使用了Sprint.net