使用 linq 的存储库模式

2023-12-27

当我使用两个不同的生成的 linq 代码时,如何实现 Rob Conery 在 [MVC Storefront][1] 中显示的所谓“存储库模式”?我是否需要像 Fredrik Normen 所讨论的那样实现真正的存储库模式存储库模式的目的是什么? http://weblogs.asp.net/fredriknormen/archive/2008/04/24/what-purpose-does-the-repository-pattern-have.aspx?问题是我想传递 LINQ 从我的“存储库”提供的一些不错的功能,以便我以后可以使用它,所以如果不需要,我不想实现 Fredrik 讨论的真正的存储库模式。

现在我的问题。我已经下载了 [dblinq ][3],它是 MySql、Oracle 和 PostgreSQL 的 Linq 提供程序。在我的项目中,我现在已经为 MySQL 和 SQL(microsoft) 生成了 LINQ 代码。问题是它们都需要生成名称相同但内容有所不同的类。我可以使用某种方式通过命名空间或其他东西来实现这一点,以便我可以同时使用它们,就像“存储库模式”的想法一样? 语言表示例 斯Ql

[Table(Name="dbo.Language")]
public partial class Language : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _Id;

    private string _Name;

    private EntitySet<Book> _Books;

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnIdChanging(int value);
partial void OnIdChanged();
partial void OnNameChanging(string value);
partial void OnNameChanged();
#endregion

    public Language()
    {
        this._Books = new EntitySet<Book>(new Action<Book>(this.attach_Books), new Action<Book>(this.detach_Books));
        OnCreated();
    }

    [Column(Storage="_Id", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
    public int Id
    {
        get
        {
            return this._Id;
        }
        set
        {
            if ((this._Id != value))
            {
                this.OnIdChanging(value);
                this.SendPropertyChanging();
                this._Id = value;
                this.SendPropertyChanged("Id");
                this.OnIdChanged();
            }
        }
    }

    [Column(Storage="_Name", DbType="NVarChar(100) NOT NULL", CanBeNull=false)]
    public string Name
    {
        get
        {
            return this._Name;
        }
        set
        {
            if ((this._Name != value))
            {
                this.OnNameChanging(value);
                this.SendPropertyChanging();
                this._Name = value;
                this.SendPropertyChanged("Name");
                this.OnNameChanged();
            }
        }
    }

    [Association(Name="Language_Book", Storage="_Books", ThisKey="Id", OtherKey="Language")]
    public EntitySet<Book> Books
    {
        get
        {
            return this._Books;
        }
        set
        {
            this._Books.Assign(value);
        }
    }

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    {
        if ((this.PropertyChanging != null))
        {
            this.PropertyChanging(this, emptyChangingEventArgs);
        }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private void attach_Books(Book entity)
    {
        this.SendPropertyChanging();
        entity.Language1 = this;
    }

    private void detach_Books(Book entity)
    {
        this.SendPropertyChanging();
        entity.Language1 = null;
    }
}

MySQL

    [Table(Name = "asp.Language")]
public partial class Language : INotifyPropertyChanged
{
    #region INotifyPropertyChanged handling

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion

    #region int ID

    private int _id;
    [DebuggerNonUserCode]
    [Column(Storage = "_id", Name = "Id", DbType = "int", IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false)]
    public int ID
    {
        get
        {
            return _id;
        }
        set
        {
            if (value != _id)
            {
                _id = value;
                OnPropertyChanged("ID");
            }
        }
    }

    #endregion

    #region string Name

    private string _name;
    [DebuggerNonUserCode]
    [Column(Storage = "_name", Name = "Name", DbType = "varchar(100)", CanBeNull = false)]
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (value != _name)
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    #endregion

    #region Children

    private EntitySet<Book> _book;
    [Association(Storage = "_book", OtherKey = "Language", ThisKey = "ID", Name = "Book_ibfk_1")]
    [DebuggerNonUserCode]
    public EntitySet<Book> Book
    {
        get
        {
            return _book;
        }
        set
        {
            _book = value;
        }
    }


    #endregion

    #region Attachement handlers

    private void Book_Attach(Book entity)
    {
        entity.LanguageLanguage = this;
    }

    private void Book_Detach(Book entity)
    {
        entity.LanguageLanguage = null;
    }


    #endregion

    #region ctor

    public Language()
    {
        _book = new EntitySet<Book>(Book_Attach, Book_Detach);
    }

    #endregion

}

理想情况下,您只需创建每个类一次,并且 ORM 应该处理数据库差异(这就是 NHibernate 和 Subsonic 的工作方式)。

如果您确实需要为每个数据库使用不同的类,那么是的,你可以拥有同名的类,只要它们位于不同的命名空间中。如果您自己编写类的代码,这非常简单:

// C# sample
namespace MyCompany.MyApp.Entities.Oracle
{
    public class MyClass
    {
    // ...
    }
}

namespace MyCompany.MyApp.Entities.SqlServer
{
    public class MyClass
    {
    // ...
    }
}

如果类是自动生成的,您应该在代码生成工具中检查如何指定名称空间。例如,使用 LINQ to SQL 设计器,您可以在表/类属性中指定命名空间,或者通过自行编辑 DBML 文件来指定命名空间。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 linq 的存储库模式 的相关文章

随机推荐