【问题标题】:MVC 4 late-bound DataContext entity LINQ referenceMVC 4 后期绑定的 DataContext 实体 LINQ 参考
【发布时间】:2012-07-09 14:23:46
【问题描述】:

长时间聆听,第一次来电。

我可能对此采取了错误的方法,所以任何帮助都会很棒。

我正在使用 C#、MVC 4、Code First、Entity Framework 4.3 和 Razor。

我正在尝试将 EF 数据类和一些方法拆分到一个单独的类项目中,以便它们通常可以被多个其他项目引用。我创建了这个简化的示例,以尽可能多地突出问题。

所以在一个课堂项目中我有:

public MyGeneric(string name)
{
    ID = Guid.NewGuid();
    Name = name;
}

public Guid ID { get; set; }
public string Name { get; set; }


public void AddThis(MyGeneric obj)
{
    using (var db = Activator.CreateInstance<myDbContext>())
    {
        var myOthers = from mo in db.MyOther
                       where mo.OtherID == obj.ID
                       select mo;

        myOthers.ForEach(mo => db.MyOther.Add(mo));
        db.SaveChanges();
    }
}

然后我可以在主项目中引用它,例如:

public class SampleInherit : MyGeneric
{
    public SampleInherit(string newName, string secName)
        : base(newName)
    {
        SecName = secName;
    }

    public string SecName { get; set; }


    public void DoSomething(SampleInherit obj)
    {
        base.AddThis(obj);
        // do other stuff
    }
}

我要做的是将一些类及其方法泛化(这是一个词吗?)到一个单独的副项目中。这个附带项目将提供基类(按需要的顺序):

  • 使用 EF DbContext 创建/访问数据库,
  • 允许添加更多属性,
  • 提供影响基础数据的方法/程序,
  • 更适合主项目的重命名。

现在,如果我只是将基类继承到主项目中的类中,那么除了基类引用 myDbContext 和 LINQ 语句中的实体对象引用之外,它都可以工作。

你能后期绑定 DataContext 还是我在这里把自己画到一个角落里?

为您提供的所有帮助干杯。

【问题讨论】:

    标签: c# asp.net-mvc entity-framework linq-to-entities datacontext


    【解决方案1】:

    我认为您要做的是实现 IRepository 模式。基本上你想做的是创建一个包含你的实体和你的 IRepository 的类项目。从那里,您可以通过创建 Repository 类来创建 IRepository 的具体实现。您使用业务逻辑层通过 IRepository 访问存储库。并在那里进行数据访问。

    public interface IRepository<T>
    {
        IQueryable<T> Fetch();
    
        bool Insert(T entity);
    
        bool Update(T entity);
    }
    

    然后创建你的存储库

    public class MyRepository<TContext> : IRepository<EntityType> where TContext: DbContext, new()
    {
        private TContext context;
    
        public MyRepository(TContext context)
        {
            this.context = context;
        }
    
        public IQueryable<T> Fetch()
        {
           return context.EntityTypes;
        }
    
        // Insert and update logic
    }
    

    最后创建你的业务逻辑

    public class BusinessLogic
    {
        private IRepository<EntityType> repository;
    
        public BusinessLogic(IRepository<EntityType> repository)
        {
            this.repository = repository;
        }
    
        public void Add(EntityType obj)
        {
          bool isNew = // check if new
          if(isNew)
          {
            this.repository.Insert (obj)    
          }
          else
          {
            this.repository.Update(obj);
          }
        }
    }
    

    然后调用你的业务逻辑

    IRepository<EntityType> repository = new MyRepository<MyDbContext>(new MyDbContext());
    BusinessLogic service = new BusinessLogic(repository);
    
    service.Add(new EntityType());
    

    我不认为您想要数据上下文的后期绑定,出于性能考虑,最好使用泛型(与上面的示例一样)。同样使用此方法,您可以实例化一个数据上下文并将其传递给您的业务服务,以便实体框架可以跟踪您的实体,而不是为每次调用 AddThis 实例化一个新的数据上下文。加上这个模式,你可以做各种很酷的事情,比如使用像 ninject 这样的 IOC 容器为你创建这些业务服务,并使用 Mocks 为你的代码创建单元测试,而无需访问数据库(通过模拟你的存储库)。

    【讨论】:

    • 不,我指的不是存储库。我想使用 Code First 来创建数据库。后期装订可能不是正确使用的术语。我想继承数据库创建能力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 2015-12-25
    • 1970-01-01
    相关资源
    最近更新 更多