日期:2014-05-17 浏览次数:20478 次
public class Customer { public int CustomerId{get;set;} public string CustomerName{get;set;} public string Address{get;set;} } public interface ICustomerDataAccess { bool Add(Customer customer); } //数据访问 public class CustomerDataAccess : ICustomerDataAccess { public bool Add(Customer customer) { string sql = @"INSERT INTO dbo.Customer (CustomerName,Address) VALUES (CustomerName,@Address)"; SqlParameter[] param = { new SqlParameter("@CustomerName",customer.CustomerName), new SqlParameter("@Address",customer.Address) }; return SQLHelper.ExecuteNoQuery(sql,param); } } //业务逻辑 public class CustomerBusiness { ICustomerDataAccess _customerDataAccess; //如果用这个构造函数实例化的话,就还是让CustomerBusiness依赖了具体的Customer实现(CustomerDataAccess) //所以用了一个带参数的构造函数注入 //public CustomerBusiness() //{ // _customerDataAccess = new CustomerDataAccess(); //} public CustomerBusiness(ICusomterDataAccess customerDataAccess) { _customerDataAccess = customerDataAccess; } public bool CreateCustomer(Customer customer) { return _customerDataAccess.Add(customer); } }
protected void btnSave_Click(object sender,EventArgs e) { //CustomerBusiness business = new CustomerBusiness( //就是这里该怎么调用CustomerBusiness呢? }
public class CustomerDataAccessFactory { //里面有个获取实例的方法 public static GetInstance() { //根据配置文件或写死 实例化哪个实体类 ICusomterDataAccess customerDataAccess = new XXXXXXX(); return customerDataAccess ; } }
------解决方案--------------------