English 中文(简体)
查阅在法典中储存的程序生成了DbContext与实体框架4.1和DDD。
原标题:Accessing stored procedures on a code generated DbContext with Entity Framework 4.1 with DDD

Im利用ASP开展大型项目。 Net MVC 3, EF 4.1 and Ninject for Dependecy Injection. 我先从这里现有的许多有关DD、EF和托存模式的问题读到,但我看看看看,任何人都把储存的程序纳入这些模式。

我不喜欢把另一个存放模式放在似乎已经由“DbContext”定义的“团结”/托存方案”之上。 此外,我一般不喜欢为系统中的每一类实体设立服务和托存班的想法。

The source of my problem stems from this common repository interface which everyone seems to use.

public interface IRepository<TEntity> where TEntity : class
{
    TEntity Get(Expression<Func<TEntity, bool>> whereClause);

    IEnumerable<TEntity> List();
    IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> whereClause);

    void Add(TEntity entity);
    void Delete(TEntity entity);

    // And so on...
}

如果你的所有询问都能在一个单一实体中提出的话,那是巨大的。 在我休息的时候,我想利用一个储存的程序。 有了EF 4.1 & 《普通法典》可以增加所储存的程序(例如选择User),这将产生这样的背景。

namespace MyCompany.Data.Database
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Objects;

    using MyCompany.Domain.Entities;
    using MyCompany.Domain.Contracts;

    public partial class MyCompanyEntities : DbContext
    {
        public MyCompanyEntities()
            : base("name=MyCompanyEntities")
        {
            this.Configuration.LazyLoadingEnabled = false;
            this.Configuration.ProxyCreationEnabled = false;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Order> Orders { get; set; }
        public DbSet<User> Users { get; set; }

        public virtual ObjectResult<User> SelectUser(Nullable<int> userId)
        {
            ((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace.LoadFromAssembly(typeof(User).Assembly);

            var userIdParameter = userId.HasValue ?
                new ObjectParameter("UserId", userId) :
                new ObjectParameter("UserId", typeof(int)); MyCompanyEntities x; x.

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<User>("SelectUser", userIdParameter);
        }

        public virtual ObjectResult<User> SelectUser(Nullable<int> userId, MergeOption mergeOption)
        {
            ((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace.LoadFromAssembly(typeof(User).Assembly);

            var userIdParameter = userId.HasValue ?
                new ObjectParameter("UserId", userId) :
                new ObjectParameter("UserId", typeof(int));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<User>("SelectUser", mergeOption, userIdParameter);
        }
    }
}

作为我的DDD设置的一部分,我有一个用户服务班,我谨向建筑商注入一个存放处。 很多例子表明,施工者应当接受(IRepository<User>userReito)。 这对我来说是徒劳的。 DbContext类作为方法生成了储存程序,我无法看到。

我认为的唯一事情是,要么与所储存的程序方法建立另一个接口。 我真的不想在一般的保存人中添加这个词,因为当时,你有保存和保存的事例;Order>你仍然看到选择性的User,似乎有点奇怪。 这难道不是大事吗?

也许我会这样错了。 如果我不试图创造全新的储存模式,我是否应该同时在我的DbContext上建立一个接口? 我确实为依赖性注射创造了条件。 如果用户服务施工者采用MyCompanyEntities,而不是接口,这是否是错误的?

最佳回答

你发现的是自然的。 问题是,一般存放处不足以应付实际情况。 仅仅有利于“基地”的实施。 你们需要一个用户实体的具体存放处,该存放处将暴露在所储存程序的环境下的收集方法。

问题回答

暂无回答




相关问题
DDD - Returning entity in response to a service operation

I am new to domain driven development & have a simple question. If a service needs to generate some entity as a response to an operation then how should it be done? One of the ways is to inject ...

Domain Driven Design efforts in dynamic languages? [closed]

Are you aware of any DDD efforts in a dynamic language ? Practical resources on DDD tend to decrease quite dramatically when straying from enterprise-oriented solutions (a google search exluding C#, ....

Accessing domain objects in the view

If I don t want to expose the internal state of my Domain Objects, but I need to display them, I can think of three approaches. Which of these is the most "correct" (if any?). The "DTO/ViewModel ...

DDD screen cast question?

I wathced a screen cast on DDD by Greg Young the other day which spoke about persisting all state transitions of an object, instead of it s state when saved, then to load it "replay" all these ...

How to fluent-map this (using fluent nhibernate)?

I have two tables in my database "Styles" and "BannedStyles". They have a reference via the ItemNo. Now styles can be banned per store. So if style x is banned at store Y then its very possible that ...

热门标签