我正在一個asp.net mvc web應用程序上工作。現在我已經創建了多個庫類,例如,我有以下的抽象庫類: -坐在我的存儲庫類和控制器類之間使用相同的上下文對象
public interface ISkillRepository : IDisposable
{//code goes here..
&
public interface IStaffRepository : IDisposable
{//code goes here
和模型庫: -
public class SkillRepository : ISkillRepository , IDisposable
{
private SkillManagementEntities context = new SkillManagementEntities();
//code goes here
&
public class StaffRepository : IStaffRepository , IDisposable
{
private SkillManagementEntities context = new SkillManagementEntities();
現在裏面Ÿ控制器我正在intializing和創建回購如下: -
public class SkillController : Controller
{
private ISkillRepository skillRepository;
public SkillController() : this(new SkillRepository()) {}
public SkillController(ISkillRepository repository)
{
skillRepository = repository;
}
,但目前我得到了我的應用程序中出現以下錯誤:
The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.
,問題是,我需要通過回購和控制器傳遞相同的上下文。因此,誰能書於在此: -
我怎麼可以在一個模型內回購參考使用相同的上下文類另一個回購。例如在Staff倉庫內部參考技能庫?我可以在一個控制器類中引用多個回購,但同時在它們之間傳遞相同的上下文對象,所以如果我發出一個save()它將包裝一個事務內的所有語句。例如insie我skillController引用技能&員工回購使用相同的上下文對象?
感謝
編輯 我創建工作類的以下單位: -
public class UnitOfWork : IDisposable
{
private SkillManagementEntities context = new SkillManagementEntities();
private SkillRepository skillRepository;
private StaffRepository staffRepository;
private SecurityRoleRepository securityroleRepository;
public SkillRepository SkillRepository
{
get
{
if (this.skillRepository == null)
{
this.skillRepository = new SkillRepository(context);
}
return skillRepository;
}
}
public StaffRepository StaffRepository
{
get
{
if (this.staffRepository == null)
{
this.staffRepository = new StaffRepository(context);
}
return staffRepository;
}
}
public SecurityRoleRepository SecurityRoleRepository
{
get
{
if (this.staffRepository == null)
{
this.staffRepository = new SecurityRoleRepository(context);
}
return securityroleRepository;
}
}
public async Task Save()
{
await context.SaveChangesAsync();
}
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);
}
}
}
,然後我的回購裏面我做了以下內容: -
public class SecurityRoleRepository : ISecurityRoleRepository , IDisposable
{
private SkillManagementEntities context;// = new SkillManagementEntities();
public SecurityRoleRepository(SkillManagementEntities context)
{
this.context = context;
和控制器類我會參考ncing的的UnitOfWork如下: -
public class SecurityRoleController : Controller
{
private UnitOfWork unitOfWork = new UnitOfWork();
public async Task<ActionResult> Index(string filter = null, int page = 1, int pageSize = 20, string sort = "Name", string sortdir = "ASC")
{
try
{
var records = new PagedList<SecurityRole>();
ViewBag.filter = filter;
records.Content = await unitOfWork.SecurityRoleRepository.GetSecurityRoleForGrid(filter, page, pageSize, sort, sortdir).ToListAsync();
現在我面臨的一個問題是,我怎麼能referecne從另一個回購回購?例如我可以參考SecurityRole回購庫中的技能回購?
編輯決賽 我做了如下步驟: - 1.我安裝
Install-Package Ninject.MVC5
2。然後,我創建了以下相關性類: - :
public class SkillRepository : ISkillRepository , IDisposable
{
private SkillManagementEntities context ;
private IStaffRepository staffrepo = (IStaffRepository)DependencyResolver.Current.GetService(typeof(IStaffRepository));
public SkillRepository(SkillManagementEntities context)
{
this.context = context;
}
最後我的操作方法中,我將調用 -
public class YourDependencyResolverClass : IDependencyResolver
{
private IKernel kernel;
public YourDependencyResolverClass()
{
kernel = new StandardKernel();
AddBindings();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
private void AddBindings()
{
kernel.Bind<ISkillRepository>().To<SkillRepository>();
kernel.Bind<IStaffRepository>().To<StaffRepository>();
kernel.Bind<ISecurityRoleRepository>().To<SecurityRoleRepository>();
kernel.Bind<ICustomerRepository>().To<CustomerRepository>();
kernel.Bind<ISkillVersionHistoryRepository>().To<SkillVersionHistoryRepository>();
}
}
}
3.now我SkillRepository類,我將引用StaffRepository如下內Uiteofwork類如下: -
public class StaffController : Controller
{
//private SkillManagementEntities db = new SkillManagementEntities();
UnitOfWork unitofwork = new UnitOfWork();
public async Task<ActionResult> AutoComplete(string term)
{
var staff = await unitofwork.StaffRepository.GetAllActiveStaff(term).Select(a => new { label = a.SamAccUserName }).ToListAsync();
return Json(staff, JsonRequestBehavior.AllowGet);
和工作類的團結是: -
public class UnitOfWork : IDisposable
{
private SkillManagementEntities context = new SkillManagementEntities();
private SkillRepository skillRepository;
private StaffRepository staffRepository;
private SecurityRoleRepository securityroleRepository;
private CustomerRepository customerRepository;
private SkillVersionHistoryRepository SVH;
public SkillRepository SkillRepository
{
get
{
if (this.skillRepository == null)
{
this.skillRepository = new SkillRepository(context);
}
return skillRepository;
}
}
public StaffRepository StaffRepository
{
get
{
if (this.staffRepository == null)
{
this.staffRepository = new StaffRepository(context);
}
return staffRepository;
}
}
public CustomerRepository CustomerRepository
{
get
{
if (this.customerRepository == null)
{
this.customerRepository = new CustomerRepository(context);
}
return customerRepository;
}
}
public SecurityRoleRepository SecurityRoleRepository
{
get
{
if (this.securityroleRepository == null)
{
this.securityroleRepository = new SecurityRoleRepository(context);
}
return securityroleRepository;
}
}
public SkillVersionHistoryRepository SkillVersionHistoryRepository
{
get
{
if (this.SVH == null)
{
this.SVH = new SkillVersionHistoryRepository(context);
}
return SVH;
}
}
public async Task Save()
{
await context.SaveChangesAsync();
}
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);
}
}
所以,如果我使用unitefwork和DI的方法可以保證我所有的語句都會在單個數據庫事務中發生變形,那麼你能否提出一些建議? thnaks?
調查依賴注入。任何好的DI容器都應該允許您在請求的生命週期中將類型綁定到實例。我個人使用Ninject,並發現它非常易於使用。但是,有很多選擇;找到最適合你的一個。 – 2015-02-05 20:52:45
@ChrisPratt感謝您的回覆,但我可以使用諸如http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing -the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application在我的情況下? – 2015-02-05 22:40:05
不知道這個全新的問題在哪裏出現問題 - 您在一個本地化的地方創建對象 - 顯然您應該能夠根據需要傳遞對構造函數的引用;或者如果您有循環依賴性,則可以使用「Func」。 ..(我仍然聲稱DI框架是解決/注入依賴關係的更方便的方式 - 但是對於每個特定情況,手動執行都不是太難)。 –
2015-02-06 00:08:17