Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Injector: Inject same UnitOfWork instance across services of the same graph
    primarykey
    data
    text
    <p>I have multiple services, each of which have a <code>UnitOfWork</code> injected into the constructor using the <a href="http://simpleinjector.codeplex.com">Simple Injector</a> IoC container.</p> <p>Currently I can see each <code>UnitOfWork</code> instance is a separate object, this is bad as i am using Entity Framework and require the same context reference across all units of work.</p> <p>How can I ensure the same <code>UnitOfWork</code> instance is injected into all services per each resolve request? My <code>UnitOfWor</code> will be saved by an external command handler decorator after the command completes.</p> <p>Please note, this is a common library and will be used for both MVC and Windows Forms, it would be nice to have a generic solution for both platforms if possible.</p> <p>Code is below:</p> <pre><code>// snippet of code that registers types void RegisterTypes() { // register general unit of work class for use by majority of service layers container.Register&lt;IUnitOfWork, UnitOfWork&gt;(); // provide a factory for singleton classes to create their own units of work // at will container.RegisterSingle&lt;IUnitOfWorkFactory, UnitOfWorkFactory&gt;(); // register logger container.RegisterSingle&lt;ILogger, NLogForUnitOfWork&gt;(); // register all generic command handlers container.RegisterManyForOpenGeneric(typeof(ICommandHandler&lt;&gt;), AppDomain.CurrentDomain.GetAssemblies()); container.RegisterDecorator(typeof(ICommandHandler&lt;&gt;), typeof(TransactionCommandHandlerDecorator&lt;&gt;)); // register services that will be used by command handlers container.Register&lt;ISynchronisationService, SynchronisationService&gt;(); container.Register&lt;IPluginManagerService, PluginManagerService&gt;(); } </code></pre> <p>The desired outcome of the below line is to create a object which has a shared UnitOfWork instance throughout the constructed object graph:</p> <pre><code>var handler = Resolve&lt;ICommandHandler&lt;SyncExternalDataCommand&gt;&gt;(); </code></pre> <p>Here are my services:</p> <pre><code>public class PluginManagerService : IPluginSettingsService { public PluginManagerService(IUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; } private readonly unitOfWork; void IPluginSettingsService.RegisterPlugins() { // manipulate the unit of work } } public class SynchronisationService : ISynchronisationService { public PluginManagerService(IUnitOfWork unitOfWork) { this.unitOfWork = unitOfWork; } private readonly unitOfWork; void ISynchronisationService.SyncData() { // manipulate the unit of work } } public class SyncExternalDataCommandHandler : ICommandHandler&lt;SyncExternalDataCommand&gt; { ILogger logger; ISynchronisationService synchronisationService; IPluginManagerService pluginManagerService; public SyncExternalDataCommandHandler( ISynchronisationService synchronisationService, IPluginManagerService pluginManagerService, ILogger logger) { this.synchronisationService = synchronisationService; this.pluginManagerService = pluginManagerService; this.logger = logger; } public void Handle(SyncExternalDataCommand command) { // here i will call both services functions, however as of now each // has a different UnitOfWork reference internally, we need them to // be common. this.synchronisationService.SyncData(); this.pluginManagerService.RegisterPlugins(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload