【问题标题】:Persist Data by Programming Against Interface通过针对接口编程来持久化数据
【发布时间】: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
{
      ..
    }
}

阅读

  1. Optimizing Repository’s SubmitChanges Method

  2. How do you abstract out your persistence code when using LINQ to SQL?

  3. LINQ to SQL - mapping exception when using abstract base classes

【问题讨论】:

  • theNameOfYourDBContext.SaveChanges()
  • @Boomer 我在存储库方法中只收到一个接口。您的建议无效
  • 如果您知道您的 IBankAccount 参数肯定是 BankAccount 类型,您可以直接转换对象...?
  • @Shaul。但这并不能提供所需的帮助。参数应该是 BankAccount 类型。这意味着客户端必须知道 BankAccount(将参数传递给函数)——不仅仅是接口。

标签: c# nhibernate linq-to-sql tdd domain-driven-design


【解决方案1】:

您的存储库应该接受 BankAccount - 而不是 IBankAccount 因为 Linq-to-sql 不知道什么是 IBankAccount 并且编译器不允许您在不先将其转换为 BankAccount 的情况下存储它(即如果IBankAccount 实例不是BankAccount,则显然会在运行时失败。

一旦您拥有BankAccount,您只需拨打电话:

Context.BankAccounts.Add(account);
Context.SubmitChanges();

【讨论】:

  • 谢谢。然而,这不会是“编程到接口”,不是吗?以下是 Scott 在weblogs.asp.net/scottgu/archive/2007/06/29/… 中的评论 - “将接口添加到您的 LINQ to SQL 数据模型类。LINQ to SQL 类是部分类 - 这意味着您可以直接将接口添加到它们。”这个建议在我们的场景中不起作用。有没有办法让它工作?
【解决方案2】:

据我了解,您需要为银行系统建模。这意味着处理银行账户。这是一项艰巨的业务,没有“Hello World!”类型的信息技术。

基本上你需要的银行账户:

  • 读取详细信息(例如帐户名称、值等
  • 借记账户
  • 信用账户
  • 终止帐户
  • 创建一个新帐户
  • ...和其他操作

借方和贷方操作是所有操作中最具“交易性”的操作,因为这意味着您将一次编辑两个帐户,并且您希望同时成功或同时失败。

这又是一项有风险的业务,因为它涉及检查许多业务规则,首先是账户上有足够的资金(这并不那么简单,因为拥有透支账户意味着您可以低于零)一直到确保有一个持久的交易,从一个账户中减去钱,向另一个账户增加钱。

您还必须检查一个人是否试图转移负金额,因为这实际上是从其他账户窃取资金。列表并不止于此。 Scott Hanselman 在一家银行工作了一段时间,他可能会对需要检查的内容有所帮助。

总而言之,我的回复似乎是“不值得”的回复,您不妨投反对票,但同样,主题太深,无法在 stackoverflow 回复中涵盖。

对不起。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    相关资源
    最近更新 更多