Created
May 23, 2014 01:33
-
-
Save melvinlee/080ac922acd1175e780c to your computer and use it in GitHub Desktop.
Nancy typed factory dependency injection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DbConnectionFactory : IDbConnectionFactory | |
{ | |
private readonly Func<IDbConnection> _dbConnection; | |
public DbConnectionFactory(Func<IDbConnection> dbConnection) | |
{ | |
_dbConnection = dbConnection; | |
} | |
public IDbConnection GetConnection() | |
{ | |
return _dbConnection(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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