我有一個IBankAccount接口,我將傳遞給ApplicationService。帳戶對象(在ApplicationService項目中)所做的更改需要在數據庫中保存。存儲庫使用IBankAccount接口接收更改。我怎樣才能將這些數據保存到數據庫中?這是使用LINQ to SQL實現的。 。通過接口編程保存數據
注:以下是從斯科特http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx 評論「到你的LINQ的接口添加到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
{
..
}
}
READING
How do you abstract out your persistence code when using LINQ to SQL?
LINQ to SQL - mapping exception when using abstract base classes
theNameOfYourDBContext.SaveChanges() – Boomer
@Boomer我只接收存儲庫方法中的一個接口。您的建議不起作用 – Lijo
如果您知道您的IBankAccount參數肯定是BankAccount類型的,您可以只投出對象...? –