Last active
August 29, 2015 14:02
-
-
Save Jawvig/f4a73d8c2230e9764180 to your computer and use it in GitHub Desktop.
Entity Framework whole context injection
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 Coordinator | |
{ | |
private readonly IMyContext _myContext; | |
private readonly WidgetGenerator _widgetGenerator; | |
private readonly WotsitGenerator _wotsitGenerator; | |
public Coordinator( | |
IMyContext myContext, | |
WidgetGenerator widgetGenerator, | |
WotsitGenerator wotsitGenerator) | |
{ | |
_myContext = myContext; | |
_widgetGenerator = widgetGenerator; | |
_wotsitGenerator = wotsitGenerator; | |
} | |
public void DoStuff() | |
{ | |
_widgetGenerator.GenerateWidgets(); | |
_wotsitGenerator.GenerateWotsits(); | |
_myContext.SaveChanges(); | |
} | |
} | |
public class WotsitGenerator | |
{ | |
private readonly IMyContext _myContext; | |
public WotsitGenerator(IMyContext myContext) | |
{ | |
_myContext = myContext; | |
} | |
public void GenerateWotsits() | |
{ | |
if (DateTime.Now.DayOfWeek == DayOfWeek.Friday) | |
{ | |
throw new Exception("No wotsit generation on Fridays!"); | |
} | |
_myContext.Wotsits.Add(new Wotsit()); | |
} | |
} | |
public class WidgetGenerator | |
{ | |
private readonly IMyContext _myContext; | |
public WidgetGenerator(IMyContext myContext) | |
{ | |
_myContext = myContext; | |
} | |
public void GenerateWidgets() | |
{ | |
_myContext.Widgets.Add(new Widget()); | |
_myContext.SaveChanges(); | |
} | |
} |
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 MyContext : DbContext, IMyContext | |
{ | |
public IDbSet<Widget> Widgets { get; set; } | |
public IDbSet<Wotsit> Wotsits { get; set; } | |
} | |
public interface IMyContext | |
{ | |
IDbSet<Widget> Widgets { get; set; } | |
IDbSet<Wotsit> Wotsits { get; set; } | |
int SaveChanges(); | |
} |
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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (var container = CreateContainer()) | |
{ | |
var coordinator = container.Resolve<Coordinator>(); | |
coordinator.DoStuff(); | |
} | |
} | |
private static IContainer CreateContainer() | |
{ | |
var containerBuilder = new ContainerBuilder(); | |
containerBuilder.RegisterType<MyContext>().AsImplementedInterfaces().InstancePerLifetimeScope(); | |
containerBuilder.RegisterType<Coordinator>(); | |
containerBuilder.RegisterType<WotsitGenerator>(); | |
containerBuilder.RegisterType<WidgetGenerator>(); | |
return containerBuilder.Build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment