【发布时间】:2012-07-02 17:18:58
【问题描述】:
我有一个将传递给 ApplicationService 的 IBankAccount 接口。对帐户对象(在 ApplicationService 项目中)所做的更改需要保存在数据库中。存储库使用 IBankAccount 接口接收更改。如何将这些数据保存到数据库中?这是使用 LINQ to SQL 实现的。
注意:以下是 Scott 在http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx 中的评论 “将接口添加到您的 LINQ to SQL 数据模型类。LINQ to SQL 类是部分类 - 这意味着您可以直接向它们添加接口。”
public class LijosSimpleBankRepository : ILijosBankRepository
{
public System.Data.Linq.DataContext Context
{
get;
set;
}
public virtual void UpdateAccount(DomainInterfaces.IBankAccount iBankAcc)
{
DBML_Project.BankAccount bankAccount;
}
}
namespace DomainInterfaces
{
public interface IBankAccount
{
int BankAccountID { get; set; }
string AccountType { get; set; }
System.Nullable<System.DateTime> OpenedDate { get; set; }
string Status { get; set; }
System.Nullable<int> AccountOwnerID { get; set; }
}
}
namespace DBML_Project
{
public class FixedBankAccount : BankAccount
{
//Note: BankAccount already implemnts IBankAccount
}
public class SavingsBankAccount : BankAccount
{
//Note: BankAccount already implemnts IBankAccount
}
//The auto generated calss is made as abstract
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.BankAccount")]
[InheritanceMapping(Code = "Fixed", Type = typeof(FixedBankAccount), IsDefault = true)]
[InheritanceMapping(Code = "Savings", Type = typeof(SavingsBankAccount))]
public abstract partial class BankAccount : INotifyPropertyChanging, INotifyPropertyChanged, DomainInterfaces.IBankAccount
{
..
}
}
阅读
【问题讨论】:
-
theNameOfYourDBContext.SaveChanges()
-
@Boomer 我在存储库方法中只收到一个接口。您的建议无效
-
如果您知道您的 IBankAccount 参数肯定是 BankAccount 类型,您可以直接转换对象...?
-
@Shaul。但这并不能提供所需的帮助。参数应该是 BankAccount 类型。这意味着客户端必须知道 BankAccount(将参数传递给函数)——不仅仅是接口。
标签: c# nhibernate linq-to-sql tdd domain-driven-design