通过编程对接口保留数据 [英] Persist Data by Programming Against Interface

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

问题描述

我有一个IBankAccount界面,我将传递到ApplicationService。在帐户对象(在ApplicationService项目)方面的变化需要在数据库中被持久。这个仓库接收使用IBankAccount界面的变化。我怎样才能坚持这个数据到数据库?这是使用LINQ to SQL实现的。

请注意:下面是斯科特<一个评论href=\"http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx\" rel=\"nofollow\">http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx
接口添加到您的LINQ to SQL数据模型类的LINQ to SQL类是部分类。 - 这意味着你可以直接添加界面,他们

 公共类LijosSimpleBankRepository:ILijosBankRepository
{
    公共System.Data.Linq.DataContext上下文
    {
        得到;
        组;
    }    公共虚拟无效UpdateAccount(DomainInterfaces.IBankAccount iBankAcc)
    {
        DBML_Project.BankAccount的BankAccount;
    }}命名空间DomainInterfaces
{
公共接口IBankAccount
{
    INT BankAccountID {搞定;组; }
    字符串ACCOUNTTYPE {搞定;组; }
    System.Nullable&LT;&System.DateTime的GT; OpenedDate {搞定;组; }
    字符串状态{搞定;组; }
    System.Nullable&LT; INT&GT; AccountOwnerID {搞定;组; }
}}命名空间DBML_Project
{
公共类FixedBankAccount:的BankAccount
{
    //注意:已经的BankAccount implemnts IBankAccount
}公共类SavingsBankAccount:的BankAccount
{
    //注意:已经的BankAccount implemnts IBankAccount
}//自动生成的CALSS由抽象
[全球:: System.Data.Linq.Mapping.TableAttribute(NAME =dbo.BankAccount)]
[InheritanceMapping(code =固定,类型= ty​​peof运算(FixedBankAccount),ISDEFAULT = TRUE)]
[InheritanceMapping(code =储蓄,类型= ty​​peof运算(SavingsBankAccount))]
公共抽象部分类的BankAccount:INotifyPropertyChanging,INotifyPropertyChanged的,DomainInterfaces.IBankAccount
{
      ..
    }
}

阅读


  1. <一个href=\"http://stackoverflow.com/questions/11262785/optimizing-repositorys-submitchanges-method\">Optimizing信息库的方法的SubmitChanges


  2. <一个href=\"http://stackoverflow.com/questions/875130/how-do-you-abstract-out-your-persistence-$c$c-when-using-linq-to-sql/875314#comment14855058_875314\">How你抽象出你的持久code。使用LINQ时,SQL?


  3. <一个href=\"http://stackoverflow.com/questions/1021274/linq-to-sql-mapping-exception-when-using-abstract-base-classes\">LINQ使用抽象基类时映射例外

  4. - 到SQL

解决方案

您库应该接受的BankAccount - 不是 IBankAccount 因为LINQ到SQL不知道什么是 IBankAccount 和编译器不允许将其存储不先浇铸成的BankAccount (可以在运行时明显失败,如果 IBankAccount 实例不是的BankAccount )。

一旦你有了的BankAccount 您只需拨打:

  Context.BankAccounts.Add(账户);
Context.SubmitChanges();

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.

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
{
      ..
    }
}

READING

  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

解决方案

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).

Once you have BankAccount you simply call:

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

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

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