Created
September 3, 2012 17:43
-
-
Save davybrion/3611259 to your computer and use it in GitHub Desktop.
code snippets for "Introduction To Dependency Injection" post
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 SqlMetaDataProvider : IMetaDataProvider | |
{ | |
private readonly string _connectionString; | |
private readonly SqlDataRetriever _sqlDataRetriever; | |
public SqlMetaDataProvider(string connectionString) | |
{ | |
_connectionString = connectionString; | |
_sqlDataRetriever = new SqlDataRetriever(); | |
} | |
public MetaDataStore GetMetaDataStore() | |
{ | |
SqlMetaData sqlMetaData = _sqlDataRetriever.GetMetaData(_connectionString); | |
return ConvertToMetaDataStore(sqlMetaData); | |
} | |
private MetaDataStore ConvertToMetaDataStore(SqlMetaData sqlMetaData) | |
{ | |
MetaDataStore store = new MetaDataStore(); | |
AddTablesToStore(sqlMetaData.TableInfo, store); | |
AddColumnsToTablesInStore(sqlMetaData.ColumnInfo, store); | |
CreateRelationshipsBetweenTables(sqlMetaData.RelationshipInfo, store); | |
return store; | |
} | |
} |
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 interface ISqlDataRetriever | |
{ | |
SqlMetaData GetMetaData(string connectionString); | |
} |
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 SqlDataRetriever : ISqlDataRetriever |
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
private readonly ISqlDataRetriever _sqlDataRetriever; |
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 SqlMetaDataProvider(string connectionString, ISqlDataRetriever sqlDataRetriever) | |
{ | |
_connectionString = connectionString; | |
_sqlDataRetriever = sqlDataRetriever; | |
} |
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 SqlMetaDataProvider(string connectionString) | |
: this(connectionString, new SqlDataRetriever()) {} |
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 SqlDataProviderStub : ISqlDataRetriever | |
{ | |
private SqlMetaData _sqlMetaData; | |
public SqlMetaData SqlMetaData | |
{ | |
set { _sqlMetaData = value; } | |
} | |
SqlMetaData ISqlDataRetriever.GetMetaData(string connectionString) | |
{ | |
return _sqlMetaData; | |
} | |
} |
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
[Test] | |
public void GetMetaDataStore_ProvideDataSetWithTwoTablesAndRelationship_MetaDataStoreIsCorrect() | |
{ | |
SqlMetaData sqlMetaData = PrepareMetaDataSetInMemoryWithTestData(); | |
SqlDataProviderStub sqlDataProvider = new SqlDataProviderStub(); | |
sqlDataProvider.SqlMetaData = sqlMetaData; | |
// pass null as the connectionString, and pass our SqlDataProviderStub | |
IMetaDataProvider metaDataProvider = new SqlMetaDataProvider(null, sqlDataProvider); | |
MetaDataStore store = metaDataProvider.GetMetaDataStore(); | |
AssertStoreContainsOurTestData(store); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment