Last active
February 24, 2017 23:53
-
-
Save Jawvig/813a125e8559de8113c7 to your computer and use it in GitHub Desktop.
Entity Framework tightened dependencies
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 ICompleteUnitOfWork _unitOfWorkCompleter; | |
private readonly WidgetGenerator _widgetGenerator; | |
private readonly WotsitGenerator _wotsitGenerator; | |
public Coordinator( | |
ICompleteUnitOfWork unitOfWorkCompleter, | |
WidgetGenerator widgetGenerator, | |
WotsitGenerator wotsitGenerator) | |
{ | |
_unitOfWorkCompleter = unitOfWorkCompleter; | |
_widgetGenerator = widgetGenerator; | |
_wotsitGenerator = wotsitGenerator; | |
} | |
public void DoStuff() | |
{ | |
_widgetGenerator.GenerateWidgets(); | |
_wotsitGenerator.GenerateWotsits(); | |
_unitOfWorkCompleter.SaveChanges(); | |
} | |
} | |
public class WotsitGenerator | |
{ | |
private readonly IDbSet<Wotsit> _wotsitDbSet; | |
public WotsitGenerator(IDbSet<Wotsit> wotsitDbSet) | |
{ | |
_wotsitDbSet = wotsitDbSet; | |
} | |
public void GenerateWotsits() | |
{ | |
if (DateTime.Now.DayOfWeek == DayOfWeek.Friday) | |
{ | |
throw new Exception("No wotsit generation on Fridays!"); | |
} | |
_wotsitDbSet.Add(new Wotsit()); | |
} | |
} | |
public class WidgetGenerator | |
{ | |
private readonly IDbSet<Widget> _widgetDbSet; | |
public WidgetGenerator(IDbSet<Widget> widgetDbSet) | |
{ | |
_widgetDbSet = widgetDbSet; | |
} | |
public void GenerateWidgets() | |
{ | |
_widgetDbSet.Add(new Widget()); | |
} | |
} |
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, ICompleteUnitOfWork | |
{ | |
public IDbSet<Widget> Widgets { get; set; } | |
public IDbSet<Wotsit> Wotsits { get; set; } | |
} | |
public interface ICompleteUnitOfWork | |
{ | |
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>().AsSelf().AsImplementedInterfaces().InstancePerLifetimeScope(); | |
containerBuilder.Register(c => c.Resolve<MyContext>().Widgets); | |
containerBuilder.Register(c => c.Resolve<MyContext>().Wotsits); | |
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