如何在接口中实现Delete方法并在Controller MVC4中调用它 [英] How to implement the Delete method in the interface and call it in the Controller MVC4

查看:89
本文介绍了如何在接口中实现Delete方法并在Controller MVC4中调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在我的DataSource中实现我的void Delete方法。我已经创建了Save()方法,但是正在努力使用Delete()。我一直在寻找,但仍然不知道该怎么做。任何帮助。



界面:



Hi i want to implement my void Delete method in my DataSource. I've created the Save() method, but struggling with Delete(). I've been searching but still don't know how to do it. Any help.

The interface:

   public  interface IDepartmentDataSource
{
    IQueryable<User> Users { get; }
    IQueryable<Department> Departments { get;}
    IQueryable<Entry> Entries { get; }

    void Delete();
    void Save();

}





数据源:





DataSource:

public class DepartmentDb : DbContext, IDepartmentDataSource
{
    public DepartmentDb() : base("DefaultConnection")
    {

    }

    public DbSet<User> Users { get; set; }
    public DbSet<Department> Departments { get; set; }
    public DbSet<Entry> Entries { get; set; }


    void IDepartmentDataSource.Save()
    {
        SaveChanges();  
    }

    void IDepartmentDataSource.Delete()
    {

        // What is the logic here?

    }

    IQueryable<Entry> IDepartmentDataSource.Entries
    {
        get { return Entries; }
    }

    IQueryable<Department> IDepartmentDataSource.Departments
    {
        get { return Departments; }
    }

    IQueryable<User> IDepartmentDataSource.Users
    {
        get { return Users; }
    }
}

推荐答案

你可以用[HttpPost,ActionName(Delete)]装饰控制器



请参阅此链接 [ ^ ]
you can decorate controller with [HttpPost, ActionName("Delete")]

please refer this link[^]


你可以使用:





you can use :


void IDepartmentDataSource.Delete(Entry entry) 
{
    Entries.Remove(entry);
}







但可能是,

使用像下面这样的存储库可能更好

(一个封装每个CRUD EF操作的存储库)

i会分两步:




but may be,
using a repository like below could be better
(a repository that encapsulate every CRUD EF operation)
i would make it in 2 steps :

// this is your EF dbcontext (stndard)
public class DepartmentDb : DbContext
{
    public DepartmentDb() : base("DefaultConnection")
    {

    }

    public DbSet<User> Users { get; set; }
    public DbSet<Department> Departments { get; set; }
    public DbSet<Entry> Entries { get; set; }

}

// your repository
public class DepartmentRepository: IDisposable
{
    protected bool isDisposed = false;
    private DepartmentDb Context { get; set; } 

    // read (public)
    public IQueryable<user> Users { get { return Context.Users; } }
    public IQueryable<department> Departments { get { return Context.Departments; } }
    public IQueryable<entry> Entries { get { return Context.Entries; } }

    public DepartmentRepository() 
    {
         Context = new DepartmentDb();
    }

    // write the same for the others
    public int Insert(User item)
    { 
        Context.Users.Add(item);
        return Context.SaveChanges();
    }

    // write the same for the others
    public int Update(User item)
    { 
        Context.Entry<user>(item).State = EntityState.Modified;
        return Context.SaveChanges();
    }

    // write the same for the others
    public int Delete(User item)
    { 
        Context.Users.Remove(item);
        return Context.SaveChanges();
    }

    protected virtual void Dispose(bool disposing)
    {
	if (isDisposed)
		return;

	if (disposing)
	{
		if (this.Context != null)
		{
			this.Context.Dispose();
			this.Context = null;
		}
	}

	isDisposed = true;
    }

    public void Dispose()
    {
	Dispose(true);
	GC.SuppressFinalize(this);
    }	

}





用法:





usage :

using(var repo = new DepartmentRepository())
{
  var u = new User(){Name="Jack"};
  var repo.Insert(u);
  u.Name = "Jim";
  repo.Update(u);
  repo.Delete(u);
}





如果你想阅读更多查找存储库模式它有几个版本









更新:

如果你想要写更少的代码;)

你可以写



if you want to read more look for "repository pattern" there are several versions of it




UPDATE :
if you want to write less code ;)
you can write

public interface IDepRepoEntity
{
    // empty
}
public partial class User : IDepRepoEntity {}
public partial class Department: IDepRepoEntity {}
public partial class Entry: IDepRepoEntity {}



比你的repositor y:


than in your repository :

public int Insert<T>(T item) where T : IDepRepoEntity
{
    Context.Set<T>.Add(item);
    return Context.SaveChanges();
}

public int Update<T>(T item) where T : IDepRepoEntity
{
    Context.Entry<T>(item).State = EntityState.Modified;
    return Context.SaveChanges();
}

public int Delete<T>(T item) where T : IDepRepoEntity
{
    Context.Set<T>.Remove(item);
    return Context.SaveChanges();
}


这篇关于如何在接口中实现Delete方法并在Controller MVC4中调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆