Skip to content

Adding Custom Repositories

Long Le edited this page Sep 20, 2017 · 2 revisions

Update (1/1/2014): This feature as been decommissioned in v3.3.5, leaving document here for support for those that are on <= v3.3.4.

Example (Unit Test) on How to Add Custom Repositories

This allows you to access all Repositories in your application the same way, so that it's consistent for all Repositories not just for database (EF) ones.

[TestMethod]
public void GetCustomRepositoryFromOtherRepository()
{
    // Setup custom repositories e.g. IAzureBlobRepository
    var factories = new Dictionary<Type, Func<dynamic>>
    {
        {
            typeof (IAzureBlobRepository<MyBlob>),
            () => new AzureBlobRepository<MyBlob>()
        }
    };

    IRepositoryProvider repositoryProvider = 
        new RepositoryProvider(new RepositoryFactories(factories));

    using (IDataContextAsync context = new NorthwindFakeContext())
    using (IUnitOfWorkAsync uow = new UnitOfWork(context, repositoryProvider))
    {
        IRepositoryAsync<Product> productRepository = 
            new Repository<Product>(context, uow);

        // Retrieving custom repository from UnitOfWork instance
        var blobRepository1 = 
            uow.GetCustomRepository<IAzureBlobRepository<MyBlob>>();

        // Retrieving custom repository from another repository
        var blobRepository2 = 
            productRepository.GetCustomRepository<IAzureBlobRepository<MyBlob>>();

        Assert.AreEqual(blobRepository1.SayHello(), "Hello World!");
        Assert.AreEqual(blobRepository2.SayHello(), "Hello World!");
    }
}