Created
May 1, 2011 15:28
-
-
Save XiXora/950579 to your computer and use it in GitHub Desktop.
Slice of app
This file contains 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 ContextFactory : IContextFactory | |
{ | |
public DbContext GetCurrentContext() | |
{ | |
DbContext context = HttpContext.Current.Items["CurrentContext"] as DbContext; | |
if (context == null) | |
{ | |
DbProviderInfo providerInfo = new DbProviderInfo("System.Data.SqlClient", "2008"); | |
DbModelBuilder modelBuilder = new DbModelBuilder(); | |
modelBuilder.Configurations.Add(new ClientMapping()); | |
modelBuilder.Configurations.Add(new IconMapping()); | |
modelBuilder.Configurations.Add(new IconSetMapping()); | |
modelBuilder.Configurations.Add(new IconSetSelectionMapping()); | |
modelBuilder.Configurations.Add(new UserMapping()); | |
modelBuilder.Configurations.Add(new UserCompetencyMapping()); | |
modelBuilder.Configurations.Add(new RoleCompetencyMapping()); | |
modelBuilder.Configurations.Add(new UserRoleMapping()); | |
modelBuilder.Configurations.Add(new LanguageMapping()); | |
//modelBuilder.Configurations.Add(new TranslationMapping()); | |
modelBuilder.Configurations.Add(new IconTranslationMapping()); | |
modelBuilder.Configurations.Add(new LocationTranslationMapping()); | |
DbModel model = modelBuilder.Build(providerInfo); | |
context = new DbContext(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString, model.Compile()); | |
HttpContext.Current.Items["CurrentContext"] = context; | |
} | |
return context; | |
} | |
} |
This file contains 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 IconSet : IClientEntity | |
{ | |
public int ClientId { get; private set; } | |
public virtual Client Client { get; private set; } | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Description { get; set; } | |
public virtual ICollection<IconSetSelection> IconSelections { get; set; } | |
} |
This file contains 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 IconSetController : Controller | |
{ | |
private readonly IUnitOfWorkFactory unitOfWorkFactory; | |
private readonly IIconSetRepository iconSetRepository; | |
private readonly ISessionService sessionService; | |
public IconSetController(IUnitOfWorkFactory unitOfWorkFactory, IIconSetRepository iconSetRepository, ISessionService sessionService) | |
{ | |
if (unitOfWorkFactory == null) | |
throw new ArgumentNullException("unitOfWorkFactory"); | |
if (iconSetRepository == null) | |
throw new ArgumentNullException("iconSetRepository"); | |
if (sessionService == null) | |
throw new ArgumentNullException("sessionService"); | |
this.unitOfWorkFactory = unitOfWorkFactory; | |
this.iconSetRepository = iconSetRepository; | |
this.sessionService = sessionService; | |
} | |
// | |
// GET: /Api/IconSet/ | |
[HttpGet] | |
public ActionResult Index() | |
{ | |
IEnumerable<IconSet> iconSets = iconSetRepository.GetAll(sessionService.ClientId); | |
Mapper.CreateMap<IconSet, IconSetViewModel>(); | |
Mapper.CreateMap<Translation, TranslationViewModel>(); | |
IEnumerable<IconSetViewModel> model = Mapper.Map<IEnumerable<IconSet>, IEnumerable<IconSetViewModel>>(iconSets); | |
return this.JsonEx(model); | |
} | |
} |
This file contains 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 IconSetMapping : EntityTypeConfiguration<IconSet> | |
{ | |
public IconSetMapping() | |
{ | |
HasKey(t => new { t.ClientId, t.Id }); | |
HasMany(p => p.IconSelections) | |
.WithRequired() | |
.HasForeignKey(k => new { k.ClientId, k.IconSetId }); | |
} | |
} |
This file contains 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 IconSetRepository : IIconSetRepository | |
{ | |
private readonly DbContext context; | |
private readonly DbSet<IconSet> iconSets; | |
public IconSetRepository(IContextFactory contextFactory) | |
{ | |
if (contextFactory == null) | |
throw new ArgumentNullException("contextFactory"); | |
this.context = contextFactory.GetCurrentContext(); | |
this.iconSets = context.Set<IconSet>(); | |
} | |
public IconSet GetById(int clientId, int id) | |
{ | |
var query = from i in iconSets | |
where i.ClientId == clientId && i.Id == id | |
select i; | |
return query.SingleOrDefault(); | |
} | |
public IEnumerable<IconSet> GetAll(int clientId) | |
{ | |
var query = from i in iconSets | |
where i.ClientId == clientId | |
select i; | |
return query.AsEnumerable(); | |
} | |
} |
This file contains 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 UnitOfWork : IUnitOfWork | |
{ | |
private readonly DbContext context; | |
private List<object> markedNew = new List<object>(); | |
private List<object> markedDirty = new List<object>(); | |
private List<object> markedRemoved = new List<object>(); | |
public UnitOfWork(IContextFactory contextFactory) | |
{ | |
if (contextFactory == null) | |
throw new ArgumentNullException("contextFactory"); | |
context = contextFactory.GetCurrentContext(); | |
} | |
public void New(object entity) | |
{ | |
markedNew.Add(entity); | |
} | |
public void Dirty(object entity) | |
{ | |
markedDirty.Add(entity); | |
} | |
public void Removed(object entity) | |
{ | |
markedRemoved.Add(entity); | |
} | |
public void Commit() | |
{ | |
using (TransactionScope transaction = new TransactionScope()) | |
{ | |
foreach (var entity in markedNew) | |
context.Entry(entity).State = EntityState.Added; | |
foreach (var entity in markedDirty) | |
context.Entry(entity).State = EntityState.Modified; | |
foreach (var entity in markedRemoved) | |
context.Entry(entity).State = EntityState.Deleted; | |
context.SaveChanges(); | |
transaction.Complete(); | |
} | |
} | |
public void Rollback() | |
{ | |
markedNew.Clear(); | |
markedDirty.Clear(); | |
markedRemoved.Clear(); | |
} | |
public void Dispose() | |
{ | |
// Silent | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment