Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I will go from top (presenter) and describe the relations between participants.</p> <p>Presenter gets service object through dependency. Service functionality is outlined using its contract:</p> <pre><code>class Presenter { public Presenter(IService service) { ... } } </code></pre> <p>Service implementation is abstracted from particular data access layer implementation. Basically whenever service performs some action that requires data source interaction it creates an instance of unit of work and disposes it when it finished.</p> <pre><code>interface IService { void Do(); } class Service : IService { private readonly IUnitOfWorkFactory unitOfWorkFactory; public Service(IUnitOfWorkFactory unitOfWorkFactory) { this.unitOfWorkFactory = unitOfWorkFactory; } public void Do() { // Whenever we need to perform some data manipulation we create and later dispose // dispose unit of work abstraction. It is created through a factory to avoid // dependency on particular implementation. using(IUnitOfWork unitOfWork = this.unitOfWorkFactory.Create()) { // Unit of work holds Entity Framework ObjectContext and thus it used // create repositories and propagate them this ObjectContext to work with IRepository repository = unitOfWork.Create&lt;IRepository&gt;(); repository.DoSomethingInDataSource(); // When we are done changes must be commited which basically means committing // changes of the underlying object context. unitOfWork.Commit(); } } } /// &lt;summary&gt; /// Represents factory of &lt;see cref="IUnitOfWork"/&gt; implementations. /// &lt;/summary&gt; public interface IUnitOfWorkFactory { /// &lt;summary&gt; /// Creates &lt;see cref="IUnitOfWork"/&gt; implementation instance. /// &lt;/summary&gt; /// &lt;returns&gt;Created &lt;see cref="IUnitOfWork"/&gt; instance.&lt;/returns&gt; IUnitOfWork Create(); } /// &lt;summary&gt; /// Maintains a list of objects affected by a business transaction and coordinates the writing out of /// changes and the resolution of concurrency problems. /// &lt;/summary&gt; public interface IUnitOfWork : IDisposable { /// &lt;summary&gt; /// Creates and initializes repository of the specified type. /// &lt;/summary&gt; /// &lt;typeparam name="TRepository"&gt;Type of repository to create.&lt;/typeparam&gt; /// &lt;returns&gt;Created instance of the repository.&lt;/returns&gt; /// &lt;remarks&gt; /// Created repositories must not be cached for future use because once this /// &lt;see cref="IUnitOfWork"/&gt; is disposed they won't be able to work properly. /// &lt;/remarks&gt; TRepository Create&lt;TRepository&gt;(); /// &lt;summary&gt; /// Commits changes made to this &lt;see cref="IUnitOfWork"/&gt;. /// &lt;/summary&gt; void Commit(); } /// &lt;summary&gt; /// Represents factory of &lt;see cref="UnitOfWork"/&gt;s. /// &lt;/summary&gt; public class UnitOfWorkFactory : IUnitOfWorkFactory { private readonly IUnityContainer container; /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="UnitOfWorkFactory"/&gt; class. /// &lt;/summary&gt; /// &lt;param name="container"&gt; /// Dependency injection container instance used to manage creation of repositories /// and entity translators. /// &lt;/param&gt; public UnitOfWorkFactory(IUnityContainer container) { this.conainer = container; } /// &lt;summary&gt; /// Creates &lt;see cref="IUnitOfWork"/&gt; implementation instance. /// &lt;/summary&gt; /// &lt;returns&gt;Created &lt;see cref="IUnitOfWork"/&gt; instance.&lt;/returns&gt; public IUnitOfWork Create() { var unitOfWork = this.container.Resolve&lt;UnitOfWork&gt;(); unitOfWork.SetupObjectContext(); return unitOfWork; } ... other members elidged for clarity } </code></pre> <p>The implementation of IUnitOfWork receives instance of IUnityContainer and then creates child container and registers ObjectContext instance there. This child container will be used to create repositories and propagate ObjectContext.</p> <p>Here is a simplified implementation of IUnitOfWork:</p> <pre><code>class UnitOfWork : IUnitOfWork { private readonly IUnityContainer container; private ObjectContext objectContext; public UnitOfWork (IUnityContainer container) { this.container = container.CreateChildContainer(); } public void SetupObjectContext() { this.objectContext = ... // Create object context here this.container.RegisterInstance(context.GetType(), context); } public void Create&lt;TRepository&gt;() { // As long as we registered created object context instance in child container // it will be available now to repositories during resolve return this.container.Resolve&lt;TRepository&gt;(); } public void Commit() { this.objectContext.SaveChanges(); } } class Repository : IRepository { private readonly SomeObjectContext objectContext; public Repository(SomeObjectContext objectContext) { this.objectContext = objectContext; } public void DoSomethingInDataSource() { // You can use object context instance here to do the work } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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