首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Entity Framework4.0中域模型与数据库模式的区别

Entity Framework4.0中域模型与数据库模式的区别
EN

Stack Overflow用户
提问于 2009-11-07 02:49:55
回答 1查看 431关注 0票数 1

我对Entity Framework非常陌生,正在尝试理解如何以从开发人员的角度来看最有意义的方式来最好地架构我的数据层。我正在使用AventureWorks数据库进行概念验证。我构建了一个通用存储库,用于查询数据库中的任何表。下面是一个简短但可编译(我希望)的代码片段(实际代码有更新、删除等方法):

代码语言:javascript
复制
    /// <summary>
    /// A generic repository for working with data in the database
    /// </summary>
    /// <typeparam name="T">A POCO that represents an Entity Framework entity</typeparam>
    public class DataRepository<T> : IDisposable, IRepository<T> where T : class
    {
        /// <summary>
        /// The context object for the database
        /// </summary>
        private ObjectContext _context;

        /// <summary>
        /// The IObjectSet that represents the current entity. Used to query, add, modify and delete objects of the specified entity.
        /// </summary>
        private IObjectSet<T> _objectSet;

        /// <summary>
        /// Initializes a new instance of the DataRepository class
        /// </summary>
        public DataRepository()
            : this(new AdventureWorksEntities())
        {
        }

        /// <summary>
        /// Initializes a new instance of the DataRepository class
        /// </summary>
        /// <param name="context">The Entity Framework ObjectContext</param>
        public DataRepository(ObjectContext context)
        {
            _context = context;
            _objectSet = _context.CreateObjectSet<T>();
        }

        /// <summary>
        /// Gets all records as an IQueryable
        /// </summary>
        /// <returns>An IQueryable object, containing the results of the query</returns>
        public IQueryable<T> GetQuery()
        {
            return from x in _objectSet select x;
        }

        /// <summary>
        /// Releases all resources used by the WarrantManagement.DataExtract.Dal.ReportDataBase
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases all resources used by the WarrantManagement.DataExtract.Dal.ReportDataBase
        /// </summary>
        /// <param name="disposing">A boolean value indicating whether or not to dispose managed resources</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_context != null)
                {
                    _context.Dispose();
                    _context = null;
                }
            }
        }
    }

在我的ASP.NET演示应用程序中,我可以使用存储库,如下所示,获取员工记录(请注意对联系人的引用):

代码语言:javascript
复制
        using (DataRepository repository = new DataRepository())
        {
            this.GridView1.DataSource = from x in repository.GetQuery()
                                        select new
                                        {
                                            x.MaritalStatus,
                                            x.Contact.FirstName,
                                            x.Contact.LastName,
                                            x.BirthDate
                                        };
            this.GridView1.DataBind();
        }

这可以很好地工作,但我担心的是视图现在具有数据库模式的知识,这是我不喜欢的。例如,如果我决定将FirstName和LastName列移动到Employee表中,则必须重写引用这些列的所有代码行,就像在上面的代码片段中所做的那样。我更喜欢通过创建Employee类的属性来更改概念模型,并在必要时让Entity Framework自动执行联接。

首先,有没有人像我提出的那样设计了一个模型,有什么“经验教训”?有什么负面的东西我应该注意的吗?其次,有没有我应该知道的替代方案?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-11-07 04:46:19

你遇到的部分问题是ASP.NET,让视图知道存储库是很可疑的。例如,在MVC中,将存储库公共类型绑定到视图模型是在控制器中完成的,视图将只知道视图模型。

但即使在直接的ASP.NET中,我也建议创建专用的视图/表示模型,并使用L2E查询将其投影到这些模型上,类似于您现在对匿名类型所做的操作。使用表示模型而不是匿名类型将允许您将查询移出页面,并从页面中移除实体类型的知识。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1689537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档