mardi 13 février 2018

Undersatnding Unit of Work pattern without any ORM

I am trying to understand essence of Unit of Work but almost all programmers use Entity Framework or other ORM in their examples. So I want to create my own Unit of Work + Repository.

I've tried to found detailed pattern guide, but almost all guides I've found are incomplete or just contain incomplete information. I don't want to use Entity Framework or other ORM while I don't understand how does this pattern works.

What I have found:

public interface IEntity
{
    int Id { get; set; }
    void Insert();
    void Update();
    void Delete();
}

public class UnityOfWork : IUnitOfWork
{
    private List<IEntity> _inserted;
    private List<IEntity> _updated;
    private List<IEntity> _deleted;

    public void Add(IEntity entity)
    {
        _inserted.Add(entity);
    }

    public void Commit()
    {
        using (TransactionScope ts = new TransactionScope())
        {
            foreach (IEntity entity in _inserted)
            {
                entity.Insert();
            }

            foreach (IEntity entity in _updated)
            {
                entity.Update();
            }

            foreach (IEntity entity in _deleted)
            {
                entity.Delete();
            }

            ts.Complete();
        }
    }
}

This variant uses three Lists for storing inserted, updated, deleted entities. I have found out that this is being used to send only changed data to server. But I also saw that UnitOfWork is a kind of storage for all repositories. And code will look like

interface IFooRepository : IDisposable
{
    IEnumerable<Foo> GetFooList();
    Foo GetFoo(int id);
    void Create(Foo item);
    void Update(Foo item);
    void Delete(int id);
}

//Almost the same IBarRepository

public class FooRepository: IFooRepository 
{
    IEnumerable<Foo> GetFooList();
    Foo GetFoo(int id);
    void Create(Foo item);
    void Update(Foo item);
    void Delete(int id);
}

public class BarRepository: IBarRepository
{
    // Implementation of interface
}

public class UnityOfWork : IUnitOfWork
    {
    private FooRepository;
    private BarRepository;
    ...

    public void Add(IEntity entity)
    {
        _inserted.Add(entity);
    }

    //Rest of code
}

So what do I have to do with this case? Should I use Lists (inserted, deleted, ...) for each repository? But what if I have a large number of repositories? Is code will still be maintainable?

I have also found this code but without any implementation:

public interface IUnitOfWork 
{ 
void MarkDirty(object entity)
void MarkNew(object entity);
void MarkDeleted(object entity);
void Commit();
void Rollback();
}

What URL's have I checked:

  1. Patterns in Practice - The Unit Of Work Pattern And Persistence Ignorance
  2. Understanding Unit of Work / Repository patterns without an ORM framework.
  3. Generic Repository without Entity Framework

And many many more...

Sorry if it isn't a difficult question but I have already spent 4 days to find solution and didn't receive any answers.

Aucun commentaire:

Enregistrer un commentaire