Skip to content

Instantly share code, notes, and snippets.

@melvinlee
Created May 23, 2014 01:33
Show Gist options
  • Save melvinlee/080ac922acd1175e780c to your computer and use it in GitHub Desktop.
Save melvinlee/080ac922acd1175e780c to your computer and use it in GitHub Desktop.
Nancy typed factory dependency injection.
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
var factory = new DbConnectionFactory(() => new SQLiteConnection("Data Source=dbname.db3;Version=3"));
container.Register<IDbConnection>(factory.GetConnection());
container.Register<UserRepository>();
}
}
public class DbConnectionFactory : IDbConnectionFactory
{
private readonly Func<IDbConnection> _dbConnection;
public DbConnectionFactory(Func<IDbConnection> dbConnection)
{
_dbConnection = dbConnection;
}
public IDbConnection GetConnection()
{
return _dbConnection();
}
}
public class UserRepository
{
private readonly IDbConnection _connection;
public UserRepository(IDbConnection connection)
{
_connection = connection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment