通过针对接口编程来持久化数据 [英] Persist Data by Programming Against Interface

查看:23
本文介绍了通过针对接口编程来持久化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 IBankAccount 接口,我将把它传递给 ApplicationService.对帐户对象(在 ApplicationService 项目中)所做的更改需要保留在数据库中.存储库使用 IBankAccount 接口接收更改.如何将这些数据持久化到数据库中?这是使用 LINQ to SQL 实现的.

I have a IBankAccount interface that I will be passing to the ApplicationService. The changes made on the account objects (in the ApplicationService project) need to be persisted in the database. The repository receives the changes using IBankAccount interface. How can I persist this data into database? This is implemented using 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 类是部分类 - 这意味着您可以直接向它们添加接口."

Note: Following is a comment from Scott in http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx "Add the interfaces to your LINQ to SQL data model classes. The LINQ to SQL classes are partial classes - which means you could add the interface directly to them."

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. 优化 Repository 的 SubmitChanges 方法

在使用 LINQ to SQL 时如何抽象出持久性代码?

LINQ to SQL - 映射异常使用抽象基类时

推荐答案

你的存储库应该接受 BankAccount - 而不是 IBankAccount 因为 Linq-to-sql 不知道什么是 IBankAccount 并且编译器不允许您在不首先将其转换为 BankAccount 的情况下存储它(如果 IBankAccount 实例是不是 BankAccount).

Your repository should accept BankAccount - not IBankAccount because Linq-to-sql doesn't know what is IBankAccount and compiler doesn't allow you to store it without casting it first to BankAccount (that can obviously fail at runtime if IBankAccount instance is not a BankAccount).

一旦你有了 BankAccount,你只需调用:

Once you have BankAccount you simply call:

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

这篇关于通过针对接口编程来持久化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆