patterncsharpMinor
Unit of Work (UoW) pattern with ADO.NET
Viewed 0 times
withadonetworkuowpatternunit
Problem
I'm trying to implement the UoW pattern using ADO.NET and this is what I've achieved so far:
The IUnitOfWork Interface:
The implementation for IUnitOfWork:
```
public class SqlUnitOfWork : IUnitOfWork
{
const string ConnectionStringName = "DefaultConnection";
private AdoNetContext _context;
private DomainTableRepository _domainTables;
private VariableRepository _variables;
private ModelRepository _models;
private StructureRepository _structures;
private SentenceRepository _sentences;
private ExpressionRepository _expressions;
private ReturnRepository _returns;
public SqlUnitOfWork()
{
var connectionString =
ConfigurationManager
.ConnectionStrings[ConnectionStringName]
.ConnectionString;
_context = new AdoNetContext(connectionString, true);
}
public IDomainTableRepository DomainTables
{
get
{
if (_domainTables == null)
{
_domainTables = new DomainTableRepository(_context);
}
return _domainTables;
}
}
//...getters for the remaining repositories
public void Commit()
{
_context.SaveChanges();
}
#region IDisposable Support
private bool disposedValue = false;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
The IUnitOfWork Interface:
public interface IUnitOfWork : IDisposable
{
IDomainTableRepository DomainTables { get; }
IVariableRepository Variables { get; }
IModelRepository Models { get; }
IStructureRepository Structures { get; }
ISentenceRepository Sentences { get; }
IExpressionRepository Expressions { get; }
IReturnRepository Returns { get; }
void Commit();
}The implementation for IUnitOfWork:
```
public class SqlUnitOfWork : IUnitOfWork
{
const string ConnectionStringName = "DefaultConnection";
private AdoNetContext _context;
private DomainTableRepository _domainTables;
private VariableRepository _variables;
private ModelRepository _models;
private StructureRepository _structures;
private SentenceRepository _sentences;
private ExpressionRepository _expressions;
private ReturnRepository _returns;
public SqlUnitOfWork()
{
var connectionString =
ConfigurationManager
.ConnectionStrings[ConnectionStringName]
.ConnectionString;
_context = new AdoNetContext(connectionString, true);
}
public IDomainTableRepository DomainTables
{
get
{
if (_domainTables == null)
{
_domainTables = new DomainTableRepository(_context);
}
return _domainTables;
}
}
//...getters for the remaining repositories
public void Commit()
{
_context.SaveChanges();
}
#region IDisposable Support
private bool disposedValue = false;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
Solution
you could collapse this piece of code into a single line
by using a Null Coalescing operator (
like this
unfortunately this isn't quite the same as what you have because it doesn't perform "Lazy Initialization" but the following code, that @Programmer gave in a comment, does so while also keeping with the spirit of using less indentation
I also noticed that in one instance you put the return statement outside of the using block but in the rest of the methods in that class you put the returns inside of the using blocks, while either will work you should stay consistent with the way that you write your code.
the code that I am talking about
and while I am here I would even place the Declaration of the variable inside of the using statement because there isn't anything about it (that I can tell) that would make it mandatory to declare it outside of the using statement
You could actually just return the results of the
public IDomainTableRepository DomainTables
{
get
{
if (_domainTables == null)
{
_domainTables = new DomainTableRepository(_context);
}
return _domainTables;
}
}by using a Null Coalescing operator (
??)like this
public IDomainTableRepository DomainTables
{
get
{
return _domainTables ?? new DomainTableRepository(_context);
}
}unfortunately this isn't quite the same as what you have because it doesn't perform "Lazy Initialization" but the following code, that @Programmer gave in a comment, does so while also keeping with the spirit of using less indentation
public IDomainTableRepository DomainTables =>
_domainTables ?? (_domainTables = new DomainTableRepository(_context));I also noticed that in one instance you put the return statement outside of the using block but in the rest of the methods in that class you put the returns inside of the using blocks, while either will work you should stay consistent with the way that you write your code.
the code that I am talking about
public DomainTable Get(int id)
{
DomainTable table;
using (var commandTable = _context.CreateCommand())
{
commandTable.CommandType = CommandType.StoredProcedure;
commandTable.CommandText = "up_DomainTable_GetById";
commandTable.Parameters.Add(commandTable.CreateParameter("@pId", id));
table = ToList(commandTable).FirstOrDefault();
}
return table;
}and while I am here I would even place the Declaration of the variable inside of the using statement because there isn't anything about it (that I can tell) that would make it mandatory to declare it outside of the using statement
public DomainTable Get(int id)
{
using (var commandTable = _context.CreateCommand())
{
DomainTable table;
commandTable.CommandType = CommandType.StoredProcedure;
commandTable.CommandText = "up_DomainTable_GetById";
commandTable.Parameters.Add(commandTable.CreateParameter("@pId", id));
table = ToList(commandTable).FirstOrDefault();
return table;
}
}You could actually just return the results of the
ToList Method call like this:public DomainTable Get(int id)
{
using (var commandTable = _context.CreateCommand())
{
commandTable.CommandType = CommandType.StoredProcedure;
commandTable.CommandText = "up_DomainTable_GetById";
commandTable.Parameters.Add(commandTable.CreateParameter("@pId", id));
return ToList(commandTable).FirstOrDefault();
}
}Code Snippets
public IDomainTableRepository DomainTables
{
get
{
if (_domainTables == null)
{
_domainTables = new DomainTableRepository(_context);
}
return _domainTables;
}
}public IDomainTableRepository DomainTables
{
get
{
return _domainTables ?? new DomainTableRepository(_context);
}
}public IDomainTableRepository DomainTables =>
_domainTables ?? (_domainTables = new DomainTableRepository(_context));public DomainTable Get(int id)
{
DomainTable table;
using (var commandTable = _context.CreateCommand())
{
commandTable.CommandType = CommandType.StoredProcedure;
commandTable.CommandText = "up_DomainTable_GetById";
commandTable.Parameters.Add(commandTable.CreateParameter("@pId", id));
table = ToList(commandTable).FirstOrDefault();
}
return table;
}public DomainTable Get(int id)
{
using (var commandTable = _context.CreateCommand())
{
DomainTable table;
commandTable.CommandType = CommandType.StoredProcedure;
commandTable.CommandText = "up_DomainTable_GetById";
commandTable.Parameters.Add(commandTable.CreateParameter("@pId", id));
table = ToList(commandTable).FirstOrDefault();
return table;
}
}Context
StackExchange Code Review Q#144784, answer score: 3
Revisions (0)
No revisions yet.