HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Sharing domain between ASP.NET and self running application

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
applicationselfnetbetweenrunninganddomainaspsharing

Problem

I'm building pretty big application and I wonder how should I share the code between asp.net mvc website and standalone (self running) server application.
I've got some idea how I should do this but I'm not sure if I'm doing this right.
I've got Domain Entities, Services and Repositories that will be shared between asp.mvc app and server app. Services will perform transactions on multiple repositories using IDbTransaction (I can't use ORM, pure ado.net and mysql).

IDbContext.cs

public interface IDbContext : IDisposable
{
    IUnitOfWork CreateUnitOfWork();
}


IDbContextFactory.cs

public interface IDbContextFactory
{
    IDbContext Create();
}


IRepositoryFactory.cs

public interface IRepositoryFactory
{
    T GetRepository(IDbContext context, IUnitOfWork unitOfWork = null)
        where T : class;
}


IUnitOfWork.cs

public interface IUnitOfWork : IDisposable
    {
        bool IsInTransaction { get; }
        void BeginTransaction();
        void BeginTransaction(IsolationLevel isolation);
        void CommitTransaction();
        void RollbackTransaction();
    }


Now the MySql implementation

MysqlDbContext.cs

```
public class MysqlDbContext : IDbContext
{
#region Fields
private MySqlConnection _connection;
private bool _disposed;

#endregion

#region Constructor

public MysqlDbContext(MysqlConnectionFactory connectionFactory)
{
//Todo: create and open the connection
_connection = connectionFactory.Create();
if (_connection.State != ConnectionState.Open)
_connection.Open();
}

#endregion

public MySqlConnection Connection
{
get { return _connection; }
}

public IUnitOfWork CreateUnitOfWork()
{
return new MysqlUnitOfWork(_connection);
}

public void Dispose()
{
Dispose(true);
}

public v

Solution

Instead of an ApplicationException, your application would be better off throwing an InvalidOperationException, since that actually tells someone who uses your library a bit more about the problem.

Why do you even have a variable _disposed if it is only ever assigned to once as soon as the objects do get disposed? It is never read from.

MysqlRepositoryFactory is not specific to your MySQL implementation at all - just call it your RepositoryFactory. Apart from that, GetRepository should not work in the current version, because it is limited to classes?

Those nitpicks aside, I would argue that the responsibility of creating, committing and disposing the unit of work should be somewhere else. The way your design works, you could not ever use the same unit of work (and transaction) for two or more service calls. Furthermore, you would end up creating a lot of duplicate code if you do that for every public method of every service.
One usual way of solving that with ASP.NET is to create the unit of work once for every ASP.NET request, inject it into the services/repositories directly, and commit it in Application_EndRequest. In a Windows service or WinForms app, you could still scope the unit of work for every logical action in - which might still span over more than one service call.

Context

StackExchange Code Review Q#37266, answer score: 6

Revisions (0)

No revisions yet.