Tag: repository pattern

entity frameworkRC1 DbContext查询问题

我正在尝试使用entity framework代码首先实现存储库模式rc 1.我遇到的问题是创建DbContext。 我有一个解决IRepository的ioc容器,它有一个contextprovider,它只是在windsor.config文件中用连接字符串通知新的DbContext。 使用linq2sql这部分没问题,但EF似乎很窒息。 我将通过一个例子来描述下面的问题。 我已经拿出代码来简化一些事情,这就是为什么你在这里看不到任何存储库模式的东西。 只是在没有所有额外代码和类的情况下排序正在发生的事情。 using (var context = new PlssContext()) { var x = context.Set(); var y = x.Where(u => u.UserName == LogOnModel.UserName).FirstOrDefault(); } using (var context2 = new DbContext(@”Data Source=.\SQLEXPRESS;Initial Catalog=PLSS.Models.PlssContext;Integrated Security=True;MultipleActiveResultSets=True”)) { var x = context2.Set(); var y = x.Where(u => u.UserName == LogOnModel.UserName).FirstOrDefault(); } PlssContext是我创建DbContext类的地方。 存储库模式对PlssContext一无所知。 我认为我能做的最好的事情就是使用连接字符串创建一个DbContext到sqlexpress数据库并以这种方式查询数据。 在新建了PlssContext对象之后,从上下文中获取了var […]

如何用存储库模式问题解决这个generics?

我从上一个问题得到了这个代码,但它没有编译: public interface IEntity { // Common to all Data Objects } public interface ICustomer : IEntity { // Specific data for a customer } public interface IRepository : IDisposable where T : IEntity { T Get(TID key); IList GetAll(); void Save (T entity); T Update (T entity); // Common data will be added here […]

“会议已结束!” – NHibernate

这是在Web应用程序环境中: 初始请求能够成功完成,但是任何其他请求都会从NHibernate框架返回“Session is Closed”响应。 我正在使用HttpModule方法,代码如下: public class MyHttpModule : IHttpModule { public void Init(HttpApplication context) { context.EndRequest += ApplicationEndRequest; context.BeginRequest += ApplicationBeginRequest; } public void ApplicationBeginRequest(object sender, EventArgs e) { CurrentSessionContext.Bind(SessionFactory.Instance.OpenSession()); } public void ApplicationEndRequest(object sender, EventArgs e) { ISession currentSession = CurrentSessionContext.Unbind( SessionFactory.Instance); currentSession.Dispose(); } public void Dispose() { } } SessionFactory.Instance是我的单例实现,使用FluentNHibernate返回一个ISessionFactory对象。 在我的存储库类中,我尝试使用以下语法: public […]

存储库模式中的共享存储库

存储库依赖? 假设我有一个类似于以下内容的域: public class User { public int UserId { get; set; } public Lazy Posts { get; set; } } public class Post { public int PostId { get; set; } public User Poster { get; set; } } 有我post的用户。 如何设置我的存储库,以便为每个user返回一个Post列表,对于每个post ,我都会收到Poster ? 我有2个存储库: public class UserRepository : IUserRepository { public IQueryable GetUsers(); } […]

generics和entity framework:如何根据列值返回不同的类型

我们有一个人员表,存储不同类型的人(买方,卖方,代理等)。 我们的ORM是entity frameworkCodeFirst(CTP5)。 我们正在使用存储库模式进行良好的TDD和模拟。 在PersonRepository中我想返回一个特定的类型,所以我可以做这样的事情: Agent a = repository.Get(5005); // Where 5005 is just an example Id for the person a.SomeAgentProperty = someValue; Buyer b = repository.Get(253); // Again, a simple PersonId. b.SomeBuyerProperty = someOtherValue; 我的想法是,当我从存储库中获取它时,我知道我会得到什么样的人。 而且,是的,我可以创建X个不同的Get方法,称为GetBuyer(int PersonId),GetSeller(int PersonId)等等。 但那有代码味道。 generics函数看起来如何? 到目前为止,这是我的存储库界面: public interface IPersonRepository { Person Get(int PersonId); // To get a generic person […]

存储库模式仅检索所需的列

我在这里使用示例来创建一个包含工作单元的存储库模式。 在代码中,有一个通用的Get方法: public class GenericRepository where TEntity : class { internal AdminDbContext context; internal DbSet dbSet; public GenericRepository(AdminDbContext context) { this.context = context; this.dbSet = context.Set(); } public virtual IEnumerable Get( Expression<Func> filter = null, Func selector = null, Func<IQueryable, IOrderedQueryable> orderBy = null, string includeProperties = “”) { IQueryable query = dbSet; if (filter […]

使用存储库模式实现WCF数据服务

我们在ASP.NET MVC 3应用程序中使用存储库模式。 这意味着,尽管我们使用EF 4.1 Code First来访问后端中的数据,但所有MVC控制器都是通过通用存储库类而不是直接通过DbContext子类来实现的。 简化的代码段: public class MyEntityContext : DbContext, IMyEntityContext { public IDbSet MyEntities { get; set; } … } public class MyEntityRepository : IMyEntityRepository { private IMyEntityContext _context; public IQueryable MyEntities { return _context.MyEntities; } … } public class MyEntityController : Controller { private MyEntityRepository _repository; … } 我们为每个依赖项使用接口和dependency injection。 […]

UOW + Repository + Autofac加载两个不同的DbContext

我今天面临一个问题,我无法解决,我搜索了很多,无法解决问题,请尽可能帮助我。 我正在实现一个MVC应用程序,该应用程序使用EF +存储库模式+工作单元和Autofac作为dependency injection器。 我能够使用一个DbContext类,但我面临的情况是我需要使用另一个DbContext实例(使用其他用户凭据访问另一个数据库) 让我更好地解释一下:我有来自数据库A的EntityA(并且有一个DatabaseA_Context类)。 所以我需要一个EntityB,它来自数据库B(带有自己的DatabaseB_Context类)。 当我使用AutoFac注册它们时,只会在GenericRepository实现上注入最后配置的依赖项。 我已经发现文章说Autofac用最后一个值覆盖了注册。 我已经发现其他文章显示我是否在UnitOfWork构造函数上传递了IEnumerable,我能够看到它的所有注册类型,但我想要一个特定的类型。 我清楚了吗? 我的代码如下: 我的控制器: public class MyController : Controller { private readonly IBaseBLL _aBLL; private readonly IBaseBLL _bBll; public MyController(IBaseBLL aBLL, IBaseBLL bBLL) { _aBLL = aBLL; _bBLL = bBLL; } } 我的业务层 public interface IBaseBLL where T : class { T Select(Expression<Func> predicate); T AddT […]

如何设计Repository模式以便以后轻松切换到另一个ORM?

我是存储库模式的新手,但我尝试过,我的目标是制作一个设计,只需一些编辑“dependency injection或配置编辑”就可以轻松地切换到另一个ORM,而无需触及其他解决方案层。 我达到了这个实现: 这是代码: public interface IRepository { T Get(int key); IQueryable GetAll(); void Save(T entity); T Update(T entity); // Common data will be added here } public interface ICustomerRepository : IRepository { // Specific operations for the customers repository } public class CustomerRepository : ICustomerRepository { #region ICustomerRepository Members public IQueryable GetAll() { DataClasses1DataContext […]

entity framework的存储库+ UnitOfWork模式

我上下搜索网络,我没有找到适合我的应用程序的设计。 我正在寻找Repository + UnitOfWork模式,它将管理连接并在完成后自动处理它们。 我需要支持Web应用程序,其中每个请求都有自己的UnitOfWork和Windows应用程序,其中每个线程都有自己的UnitOfWork。 我需要模式自动处理UnitOfWork请求/线程完成。 我也想在exception的情况下支持rolback。 现在我使用StructureMap所以我不想继续在建议答案中使用它。 我需要Repository模式的原因是为了实现我所有实体所需的所有能力。 我需要UnitOfWork的原因是允许更多的实体更改。 我真的很乐意帮助你。 谢谢。