我希望我的界面不会独占调用DbSet。我试图将它们变成ICollection和IQueryable ......但是使用IQueryable我无法调用“添加”方法,例如_db.Posts.Add(post)。使用ICollection每当我调用Add方法时,它都会跳转到get语句并返回一个列表,而不是将其作为ORM指令的一部分添加进行插入查询。
有什么想法吗?我迷路了
我的控制器
private readonly IBlogDb _db;
public PostsController(IBlogDb db)
{
_db = db;
}
public ActionResult Update(int? id, string title, string body, DateTime date, string tags)
{
if (!IsAdmin)
{
RedirectToAction("Index");
}
if (ModelState.IsValid)
{
Post post = GetPost(id);
post.Title = title;
post.Body = body;
post.Date = date;
post.Tags.Clear();
tags = tags ?? string.Empty;
string[] tagNames = tags.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string tagName in tagNames)
{
post.Tags.Add(GetTag(tagName));
}
if (!id.HasValue || id == 0)
{
_db.Posts.Add(post);
_db.Save();
}
_db.Save();
return RedirectToAction("Details", new { id = post.Id });
}
return View();
}
然后我们有了界面。我不明白为什么我需要在这里调用DbSet。我想把它作为收藏品吗?看起来像我在这回应中提到的那样混乱:
ASP.NET MVC4: IQueryable does not contain a definition for 'Add'
public interface IBlogDb
{
DbSet<Post> Posts { get; }
DbSet<Tag> Tags { get; }
void Save();
}
最后是DbContext类
public class BlogDb : DbContext, IBlogDb
{
public BlogDb()
: base("DefaultConnection")
{
}
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; }
void IBlogDb.Save()
{
SaveChanges();
}
public DbSet<Comment> Comments { get; set; }
public DbSet<Administrator> Administrators { get; set; }
//Implementing IBlogDb
}
答案 0 :(得分:1)
如果您接受显式实现,并且不介意在实现中添加一些额外的代码行,则可以从界面轻松删除DbSet:
public interface IMyDbContext
{
ICollection<Customer> Customers { get; }
}
public class MyDbContext : IMyContext
{
ICollection<Customer> IMyContext.Customers { get { return (ICollection<Customer>)Customers; } }
public DbSet<Customer> Customers { get; set; }
}
编辑:我发布了这个答案,然后重新阅读了问题,然后将其删除,现在又回来了。这将为您提供从集合中添加/删除所需的内容。如果您想要一个额外的IQueryable,您可以使用另一个接口和显式实现对其进行分层。但是,这很难看。
你真正想要的是使用IDbSet并创建一个类,例如DbSet的替换
public class MemoryDbSet<TEntity> : DbSet<TEntity>, IQueryable,
IEnumerable<TEntity>, IDbAsyncEnumerable<TEntity>
where TEntity : class
{
private readonly Func<TEntity, object[], bool> _findSelector;
private readonly ObservableCollection<TEntity> _data;
private readonly IQueryable _query;
public MemoryDbSet(Func<TEntity, object[], bool> findSelector)
{
_findSelector = findSelector;
_data = new ObservableCollection<TEntity>();
_query = _data.AsQueryable();
}
public override TEntity Find(params object[] keyValues)
{
return _data.SingleOrDefault(item => _findSelector(item, keyValues));
}
public override TEntity Add(TEntity item)
{
_data.Add(item);
return item;
}
public override TEntity Remove(TEntity item)
{
_data.Remove(item);
return item;
}
public override TEntity Attach(TEntity item)
{
_data.Add(item);
return item;
}
public override TEntity Create()
{
return Activator.CreateInstance<TEntity>();
}
public override TDerivedEntity Create<TDerivedEntity>()
{
return Activator.CreateInstance<TDerivedEntity>();
}
public override ObservableCollection<TEntity> Local
{
get { return _data; }
}
Type IQueryable.ElementType
{
get { return _query.ElementType; }
}
Expression IQueryable.Expression
{
get { return _query.Expression; }
}
IQueryProvider IQueryable.Provider
{
get
{
return new MemoryDbAsyncQueryProvider<TEntity>(_query.Provider);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return _data.GetEnumerator();
}
IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator()
{
return _data.GetEnumerator();
}
IDbAsyncEnumerator<TEntity> IDbAsyncEnumerable<TEntity>.
GetAsyncEnumerator()
{
return new MemoryDbAsyncEnumerator<TEntity>(_data.GetEnumerator());
}
}
答案 1 :(得分:0)
你不应该做你正在做的事情!
public class DbContextFactory : IDbContextFactory<BlogDb>
{
public BlogDb Create()
{
return new BlogDb();
}
}
然后将其注入您的控制器:
public class MyController : Controller
{
private readonly IDbContextFactory<BlogDb> blogContextFactory;
public MyController(IDbContextFactory<BlogDb> blogContextFactory)
{
this.blogContextFactory = blogContextFactory;
}
}
然后在你的方法中你应该妥善处理上下文:
public ActionResult SaveRecord(FormCollection formStuff)
{
if (ModelState.IsValid)
{
try
{
using ( var context = blogContextFactory.Create())
{
// do you stuff here!
}
}
}
}
原因是您不希望打开数据库连接!
现在您正确使用context
,您可以创建一个公开该方法的服务,在该服务中您可以隐藏上下文实现。
public class MyService : IMyService
{
private readonly IDbContextFactory<BlogDb> blogContextFactory;
public MyService(IDbContextFactory<BlogDb> blogContextFactory)
{
this.blogContextFactory = blogContextFactory;
}
public void CreateBlog(FormCollection formStuff) {};
public void UpdateBlog() {};
public IQueryable<Blog> Retrieve(Func<Blog, bool> predicate)
{
return this.context.Blogs.Where(predicate);
}
}
因此IMyService
是您的控制器将与之交互的内容,因此它的名称为接口或合约。因此,您可以在此处公开用于操作博客上下文的方法:
public interface IMyService
{
void CreateBlog(Blog blog); // you can have what ever passed through to the create, a viewModel, a DTO model or the Domain model. Personally I choose the DTO model.
void UpdateBlog(Blog blog);
void DeleteBlog(Guid id);
IQueryable<Blog> RetreiveBlogs(Func<Blog, bool> predicate); // the predicate allows you to do a quick query if you want to
}
所以在你的控制器中这样做:
public class MyController : Controller
{
private readonly IMyService blogService;
public MyController(IMyService blogService)
{
this.blogService= blogService;
}
public ActionResult SaveRecord(FormCollection formStuff)
{
if (ModelState.IsValid)
{
try
{
this.blogService.CreateBlog(formStuff);
}
}
}
}