Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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; }
    }
}
Posted
Comments
Gihan Liyanage 29-Sep-14 6:28am    
Do you want delete by user ID ? Or you want to delete Departments by DepID?
PetarS089 29-Sep-14 6:49am    
I want to delete Department by Id. I have Department Class that have Id and Department_Name.

you can decorate controller with [HttpPost, ActionName("Delete")]

please refer this link[^]
 
Share this answer
 
you can use :


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



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 :
C#
// 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 :

C#
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
C#
public interface IDepRepoEntity
{
    // empty
}
public partial class User : IDepRepoEntity {}
public partial class Department: IDepRepoEntity {}
public partial class Entry: IDepRepoEntity {}

than in your repository :
C#
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();
}
 
Share this answer
 
v4
Comments
PetarS089 30-Sep-14 10:02am    
Thanks, the past day i was looking for "repository pattern" and learned and found a solution, just like yours. Thank you again.
nrgjack 30-Sep-14 10:25am    
i've added another part if you want to write less code :P
PetarS089 1-Oct-14 7:21am    
That's very helpful, thank you again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900