Created
April 1, 2012 08:32
-
-
Save Zaidos/2273448 to your computer and use it in GitHub Desktop.
The Backbone Experience
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ApiAreaRegistration : AreaRegistration | |
{ | |
public override string AreaName | |
{ | |
get { return "Api"; } | |
} | |
public override void RegisterArea(AreaRegistrationContext context) | |
{ | |
const string url = "Api/Issues"; | |
RegisterApiRoutes(context, url); | |
context.MapRoute( | |
"Api_Default", | |
"Api/{controller}/{action}/{id}", | |
new | |
{ | |
controller = "ApiIssues", | |
action = "Index", | |
id = UrlParameter.Optional | |
} | |
); | |
} | |
private static void RegisterApiIssueRoutes(AreaRegistrationContext context, string url) | |
{ | |
context.MapRoute( | |
"Issue_Get", | |
url, | |
new | |
{ | |
controller = "ApiIssues", | |
action = "Index", | |
area = "" | |
}, | |
new | |
{ | |
httpMethod = new HttpMethodConstraint("GET") | |
} | |
); | |
context.MapRoute( | |
"Issue_Get_Single", | |
url + "/{id}", | |
new | |
{ | |
controller = "ApiIssues", | |
action = "Get", | |
}, | |
new | |
{ | |
httpMethod = new HttpMethodConstraint("GET") | |
} | |
); | |
context.MapRoute( | |
"Issue_Post", | |
url, | |
new | |
{ | |
controller = "ApiIssues", | |
action = "Create" | |
}, | |
new | |
{ | |
httpMethod = new HttpMethodConstraint("POST") | |
} | |
); | |
context.MapRoute( | |
"Issue_Put", | |
url + "/{id}", | |
new | |
{ | |
controller = "ApiIssues", | |
action = "Update" | |
}, | |
new | |
{ | |
httpMethod = new HttpMethodConstraint("PUT") | |
} | |
); | |
context.MapRoute( | |
"Issue_Delete", | |
url + "/{id}", | |
new | |
{ | |
controller = "ApiIssues", | |
action = "Delete" | |
}, | |
new | |
{ | |
httpMethod = new HttpMethodConstraint("DELETE") | |
} | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ApiIssuesController : RestController<Issue> | |
{ | |
private readonly IssueContext _context; | |
public ApiIssuesController() | |
: this(new IssueContext()) | |
{ | |
} | |
public ApiIssuesController(IssueContext context) | |
{ | |
_context = context; | |
} | |
[HttpGet] | |
public override ActionResult Index() | |
{ | |
var model = _context.Issues; | |
return Json(model, JsonRequestBehavior.AllowGet); | |
} | |
[HttpGet] | |
public override ActionResult New() | |
{ | |
throw new NotImplementedException(); | |
} | |
[HttpGet] | |
public override ActionResult Get(int id) | |
{ | |
var model = _context.Issues.Find(id); | |
return Json(model, JsonRequestBehavior.AllowGet); | |
} | |
[HttpPost] | |
public override ActionResult Create(Issue entity) | |
{ | |
var model = _context.Issues.Add(entity); | |
_context.SaveChanges(); | |
return Json(model); | |
} | |
[HttpGet] | |
public override ActionResult Edit(int id) | |
{ | |
throw new NotImplementedException(); | |
} | |
[HttpPut] | |
public override ActionResult Update(Issue entity) | |
{ | |
var model = _context.Issues.Find(entity.Id); | |
// Call some code to update model properties. | |
_context.SaveChanges(); | |
return Json(model); | |
} | |
[HttpDelete] | |
public override ActionResult Delete(int id) | |
{ | |
var model = _context.Issues.Find(entity.Id); | |
_context.Issues.Remove(model); | |
_context.SaveChanges(); | |
return new EmptyResult(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDbContext : IDisposable | |
{ | |
IDbSet<Issue> Issues { get; } | |
int SaveChanges(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Issue | |
{ | |
[Key] | |
public int Id { get; set;} | |
[Required] | |
public string Name { get; set; } | |
public string Description { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class IssueContext : DbContext, IDbContext | |
{ | |
private const string IssuesSchemaName = "Issues"; | |
public IssueContext() | |
: base("IssueContext") | |
{ | |
} | |
public IssueContext(string connectionString) | |
: base(connectionString) | |
{ | |
} | |
protected override void OnModelCreating(DbModelBuilder modelBuilder) | |
{ | |
// Remove pluralizing table name conventions. | |
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); | |
// Need to explicitly state table name for model. | |
modelBuilder.Entity<Issue>().ToTable("Issue", IssuesSchemaName); | |
base.OnModelCreating(modelBuilder); | |
} | |
#region Implementation of IDbContext | |
/// <summary> | |
/// Issues DbSet. | |
/// </summary> | |
public IDbSet<Issue> Issues { get; set; } | |
#endregion | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class RestController<T> : BaseController | |
{ | |
[HttpGet] | |
public abstract ActionResult Index(); | |
[HttpGet] | |
public abstract ActionResult New(); | |
[HttpGet] | |
public abstract ActionResult Get(int id); | |
[HttpPost] | |
public abstract ActionResult Create(T entity); | |
[HttpGet] | |
public abstract ActionResult Edit(int id); | |
[HttpPut] | |
public abstract ActionResult Update(T entity); | |
[HttpDelete] | |
public abstract ActionResult Delete(int id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment