Skip to content

Instantly share code, notes, and snippets.

@gprasant
gprasant / duckable.cs
Created October 22, 2011 04:13
shows how the foreach does not necessarily require an IEnumerable. Demonstrates the use of Duck typing in C#
// resembles a IEnumerator. has Current and MoveNext
public class Duckerator
{
public bool MoveNext()
{
return false;
}
public Object Current { get; private set; }
}
@gprasant
gprasant / TestAggregate.cs
Created May 1, 2012 14:19
Shows how Enumerable.Aggregate can be used
namespace IEnumerable.Tests
{
[TestClass]
public class TestAggregate
{
Func<int, int, int> sum = (a, b) => a + b;
[TestMethod]
public void ShouldAggregateAllElementsInASequence()
{
var sequence = Enumerable.Range(1, 2);
@gprasant
gprasant / User_1.cs
Created May 11, 2012 17:07
A user class
public class User
{
public string Name { get; set; }
public string EMail { get; set; }
public string Phone1 { get; set; }
public string Phone2 { get; set; }
public virtual IEnumerable<Permission> Permissions { get; set; }
public User()
@gprasant
gprasant / User_Principal.cs
Created May 11, 2012 17:15
A user class implementing IPrincipal
public class User : IPrincipal
{
public string Name { get; set; }
public string EMail { get; set; }
public string Phone1 { get; set; }
public string Phone2 { get; set; }
public virtual IEnumerable<Permission> Permissions { get; set; }
public User()
@gprasant
gprasant / Global.asax.cs
Created May 11, 2012 17:22
Setting HttpContext.User to a custom Domain rather that System.Security.Identity.User
void WindowsAuthentication_OnAuthenticate(object sender, WindowsAuthenticationEventArgs e)
{
// Instantiate a user repository so as to get User information
IRepository<User> userRepository = new Repository<User>();
if (e.Identity != null && e.Identity.IsAuthenticated)
{
WindowsIdentity wi = e.Identity;
User user = userRepository.GetBy(u => u.Name == wi.Name).FirstOrDefault();
user.Identity = wi;
HttpContext.Current.User = user;
@gprasant
gprasant / SampleController.cs
Created May 11, 2012 17:44
A CustomController with a
[Authorize(Roles="ACustomRole")]
public class SomeController : Controller
{
}
namespace MvcApplication1.Models
{
public class CordBloodUnit
{
public int ID { get; set; }
public int HLA_A1 { get; set; }
public int HLA_A2 { get; set; }
public int HLA_B1 { get; set; }
public int HLA_B2 { get; set; }
namespace MvcApplication.Database
{
public class MyAppContext : DbContext
{
public DbSet<CordBloodUnit> CordBloodUnits { get; set; }
}
}
protected void Application_Start()
{
System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists<JeevanDBContext>());
//new LogEvent("Completed db Setup");
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BundleTable.Bundles.RegisterTemplateBundles();
public class RecreateDatabaseWithSeedData : DropCreateDatabaseIfModelChanges<Jeevan.Database.JeevanDBContext>
{
protected override void Seed(JeevanDBContext context)
{
context.CordBloodUnits.Add(new CordBloodUnit() { HLA_A1 = 2, HLA_A2 = 24, HLA_B1 = 18, HLA_B2 = 40, HLA_C1 = 12, HLA_C2 = 15, DRB_1 = 11, DRB_2 = 14, DQB_1 = 03, DQB_2 = 05 });
context.CordBloodUnits.Add(new CordBloodUnit(){HLA_A1=1, HLA_A2=32, HLA_B1=15, HLA_B2= 57, HLA_C1 = 6 , HLA_C2 = 07, DRB_1 = 15, DRB_2 = 16 , DQB_1 = 05, DQB_2 = 05});
context.CordBloodUnits.Add(new CordBloodUnit(){HLA_A1=24, HLA_A2=33, HLA_B1=40, HLA_B2= 44, HLA_C1 = 1 , HLA_C2 = 07, DRB_1 = 01, DRB_2 = 07 , DQB_1 = 02, DQB_2 = 05});
context.CordBloodUnits.Add(new CordBloodUnit(){HLA_A1=1, HLA_A2=33, HLA_B1=15, HLA_B2= 44, HLA_C1 = 3 , HLA_C2 = 07, DRB_1 = 07, DRB_2 = 13 , DQB_1 = 02, DQB_2 = 06});
context.CordBloodUnits.Add(new CordBloodUnit(){HLA_A1=3, HLA_A2=68, HLA_B1=15, HLA_B2= 55, HLA