Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Convert from PetaPoco to EntityFramework #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions MyepWeb/App-Code/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ namespace Site
{
public class HomeController : Controller
{
private readonly SiteDb _db;
private readonly Interns _interns;
private readonly Users _users;

public HomeController(SiteDb db, Interns interns, Users users)
public HomeController(Interns interns, Users users)
{
_db = db;
_interns = interns;
_users = users;
}
Expand Down
6 changes: 3 additions & 3 deletions MyepWeb/App-Code/Models/Code.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
namespace Site
{
[Table("Codes")]
public class Code : IEntity
public class Code
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

[Required]
[Index("IX_Code_TypeValue", true)]
[Index("IX_Code_TypeValue", IsUnique = true)]
[StringLength(100)]
public string Type { get; set; }

[Required]
[Index("IX_Code_TypeValue", true)]
[Index("IX_Code_TypeValue", IsUnique = true)]
[StringLength(100)]
public string Value { get; set; }

Expand Down
5 changes: 3 additions & 2 deletions MyepWeb/App-Code/Models/Intern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace Site
{
[Table("Interns")]
public class Intern : IEntity
public class Intern
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

[StringLength(10)]
[Index("IX_Intern_AccessCode", unique: true)]
[Index("IX_Intern_AccessCode", IsUnique = true)]
public string AccessCode { get; set; }

[StringLength(10)]
Expand Down Expand Up @@ -79,6 +79,7 @@ public class Intern : IEntity

public bool HasSummerSchool { get; set; }

[StringLength(50)]
public string InternshipAvailability { get; set; }

public bool ConvictedOfCrime { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion MyepWeb/App-Code/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Site
{
[Table("Users")]
public class User : IEntity
public class User
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
Expand Down
30 changes: 23 additions & 7 deletions MyepWeb/App-Code/Services/Codes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,44 @@ namespace Site
{
public class Codes
{
private readonly SiteDb _db;
private readonly IDb _db;

public Codes(SiteDb db)
public Codes(IDb db)
{
_db = db;
}

public List<string> GetTypes()
{
return _db.Query<string>("SELECT DISTINCT [Type] FROM [Codes] ORDER BY [Type]").ToList();
return _db
.Query<Code>()
.Select(x => x.Type)
.Distinct()
.OrderBy(x => x)
.ToList();
}

public List<Code> Query(string type, bool? active = null)
{
var sql = "SELECT * FROM [Codes] WHERE [Type]=@0";
if (active == true) sql += " AND Seq>=0 ";
return _db.Query<Code>(sql + " ORDER BY [Seq],[Value]", type).ToList();
var query = _db
.Query<Code>()
.Where(x => x.Type == type);

if (active == true)
{
query = query.Where(x => x.Seq >= 0);
}

return query
.OrderBy(x => x.Seq)
.ThenBy(x => x.Value)
.ToList();
}

public void Save(Code model)
{
_db.Save("Codes", "Id", model);
_db.Save(model, model.Id == 0);
_db.SaveChanges();
}
};
}
50 changes: 37 additions & 13 deletions MyepWeb/App-Code/Services/Interns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,59 @@ namespace Site
{
public class Interns
{
private readonly SiteDb _db;
private readonly IDb _db;

public Interns(SiteDb db)
public Interns(IDb db)
{
_db = db;
}

public List<InternInfo> Query(bool? inactive = false)
{
var sql = "SELECT Id AS InternId, FirstName+' '+LastName AS FullName FROM Interns";
if (inactive != true) sql += " WHERE Inactive=0 ";
return _db.Query<InternInfo>(sql).ToList();
var query = _db.Query<Intern>();

if (inactive != true)
{
query = query.Where(x => x.Inactive == false);
}

return query
.Select(x => new InternInfo
{
InternId = x.Id,
FullName = x.FirstName + " " + x.LastName,
})
.ToList();
}

public Intern Load(int? id)
{
if (!id.HasValue) return null;
return _db.SingleOrDefault<Intern>("SELECT * FROM Interns WHERE Id=@0", id);
if (!id.HasValue)
return null;

return _db
.Query<Intern>()
.SingleOrDefault(x => x.Id == id);
}

public Intern LoadByCode(string code)
{
if (!code.HasValue()) return null;
return _db.SingleOrDefault<Intern>("SELECT * FROM Interns WHERE CmsStudentId=@0 OR AccessCode=@0", code);
if (!code.HasValue())
return null;

return _db
.Query<Intern>()
.SingleOrDefault(x => x.CmsStudentId == code || x.AccessCode == code);
}

public Intern LoadByCmsId(string id)
{
if (!id.HasValue()) return null;
return _db.SingleOrDefault<Intern>("SELECT * FROM Interns WHERE CmsStudentId=@0", id);
if (!id.HasValue())
return null;

return _db
.Query<Intern>()
.SingleOrDefault(x => x.CmsStudentId == id);
}

public Intern Create(string cmsStudentId)
Expand Down Expand Up @@ -80,7 +103,8 @@ public void Save(Intern model)
if (model.RaceOther.HasValue() && !model.Race.Or().Contains("Other"))
model.Race = string.Join(",", model.Race.Or().Split(',').Union(new[] { "Other" }).Distinct());

_db.Save("Interns", "Id", model);
_db.Save(model, model.Id == 0);
_db.SaveChanges();
}

public void UploadEssay(Intern model, HttpPostedFileBase file)
Expand Down Expand Up @@ -171,7 +195,7 @@ public int ImportSchoolFile(HttpPostedFileBase file)

public Stream Export()
{
var interns = _db.Query<Intern>("SELECT * FROM Interns");
var interns = _db.Query<Intern>().ToList();
using (var excel = Excel.Create(interns))
{
return excel.GetStream();
Expand Down
114 changes: 101 additions & 13 deletions MyepWeb/App-Code/Services/SiteDb.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,111 @@
using PetaPoco;
using PetaSchema;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace Site
{
public class SiteDb : Database
public interface IDb : IDisposable
{
public SiteDb() : base("SiteDb") { }
IQueryable<T> Query<T>() where T : class;
T Add<T>(T entity) where T : class;
T Save<T>(T entity, bool? isNew = null) where T : class;
T Delete<T>(T entity) where T : class;
bool IsDetached<T>(T entity) where T : class;
int SaveChanges();
int ExecuteSqlCommand(string sql, params object[] parameters);
List<T> SqlQuery<T>(string sql, params object[] parameters);
};

public DbSchema GetSchema()
public class SiteDb : DbContext, IDb
{
public SiteDb(string cs) : base(cs)
{
var schema = new DbSchema(this);
Ext.ImplementorsOf<IEntity>().ForEach(schema.AddTable);
return schema;
Database.SetInitializer<SiteDb>(null);
}

};
public DbSet<Code> Codes { get; set; }
public DbSet<Intern> Interns { get; set; }
public DbSet<User> Users { get; set; }

public interface IEntity
{
//placeholder interface for database entities
}
//REF: http://msdn.microsoft.com/en-us/data/jj819164.aspx
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.AddFromAssembly(typeof(SiteDb).Assembly);
modelBuilder.HasDefaultSchema("dbo");
}

public virtual bool IsDetached<T>(T entity) where T : class
{
var entry = Entry(entity);
return entry.State == EntityState.Detached;
}

public virtual T Attach<T>(T entity) where T : class
{
var entry = Entry(entity);
if (entry.State == EntityState.Detached) Set<T>().Attach(entity);
return entity;
}

public virtual T Modified<T>(T entity) where T : class
{
var entry = Entry(entity);
if (entry.State == EntityState.Detached) entry.State = EntityState.Modified;
return entity;
}

public virtual T Add<T>(T entity) where T : class
{
Entry(entity).State = EntityState.Added;
return entity;
}

public virtual T Save<T>(T entity, bool? isNew = null) where T : class
{
var entry = Entry(entity);
if (entry.State == EntityState.Detached)
{
if (isNew == false)
{
Modified(entity);
}
else
{
Add(entity);
}
}
return entity;
}

public virtual T Delete<T>(T entity) where T : class
{
var entry = Entry(entity);
if (entry.State != EntityState.Detached) entry.State = EntityState.Deleted;
return entity;
}

public void DeleteAll<T>(IEnumerable<T> entities) where T : class
{
entities.Each(x => Delete(x));
}

public IQueryable<T> Query<T>() where T : class
{
return Set<T>();
}

public List<T> SqlQuery<T>(string sql, params object[] parameters)
{
return Database
.SqlQuery<T>(sql, parameters)
.ToList();
}

public int ExecuteSqlCommand(string sql, params object[] parameters)
{
return Database
.ExecuteSqlCommand(sql, parameters);
}
};
}
Loading