我正在使用EF 4.1 Code First布局一个新的数据层,从较旧的自制数据层迁移.
我已经设置了两个程序集,一个用于我的上下文,一个用于所有POCO代码的第一个类.
我有一些业务逻辑,例如,对一个表(或几个表)的查询,这些表在几个不同的地方使用. 我应该把它放在哪里?
它不能进入POCO类,因为它连接了几个表,因此需要一个上下文.它可能会进入上下文,但是上下文会因数百个无组织的查询而变得臃肿. 所有业务逻辑都有共同的模式或安排吗?
使用MVC应用程序时,管理Entity Framework上下文的最佳方法是什么?
我正在使用存储库/服务模式.
编辑
在查看其中一些问题之后:stackoverflow.com/users/587920/sam-striano,我之前更加困惑.有人说使用每个存储库的上下文,但是如果我想在一个控制器方法中使用多个存储库呢?
为了遵循良好的分离设计,如何在MVC应用程序中使用UnitOfWork而不依赖于EF?我希望能够使用模拟上下文对我的控制器,模型,服务等进行单元测试?
我想从集成测试开始.我正在使用ASP.NET MVC 3应用程序.我正在使用Entity Framework 4 Code First CTP5.我对数据库的集成测试是在一个单独的项目中,类似于MyProject.Data.IntegrationTests.
我打算使用SQL Server CE 4或SQLite.有关使用其中任何一项的建议/提示/意见,我想要完成的任务?
有谁知道我能读到的关于我想要完成的任何体面的文章?并且将非常感谢帮助/反馈.
我正在使用SQL Server 2008作为我的数据库.但是在测试我的存储库时,我想针对上面提到的其中一个数据库测试它们,所以我需要指定连接字符串.
UPDATE
我从服务层(从我的控制器调用)工作,然后服务层将调用我的存储库.例如,下面是我如何添加新闻项:
服务类:
public class NewsService : INewsService
{
private INewsRepository newsRepository;
public NewsService(INewsRepository newsRepository)
{
this.newsRepository = newsRepository;
}
public News Insert(News news)
{
// Insert news item
News newNews = newsRepository.Insert(news);
// Insert audit entry
// Return the inserted news item's unique identifier
return newNews;
}
}
Run Code Online (Sandbox Code Playgroud)
存储库类:
public class NewsRepository : INewsRepository
{
MyContext context = new MyContext();
public NewsRepository()
{ …Run Code Online (Sandbox Code Playgroud) sql-server sqlite entity-framework sql-server-ce entity-framework-4
只是想知道,在带有实体框架的ASP.NET MVC3环境中。工作单元应该指向服务层还是存储库(然后存储库指向服务层)?
我看过两个例子:
链接:实体框架4 CTP 4 / CTP 5通用存储库模式和可测试的单元
不使用服务层,但显然可以在这种情况下使用。
链接:http://blogs.msdn.com/b/adonet/archive/2009/06/16/using-repository-and-unit- of-work-patterns-with-entity-framework-4-0.aspx
有什么会更好?
谢谢。
我正在尝试如何为ASP.NET MVC3配置StructureMap我已经使用NuGet,我注意到它创建了一个名为StructuremapMVC的cs文件的App_Start文件夹,所以我检查它并注意到它是相同的代码但是简化了将在Global.asax上的App_Start部分手动编写...
这是我在IoC Class中的代码
public static class IoC
{
public static IContainer Initialize()
{
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.AddAllTypesOf<IController>();
});
x.For<OpcionDB>().Use(() => new DatabaseFactory().Get());
});
return ObjectFactory.Container;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么当我在控制器上注入一些IoC时会出现异常(我使用这种模式:实体框架4 CTP 4/CTP 5通用存储库模式和单元可测试):
private readonly IAsambleaRepository _aRep;
private readonly IUnitOfWork _uOw;
public AsambleaController(IAsambleaRepository aRep, IUnitOfWork uOw)
{
_aRep = aRep;
this._uOw = uOw;
}
public ActionResult List(string period)
{
var rs = _aRep.ByPeriodo(period).ToList<Asamblea>();
return View();
}
Run Code Online (Sandbox Code Playgroud)
例外显示:
Exception Details: System.MissingMethodException: No parameterless constructor defined …Run Code Online (Sandbox Code Playgroud) structuremap asp.net-mvc entity-framework code-first asp.net-mvc-3
我想使用可以单元测试的EF 4.1代码第一个模式.我按照本帖的回答中所示的代码示例进行了操作.但现在我收到一条错误,指出"不支持每种类型的多个对象集".我也在使用最新的IoC结构图.我不知道如何解决这个问题.
不支持每种类型的多个对象集.对象集'MemberTypes'和'MemberTypes'都可以包含'SocialSite.Model.Entities.MemberType'类型的实例.
Line 82: public virtual IEnumerable<T> FindAll()
Line 83: {
Line 84: return _dbSet.ToList();
Line 85: }
Line 86:
[InvalidOperationException: Multiple object sets per type are not supported. The object sets 'MemberTypes' and 'MemberTypes' can both contain instances of type 'SocialSite.Model.Entities.MemberType'.]
System.Data.Entity.Internal.DbSetDiscoveryService.RegisterSets(DbModelBuilder modelBuilder) +336
System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder() +393
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +22
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +117
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +407
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +17
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +62
System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +15
System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +40
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +315
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
SocialSite.Data.Repositories.Impl.Repository`1.FindAll() in D:\Visual Studio 2010\SocialSite\SocialSite.Data\Repositories\Impl\Repository.cs:84
SocialSite.Service.Impl.MemberTypesService.GetMemberTypes() in …Run Code Online (Sandbox Code Playgroud) 我真的需要有人帮助我完全理解如何与Entity Framework 4 CTP 5,POCO建立多对多的关系.我需要了解3个概念:
这是我目前的型号:
public class MusicSheet
{
[Key]
public int ID { get; set; }
public string Title { get; set; }
public string Key { get; set; }
public virtual ICollection<Author> Authors { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class Author
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public string Bio { get; set; }
public virtual …Run Code Online (Sandbox Code Playgroud) entity-relationship many-to-many entity-framework poco entity-framework-4
我正在使用一个带有ASP.NET MVC 3应用程序的EF Code First方法,而不是重新创建轮子,我想知道是否已经存在一个可靠的存储库类,我的自定义存储库类可以扩展以便提供开箱即用的默认功能(例如基本CRUD等).
所以像这样......
public class CustomerRepository : BaseRepository { ... }
Run Code Online (Sandbox Code Playgroud)
...因此将提供一种默认的方式来开箱即用的Customer对象.我想在我的MVC控制器中注入一个ICustomerRepository,并在那里提供我的功能.
我确信这样的东西已经存在,因为我已经用NHibernate做了类似的事情.
谢谢
entity-framework dependency-injection ef-code-first asp.net-mvc-3 entity-framework-4.3
asp.net-mvc ×2
.net ×1
code-first ×1
many-to-many ×1
poco ×1
sql-server ×1
sqlite ×1
structuremap ×1