我使用NHibernate设置了存储库模式.基类如下所示:
public interface IUnitOfWork : IDisposable
{
void Commit();
void Rollback();
}
// generic NHibernate implementation of IUnitOfWork here
public class NHibernateRepositoryBase<T> : IRepository<T>
{
private NHibernateUnitOfWork _unitOfWork;
public NHibernateRepositoryBase(NHibernateUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public T Get(object id)
{
return _unitOfWork.Session.Get<T>(id);
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我允许通过构造函数(使用StructureMap)填充工作单元.我正在填充ASP.NET Web服务上的存储库对象,如下所示:
[WebService(Namespace = "...")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ModuleService : System.Web.Services.WebService
{
public IUserAccountRepository UserAccountRepo { get; set; }
public ModuleService()
{
// tell IoC to inject properties
ObjectFactory.BuildUp(this);
}
// …Run Code Online (Sandbox Code Playgroud) 我想我陷入了分析的瘫痪状态.请帮忙!
我目前有一个项目
在我的案例中,工作单元实现一次支持一个NHibernate会话.我当时认为这是有道理的; 它隐藏了ViewModel中NHibernate会话的内部工作方式.
现在,根据Oren Eini(Ayende)的说法:http://msdn.microsoft.com/en-us/magazine/ee819139.aspx
他说服观众在处理与presenter/viewmodel相关联的视图时应创建/处理NHibernate会话.他提出了为什么每个Windows应用程序不需要一个会话,也不希望每个事务创建/处理会话的问题.遗憾的是,这会产生一个问题,因为我的UI可以轻松地在应用程序中显示10个以上的视图/视图模型.他正在使用MVP策略,但他的建议是否转化为MVVM?
这是否意味着我应该废弃工作单元并让viewmodel直接创建NHibernate会话?WPF应用程序一次只能有一个工作会话吗?如果这是真的,我什么时候应该创建/处置NHibernate会话?
而且我还没有考虑过NHibernate无状态会话如何适应这一切!我的大脑会爆炸.请帮忙!
更新:
我在Rhino Tools中找到了Ayende的工作单元实现.我发现他的实施与我所做的实施之间存在显着差异.他绝对支持多次会议.经过进一步的研究,我认为我最好做以下事情:
毕竟,我可以应用每个viewmodel打开一个会话/无状态会话的策略,并且当处理视图时,具有viewmodel flush/dispose会话/无状态会话.
听起来像是个计划?
我已经在Stackoverflow上阅读了很多关于在包含业务层的ASP.Net MVC 3应用程序中使用工作单元模式的帖子.但是,关于这个话题,我仍然有几个问题,非常感谢人们给我的任何反馈.
我正在开发一个使用EF 4.1的ASP.Net MVC 3 Web应用程序.我将使用这个项目的存储库和工作单元模式,类似于在这个伟大的教程中使用它们的方式
我的项目的不同之处在于我还需要包含一个业务层(我的解决方案中的单独项目),以便为应用程序执行各种业务规则.上面提到的教程没有Business层,因此从控制器创建了一个Unit of Work类的实例
public class CourseController : Controller
{
private UnitOfWork unitOfWork = new UnitOfWork();
Run Code Online (Sandbox Code Playgroud)
但是,我的问题是,如果我有业务层,我应该在哪里创建工作单元类的实例?
我个人认为它应该在我的控制器中创建,然后注入到业务层中,如下所示:
public class PeopleController : Controller
{
private readonly IUnitOfWork _UoW;
private IPersonService _personService;
public PeopleController()
{
_UoW = new UnitOfWork();
_personService = new PersonService(_UoW);
}
public PeopleController(IUnitOfWork UoW, IPersonService personService)
{
_UoW = UoW;
_personService = personService;
}
public ActionResult Edit(int id)
{
Person person = _personService.Edit(id);
return View(person);
}
public class UnitOfWork …Run Code Online (Sandbox Code Playgroud) 这里涉及三个实体:Deployment,DeploymentStep和DeploymentStatusLog.我将首先粘贴这些类的相关定义
SRC /我/包/实体/ Deployment.php
<?php
namespace My\Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\PersistentCollection;
/**
* @ORM\Table(name="deployment")
* @ORM\Entity()
*/
class Deployment
{
/**
* Status Log Entries for this deployment
*
* @var \Doctrine\ORM\PersistentCollection
*
* @ORM\OneToMany(targetEntity="DeploymentStatusLog", mappedBy="deployment", cascade={"persist","remove"})
* @ORM\OrderBy({"created_at"="DESC"})
*/
protected $status_logs;
/**
* @var \Doctrine\ORM\PersistentCollection
*
* @ORM\OneToMany(targetEntity="DeploymentStep", mappedBy="deployment", cascade={"persist","remove"})
* @ORM\OrderBy({"sequence" = "ASC"})
*/
protected $steps;
public function __construct()
{
$this->status_logs = new ArrayCollection();
$this->steps = new ArrayCollection(); …Run Code Online (Sandbox Code Playgroud) 我首先使用Entity Framework代码创建MVC Web应用程序项目.在这个项目中,我将使用通用存储库和工作单元模式.另外,我想使用存储过程来获取list by和get-list方法.
如何将存储过程与通用存储库和工作单元模式一起使用?
entity-framework unit-of-work repository-pattern entity-framework-4 ef-code-first
简单的问题.
如何将UnitOfWork与Castle.Windsor,nHibernate和ASP.NET MVC一起使用?
现在为扩展细节.在我理解UnitOfWork模式的过程中,我很难遇到任何使用直接示例的东西Castle.Windsor,特别是在需要安装的方式方面.
到目前为止,这是我的理解.
IUnitOfWork接口用于声明模式UnitOfWork类必须Commit和Rollback交易,以及暴露一个Session.所以说,这是我的IUnitOfWork.(我正在使用Fluent nHibernate)
public interface IUnitOfWork : IDisposable
{
ISession Session { get; private set; }
void Rollback();
void Commit();
}
Run Code Online (Sandbox Code Playgroud)
所以这是我的Castle.WindsorContainer Bootstrapper(ASP.NET MVC)
public class WindsorContainerFactory
{
private static Castle.Windsor.IWindsorContainer container;
private static readonly object SyncObject = new object();
public static Castle.Windsor.IWindsorContainer Current()
{
if (container == null)
{
lock (SyncObject)
{ …Run Code Online (Sandbox Code Playgroud) 我已经按照这个网站上的模式将ninject和nhibernate连接到我的asp.net-mvc3网站.
这是我的global.aspx.cs中的代码:
internal class ServiceModule : NinjectModule
{
public override void Load()
{
var helper = new NHibernateHelper(connectionString);
Bind<ISessionFactory>().ToConstant(helper.SessionFactory)
.InSingletonScope();
Bind<IUnitOfWork>().To<UnitOfWork>()
.InRequestScope();
Bind<ISession>().ToProvider(new SessionProvider())
.InRequestScope();
Bind<IIntKeyedRepository<FAQ>>().To<Repository<FAQ>>()
.InRequestScope();
}
Run Code Online (Sandbox Code Playgroud)
问题是我现在需要在我的控制器中执行Update()和Add();
我有这个作为我的控制器代码:
public FAQController(IIntKeyedRepository<FAQ> faqRepository, IUnitOfWork unitOfWork)
{
_faqRepository = faqRepository;
_unitOfWork = unitOfWork;
}
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
[ValidateInput(false)]
public ActionResult AddFAQ(FAQ contact)
{
var c = new FAQ {Question = contact.Question, Answer = contact.Answer};
_faqRepository.Add(c);
_unitOfWork.Commit();
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
我的主要问题是,在构造函数中传递Iunitofwork感觉不对,因为许多其他操作不需要它.我只是需要它来执行更新和插入我的数据库的操作.由于我在上面的链接上使用ninject IOC,似乎可以说通过IOC传递这个unitofwork对象.
那么,在asp.net-mvc中使用带有IOC的UnitOfWork模式是否有一种更好的更优化方式,它可以为我的控制器中的每个方法调用istransaction.
我按照下面链接的示例来设置统一以使用我的服务层.我的项目设置与本文中的项目非常相似,我理解除了为什么在注册服务依赖项时使用PerThreadLifetimeManager的所有内容.请记住,我也在使用我的服务层中使用的通用存储库和单元工作.大多数统一示例使用默认(瞬态)生命周期管理器,因为我的设置类似于下面的设置,我想知道为什么我应该使用PerThreadLifeimeManager?我正在为我当前的表示层使用ASP.NET Web表单项目,如果它发生了任何变化.
container.RegisterType<ICatalogService, CatalogService>(new PerThreadLifetimeManager())
Run Code Online (Sandbox Code Playgroud)
dependency-injection unity-container unit-of-work object-lifetime repository-pattern
如果我有以下背景:
public partial class HRMainDataCTX : DbContext
{
public HRMainDataCTX()
: base("name=HRMainDataCTX")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
//DbSets
public virtual int SEARCHEMPLOYEE(Nullable<decimal> p_EMP_NUM, string p_EMP_NAME)
{
var p_EMP_NUMParameter = p_EMP_NUM.HasValue ?
new ObjectParameter("P_EMP_NUM", p_EMP_NUM) :
new ObjectParameter("P_EMP_NUM", typeof(decimal));
var p_EMP_NAMEParameter = p_EMP_NAME != null ?
new ObjectParameter("P_EMP_NAME", p_EMP_NAME) :
new ObjectParameter("P_EMP_NAME", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("SEARCHEMPLOYEE", p_EMP_NUMParameter, p_EMP_NAMEParameter);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我实现这样的工作单元:
public class HRCTX : IDisposable
{
private readonly HRMainDataCTX _context;
public HRCTX()
{
_context …Run Code Online (Sandbox Code Playgroud) 我目前正在使用EF 6设置.NET Core应用程序,并且在理解各种依赖项注册方法的适当使用方面遇到了一些麻烦.据我了解:
特别是在我的情况下,我已经设置了一对DbContexts(基于CQRS模式)来处理我正在注册为Scoped的数据库查询/命令:
services.AddScoped((_) => new TestCommandContext(Configuration["Data:TestConnection:ConnectionString"]));
services.AddScoped((_) => new TestQueryContext(Configuration["Data:TestConnection:ConnectionString"]));
Run Code Online (Sandbox Code Playgroud)
这是根据ASP.NET 入门ASP.NET 5和Entity Framework 6文档:
每个范围应解决一次上下文,以确保性能并确保实体框架的可靠运行
我正在注册相应的UOW类:
services.AddTransient<ITestCommandUnit, TestCommandUnit>();
services.AddTransient<ITestQueryUnit, TestQueryUnit>();
Run Code Online (Sandbox Code Playgroud)
我基于这篇文章在这里使用Transient,这表明:
只要在应用程序中需要,就会创建在Transient范围注册的服务.这意味着每次执行(创建依赖项的方法)时,依赖注入框架将创建(注册服务)类的新实例.
基于这种理解,我正在使用Scoped注册我的存储库和服务类:
services.AddScoped<ITestCommandRepository, TestCommandRepository>();
services.AddScoped<ITestQueryRepository, TestQueryRepository>();
services.AddScoped<ITestCommandService, TestCommandService>();
services.AddScoped<ITestQueryService, TestQueryService>();
Run Code Online (Sandbox Code Playgroud)
然后根据需要在我的控制器中调用我各自的服务层方法:
public class TestController : BaseController
{
private ITestQueryService testQueryService;
// Get new object of type TestQueryService via DI
public TestController(ITestQueryService testQueryService)
{
this.testQueryService = testQueryService;
}
[HttpGet]
public IActionResult Edit(int …Run Code Online (Sandbox Code Playgroud) c# entity-framework dependency-injection unit-of-work asp.net-core
unit-of-work ×10
c# ×3
nhibernate ×2
architecture ×1
asp.net ×1
asp.net-core ×1
asp.net-mvc ×1
doctrine-orm ×1
mvvm ×1
ninject ×1
orm ×1
session ×1
symfony ×1
wpf ×1