IRepository.cs Interface file. Repository.cs class file

− IRepository.cs Interface file using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Threading.Tasks; ...
Author: Jesse Wood
3 downloads 0 Views 38KB Size
− IRepository.cs Interface file using using using using using

System; System.Collections.Generic; System.Linq; System.Text; System.Threading.Tasks;

namespace MyClassLibrary { public interface IRepository where T : class { IQueryable GetAll(); T GetById(int id); void Add(T entity); void Update(T entity, int id); void Delete(T entity); void Delete(int id); } }

− Repository.cs class file using using using using using using using

System; System.Collections.Generic; System.Data; System.Data.Entity; System.Linq; System.Text; System.Threading.Tasks;

namespace MyClassLibrary { public class Repository : IRepository where T : class { protected DatabaseContext DbContext { get; set; } protected DbSet DbSet { get; set; }

public Repository(DatabaseContext dbContext) { if (dbContext == null) throw new NullReferenceException("dbContext"); DbContext = dbContext; DbSet = dbContext.Set(); }

public IQueryable GetAll() {

return DbSet; } public T GetById(int id) { return DbSet.Find(id); } public void Add(T entity) { DbSet.Add(entity); } public void Update(T entity, int id) { var entry = DbContext.Entry(entity); var currententry = GetById(id); DbContext.Entry(currententry).CurrentValues.SetValues(entity); } public void Delete(T entity) { var entry = DbContext.Entry(entity); DbSet.Attach(entity); entry.State = EntityState.Deleted; } public void Delete(int id) { var entity = GetById(id); if (entity == null) return; Delete(entity); } } }

− Database.cs class file using using using using using

System; System.Collections.Generic; System.Linq; System.Text; System.Threading.Tasks;

namespace MyClassLibrary { public class Database { private DatabaseContext _DatabaseContext; public Database(DatabaseContext _Databasecontext1)

{ _DatabaseContext = _Databasecontext1; } public IRepository Companies { get { return new Repository(_DatabaseContext); } } public void save() { _DatabaseContext.SaveChanges(); } } }

− ConnectionString



MainWindow.xaml











− Mainwindow.xaml.cs public partial class MainWindow : Window { private MyClassLibrary.Database db; private MyClassLibrary.DatabaseContext _dbcontext = new MyClassLibrary.DatabaseContext(); public MainWindow() { InitializeComponent(); } private void btndelete_Click(object sender, RoutedEventArgs e) { db = new MyClassLibrary.Database(_dbcontext); var newcompany = new Company(); newcompany.Id = newcompany.Id = Convert.ToInt32(txtid.Text.Trim()); db.Companies.Delete(newcompany.Id); db.save(); btnClear_Click(sender, e); companyDataGrid.Items.Refresh(); } private void btnupdate_Click(object sender, RoutedEventArgs e) { db = new MyClassLibrary.Database(_dbcontext); var newcompany = new Company(); newcompany.Id = Convert.ToInt32(txtid.Text.Trim()); newcompany.Name = txtname.Text.Trim(); newcompany.Telephone = txttelephone.Text.Trim(); newcompany.Email = txtemail.Text.Trim();

newcompany.GPS = txtgps.Text.Trim(); db.Companies.Update(newcompany, newcompany.Id); db.save(); btnClear_Click(sender, e); companyDataGrid.Items.Refresh(); } private void btnInsert_Click(object sender, RoutedEventArgs e) { db = new MyClassLibrary.Database(_dbcontext); var newcompany = new Company(); newcompany.Name = txtname.Text.Trim(); newcompany.Telephone = txttelephone.Text.Trim(); newcompany.Email = txtemail.Text.Trim(); newcompany.GPS = txtgps.Text.Trim(); db.Companies.Add(newcompany); db.save(); btnClear_Click(sender, e); companyDataGrid.Items.Refresh(); } private void btnClear_Click(object sender, RoutedEventArgs e) { txtid.Text = ""; txtname.Text = ""; txttelephone.Text = ""; txtemail.Text = ""; txtgps.Text = ""; } private void Window_Loaded_1(object sender, RoutedEventArgs e) { System.Windows.Data.CollectionViewSource companyViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("companyViewSource"))); // Load data by setting the CollectionViewSource.Source property: // companyViewSource.Source = [generic data source] _dbcontext.Companies.Load(); companyViewSource.Source = _dbcontext.Companies.Local; }

Suggest Documents