这是我的测试:
[TestMethod]
public void TestUnitOfWork()
{
UnitOfWork unitOfWork = new UnitOfWork();
unitOfWork.ContactRepository.Insert(new Contact
{
Id = Guid.NewGuid(),
FirstName = "Dom",
LastName = "A",
Email = "dominicarchual@yahoo.com"
});
var contacts = unitOfWork.ContactRepository.Get(x => x.FirstName == "Dominic");
Assert.AreEqual(1, contacts.Count());
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
测试方法MvcContacts.Tests.Controllers.HomeControllerTest.TestUnitOfWork引发异常:System.Data.ProviderIncompatibleException:从数据库获取提供程序信息时发生错误.这可能是由实体框架使用不正确的连接字符串引起的.检查内部异常以获取详细信息,并确保连接字符串正确.---> System.Data.ProviderIncompatibleException:提供程序未返回ProviderManifestToken字符串.---> System.Data.SqlClient.SqlException:建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确,以及SQL Server是否配置为允许远程连接.(提供者:SQL网络接口,错误:
我没有设置任何数据库; 即我的上下文看起来像这样:
namespace MvcContacts.DAL
{
public class ContactsContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
我不确切知道如何将其映射到我的数据库; 但是,我想我不必这样做,因为我只是试图使用模拟数据进行测试.我错了吗?
E1:这是我的工作单位.
namespace MvcContacts.DAL
{
public class UnitOfWork : IDisposable
{
private ContactsContext context = …Run Code Online (Sandbox Code Playgroud) 我正在按照本网站上描述的程序设置一个 Unity Dependency Resolver,以便将依赖注入到我的一个控制器中。这段代码工作正常:
var container = new UnityContainer();
container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
Run Code Online (Sandbox Code Playgroud)
上面的代码放在WebApiConfigRegister(HttpConfiguration config)方法中。但是,说明还指定您必须使用以下代码:
config.DependencyResolver = new DependencyResolver();
Run Code Online (Sandbox Code Playgroud)
这就是问题所在:DependencyResolver() 在当前上下文中不存在。我试过搜索这个,我也试过 UnityDependencyResolver() ,它也不存在。我正在使用(或尝试使用)命名空间:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using Microsoft.Owin.Security.OAuth;
using Microsoft.Practices.Unity;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using URLShortener.Models;
using Microsoft.Practices.Unity;
using System.Web.Http.Dependencies;
Run Code Online (Sandbox Code Playgroud)
DependencyResolver 似乎不再存在,这让我感到困惑。谢谢你。
c# dependency-injection unity-container unit-of-work asp.net-web-api
我目前正在尝试实现一个简单的博客应用程序以用于学习目的。它在 DDD 架构内。
我关心的是如何实现通用存储库模式。
你们能否就我是否正确实施存储库提出您的想法。
这是我第一次使用通用存储库,看起来我根本没有使用它。
下面显示了我的用户存储库的实现。
非常感谢
public interface IRepository<TEntity> where TEntity : class
{
TEntity GetById(int id);
IEnumerable<TEntity> GetAll();
IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
void Add(TEntity entity);
void Update(TEntity entity);
void Remove(TEntity entity);
}
//Implementation of Generic Repo
using BA.Infrastructure.Data.Context;
using BA.Infrastructure.Data.Interfaces.Helpers;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
namespace BA.Infrastructure.Data.Repositories.Helpers
{
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
protected readonly BlogDbContext _context;
public Repository(BlogDbContext context)
{
_context = context;
}
public TEntity …Run Code Online (Sandbox Code Playgroud) 我想问在遵循领域驱动设计理念和干净架构的项目中使用存储库工作单元是否有意义,或者它没有更有意义并且通用存储库模式就足够了?
domain-driven-design unit-of-work specification-pattern clean-architecture
关于使用的讨论很多Repository pattern,UoW pattern.我完全理解何时以及为何Repository pattern使用.我只想说我有几个聚合根.下一步是什么?我更喜欢什么模式/实现?使用比普通NHibernate Session更复杂的解决方案的原因是什么?
谢谢!
c# nhibernate design-patterns unit-of-work repository-pattern
我正在使用ObjectContext与其他存储库共享的存储库实现.存储库包含一个ObjectSet实体.我正在通过Add()方法添加新实体ObjectSet.在导入数据时,我想查询这些新添加的对象以防止重复数据.
在ObjectContext实现工作模式的一个单元.在导入过程结束时,我想调用commit方法来调用context.SaveChange()持久化数据.
但是,在我打电话之前,我找不到一种简单的方法来查询新添加的实体SaveChanges().你们怎么处理这些问题?
entity-framework unit-of-work repository-pattern ef-code-first entity-framework-4.1
我正在使用UOW和Repository模式构建一个Web应用程序.我对此有一个基本的了解,我想知道我是否应该为我的项目中的所有表保留一个UOW实现,或者根据功能保留一个单独的实例,例如:
public interface IHomeUOW
{
IGenericRepository<User> Users { get; }
IGenericRepository<TableA> Table_A { get; }
IGenericRepository<TableB> Table_B{ get; }
}
public interface IBusinessCaseUOW
{
IGenericRepository<TableA> Table_A { get; }
IGenericRepository<TableXYZ> Table_XYZ{ get; }
}
Run Code Online (Sandbox Code Playgroud)
如您所见,TableA既可用于Home UOW,也可用于特定的商业案例UOW.一个UOW部分实现如下:
public class UnitOfWork : IUnitOfWork
{
private readonly ObjectContext _context;
private UserRepository _userRepository;
public UnitOfWork(ObjectContext Context)
{
if (Context == null)
{
throw new ArgumentNullException("Context wasn't supplied");
}
_context = Context;
}
public IGenericRepository<User> Users
{
get
{
if (_userRepository == null)
{
_userRepository …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc domain-driven-design unit-of-work repository-pattern
微软教程http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp -net-mvc-application建议实现dispose模式,如下所示:
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
Run Code Online (Sandbox Code Playgroud)
为什么我应该这样做,为什么我不能简单地处理上下文和足够的,如果我只使用会发生什么:
context.Dispose()
Run Code Online (Sandbox Code Playgroud)
实施微软部署模式的目标是什么?
作为学习练习,在尝试使用任何ORM(如EF)之前,我想使用ADO.NET和存储过程构建个人项目.
因为我不希望我的代码随着时间的推移变得混乱,我想使用一些模式,如存储库和UoW模式.
除了交易处理之外,几乎所有事情都已经解决了.
为了某种方式"模拟"一个UoW,我使用了@jgauffin 提供的这个类,但阻止我使用该类的是每次你创建该类的新实例(AdoNetUnitOfWork)时,你自动开始一个事务并且有很多您只需要读取数据的情况.
在这方面,这是我在我读过的一本SQL书中找到的:
在事务中执行SELECT语句可以在引用的表上创建锁定,这可以阻止其他用户或会话执行工作或读取数据
这是AdoNetUnitOfWork班级:
public class AdoNetUnitOfWork : IUnitOfWork
{
public AdoNetUnitOfWork(IDbConnection connection, bool ownsConnection)
{
_connection = connection;
_ownsConnection=ownsConnection;
_transaction = connection.BeginTransaction();
}
public IDbCommand CreateCommand()
{
var command = _connection.CreateCommand();
command.Transaction = _transaction;
return command;
}
public void SaveChanges()
{
if (_transaction == null)
throw new InvalidOperationException("Transaction have already been commited. Check your transaction handling.");
_transaction.Commit();
_transaction = null;
}
public void Dispose()
{
if (_transaction != null) …Run Code Online (Sandbox Code Playgroud) 我有一个Generic Repository像下面的处理我的CRUD,对于一个单一的实体它易于使用,问题开始时,我尝试加入我的POCOs.
假设我有这些POCO,它们使用流畅的api(多对多和一对多关系)进行映射:
public class Student
{
public Student()
{
this.Courses = new HashSet<Course>();
}
public int StudentId { get; set; }
public string StudentName { get; set; }
//FKs
public virtual Standard Standard { get; set; }
public int StdandardRefId { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class Course
{
public Course()
{
this.Students = new HashSet<Student>();
}
public int CourseId { get; set; }
public …Run Code Online (Sandbox Code Playgroud) unit-of-work ×10
c# ×8
asp.net-mvc ×3
ado.net ×1
dispose ×1
nhibernate ×1
sql-server ×1
unit-testing ×1