Continuation of this question: Unit of Work with Generic Repository Pattern MVVM
I have made some modifications:
- Interfaces aren't forced to implement IDisposable
- Removed the finalizers
- Rebuilt UoW
FeedRepository:
internal class FeedRepository<T> : IFeedRepository<T>, IDisposable
where T: IBaseFeed, new()
{
private readonly SQLiteConnection _db;
private IList<T> _feeds;
private bool _isDisposed = false;
public int Count
{
get
{
return _feeds.Count;
}
}
public FeedRepository(SQLiteConnection db)
{
this._db = db;
this._feeds = _db.Table<T>().ToList();
}
public void Add(T feed)
{
this._feeds.Add(feed);
this._db.Insert(feed);
}
public IEnumerable<T> GetAll()
{
return this._feeds;
}
public T GetFeedById(int id)
{
return this._feeds.Where(feed => int.Equals(feed.Id, id)).Single();
}
public T GetFeedByLink(string feedLink)
{
return this.GetFeedByLink(new Uri(feedLink));
}
public T GetFeedByLink(Uri feedLink)
{
return this._feeds.Where(feed => Uri.Equals(feed.Link, feedLink)).Single();
}
public int GetFeedIdByLink(string feedLink)
{
return this.GetFeedIdByLink(new Uri(feedLink));
}
public int GetFeedIdByLink(Uri feedLink)
{
return this._feeds.Where(feed => Uri.Equals(feed.Link, feedLink)).Select(feed => feed.Id).Single();
}
public void Remove(T feed)
{
this._feeds.Remove(feed);
this._db.Delete(feed);
}
public void RemoveById(int id)
{
this.Remove(this.GetFeedById(id));
}
public void Update(T feed)
{
int indexOfFeed = this._feeds.IndexOf(this.GetFeedById(feed.Id));
this._feeds[indexOfFeed] = feed;
this._db.Update(feed);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool isDisposing)
{
if (!this._isDisposed)
{
if (isDisposing)
{
if (this._db != null)
{
this._db.Dispose();
}
}
this._isDisposed = true;
}
}
}
IUnitOfWork:
internal interface IUnitOfWork
{
void Commit();
void Rollback();
}
UnitOfWork:
internal class UnitOfWork : IUnitOfWork, IDisposable
{
private readonly SQLiteConnection _db;
private FeedRepository<FeedData> _feedDataRepository;
private FeedRepository<FeedItem> _feedItemRepository;
private bool _isDisposed = false;
public FeedRepository<FeedData> FeedDataRepository
{
get
{
return _feedDataRepository;
}
set
{
_feedDataRepository = value;
}
}
public FeedRepository<FeedItem> FeedItemRepository
{
get
{
return _feedItemRepository;
}
set
{
_feedItemRepository = value;
}
}
public UnitOfWork(SQLiteConnection db)
{
this._db = db;
this._feedDataRepository = new FeedRepository<FeedData>(this._db);
this._feedItemRepository = new FeedRepository<FeedItem>(this._db);
this._db.BeginTransaction();
}
public void Commit()
{
this._db.Commit();
}
public void Rollback()
{
this._db.Rollback();
}
protected virtual void Dispose(bool isDisposing)
{
if (!this._isDisposed)
{
if (isDisposing)
{
if (this._db != null)
{
this._db.Dispose();
}
}
this._isDisposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Unfortunately, I still have no idea how to remove this._feeds = _db.Table<T>().ToList();
from the repository, because when I delete it, I can't use GetAll()
, GetFeedById()
and so on.
Edit
That's IBaseFeed:
public interface IBaseFeed
{
int Id { get; set; }
string Title { get; set; }
DateTime PubDate { get; set; }
Uri Link { get; set; }
string Misc { get; set; }
}
If I don't use IBaseFeed I won't take advantage of using:
public T GetFeedByLink(Uri feedLink)
{
return this._feeds.Where(feed => Uri.Equals(feed.Link, feedLink)).Single();
}
due to any of following properties such as 'Id' or 'Link'. That' why I'd rather use IRepository<T> where T : class
and IFeedRepository<T> where T : class, IBaseFeed
.
I have been writing an app for Windows 8.1 and unluckily EF is not available. The only wrapper for SQLite that I found is sqlite-net.
sqlite-net
doesn't contain method LastInsertId
so I have no idea how to return T
with the database-generated ID?
LastInsertId
method - it should just update your POCO automagically :) \$\endgroup\$