使用存储库模式时,多个数据库环境 [英] Multiple database contexts when using repository pattern
问题描述
我是有点失落,现在......我从来没有见过关于解决问题的方法这么多不同的信息。但是,让我们从头开始。
I am a bit lost right now... I've never seen this much divergent information regarding solution to the problem. But let us start from the beginning.
我使用ASP.NET MVC与存储库注入控制器,由于Ninject。我有2个简单的实体:管理与创建的博客文章和条目的一个虚拟管理领域的一个列表
I am using ASP.NET MVC with Repositories injected to Controllers, thanks to the Ninject. I have 2 simple Entities: Admin with a list of created blog entries and Entries with one virtual Admin field.
管理
public class Admin
{
[Key, ScaffoldColumn(false)]
public int Id { get; set; }
[Required(ErrorMessage = "Zły login.")]
[StringLength(20), MinLength(3)]
[RegularExpression(@"^[a-zA-Z0-9]*$", ErrorMessage = "Special characters are not allowed.")]
public string Login { get; set; }
[Required(ErrorMessage = "Złe hasło.")]
[StringLength(20, MinimumLength = 3)]
[DataType(DataType.Password)]
[Display(Name = "Hasło")]
public string Password { get; set; }
public virtual List<Entry> CreatedEntries { get; set; } // napisane aktualności przez danego admina
}
条目:
public class Entry
{
[Key, ScaffoldColumn(false)]
public int Id { get; set; }
[StringLength(200, MinimumLength = 2)]
[DataType(DataType.Text)]
[Display(Name = "Tytuł")]
public string Title { get; set; }
[Required, StringLength(2000), MinLength(3)]
[Display(Name = "Treść")]
[UIHint("tinymce_jquery_full"), AllowHtml]
public string Text { get; set; }
public virtual Admin Admin { get; set; }
}
您可能知道往哪里去,因为这个问题是计算器......经典。
You probably know where it is going, since this problem is... "classic" on stackoverflow.
在控制器我想一个对象绑定到另一个:
In the Controller I want to bind one object to another:
entry.Admin = repAdmins.GetAdmin(User.Identity.Name);
repEntries.AddEntry(entry);
在存储库:
public void AddEntry(Entry entry)
{
db.Entries.Add(entry);
db.SaveChanges();
}
当然,我不能这样做,因为出名,这是每一个仓库有不同的数据库环境的结果一个实体对象不能被IEntityChangeTracker的多个实例引用。
Of course I can't do that, because of famous "An entity object cannot be referenced by multiple instances of IEntityChangeTracker", which is a result of having separate database contexts in each repository.
当我在寻找我已经知道可能解决它是使用一个共同的背景下,最好的方式解决。然后我发现单位工作模式。但真正的问题开始的时候这里的。
When I was searching for a solution I already knew that probably the best way to solve it is to use one common context. And then I discovered Unit Of Work pattern. But here's when the real problems starts.
- 在很多网站的解决方案,这是一个有点不同。
- 的库必须有共同的通用接口(我不想用了,因为我并不需要对每个实体的每个CRUD操作,再加上有时我需要有额外的方法,如IfExists等。 )
- 在几个网站,我读过,并不需要这整个抽象,抽象以来已经提供了实体框架和UOW中的DbContext执行(知道是什么意思)
- 工作模式(至少从互联网上的例子)的单位似乎对我来说是真正的痛苦...
我需要一些指导......我学习ASP.NET MVC只待了一年。对我来说,这似乎就像是一个的形式对内容的胜利。因为... 我只是需要的是一个对象绑定到另一个。我开始认为这是更好的时候我只是有一个控制器中的上下文对象,我也没必要建埃菲尔铁塔达到上述提到什么的:\\但是我喜欢知识库的想法......
I need some guidance... I learn ASP.NET MVC for only a year. For me it seems like it's a "triumph of form over content". Because... What I simply need is to bind one object to another. I'm starting to think that it was better when I simply had a context object in the Controller and I didn't need to build Eiffel Tower to achieve what's mentioned above :\ However I like idea of repositories...
对不起,我的英语...
Sorry for my English...
推荐答案
下面的示例演示如何使用多个系统信息库中相同的上下文。为了简化它,我没有使用接口和我也没有使用容器注入的依赖关系。
The following example shows how to use the same context within multiple repositories. To simplify it, I did not use interfaces and nor did I use a container to inject dependencies.
Controller类:
Controller class:
public class HomeController : Controller
{
Context context;
AdminRepository adminRepository;
EntryRepository entryRepository;
public HomeController()
{
context = new Context();
adminRepository = new AdminRepository(context);
entryRepository = new EntryRepository(context);
}
// GET: Home
public ActionResult Index()
{
string login = "MyLogin";
Admin admin = adminRepository.GetAdmin(login);
Entry entry = new Entry() { Admin = admin};
entryRepository.AddEntry(entry);
return View(entry);
}
}
库:
public class AdminRepository
{
Context context;
public AdminRepository(Context context)
{
this.context = context;
// This seeds the database
Admin admin = new Admin() { Login = "MyLogin" };
this.context.Admins.Add(admin);
this.context.SaveChanges();
}
public Admin GetAdmin(string login)
{
return context.Admins.Where(a => a.Login == login).FirstOrDefault();
}
}
public class EntryRepository
{
Context context;
public EntryRepository(Context context)
{
this.context = context;
}
public void AddEntry(Entry entry){
context.Entrys.Add(entry);
context.SaveChanges();
}
}
Context类:
Context class:
public class Context : DbContext
{
public Context()
{
Database.SetInitializer<Context>(new DropCreateDatabaseAlways<Context>());
Database.Initialize(true);
}
public DbSet<Admin> Admins { get; set; }
public DbSet<Entry> Entrys { get; set; }
}
改款车型:
public class Admin
{
public int Id { get; set; }
public string Login { get; set; }
}
public class Entry
{
public int Id { get; set; }
public virtual Admin Admin { get; set; }
}
这篇关于使用存储库模式时,多个数据库环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!