vendredi 1 juillet 2016

Autofac with DocumentDB

I'm trying to use Autofac within my application to create a JSON Document on DocumentDB. This is how it's set out:

In this part i do not know how to call CreateDocumentUri in my test assembly to create a Uri reference which will contain the databaseId and collectionId to be used in StoreDocumentAsync.

AzureDocumentDbService:

public class AzureDocumentDbService : INoSQLProvider
    {
        private readonly IDocumentClient client;

    public AzureDocumentDbService(IDocumentClient client)
    {
        this.client = client;
    }

    public Uri CreateDocumentUri(string databaseId, string collectionId)
    {
        return UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);
    }

    public async Task<dynamic> StoreDocumentAsync(Uri reference, object doc)
    {
        return await client.CreateDocumentAsync(reference, doc);
    }

    public async Task<dynamic> ReadDocAsync(Uri reference)
    {
        return await client.ReadDocumentAsync(reference);
    }
}

INoSWQProvider interface:

public interface INoSQLProvider
    {
        Task<dynamic> StoreDocumentAsync(Uri reference, object doc);

    Task<dynamic> ReadDocAsync(Uri reference);
    //public abstract GetDocument();
    //public abstract UpdateDocument();
    //public abstract DeleteDocument();
}

NoSQLConfiguration:

 public class NoSQLConfiguration
    {
        public string AuthKey { get; set; }
        public string EndpointUri { get; set; }
    }

NoSQLService:

Store Document is what will be called within my webapp.

public class NoSQLService
    {
        private INoSQLProvider client;
        private static IContainer container;



    public NoSQLService(INoSQLProvider provider)
    {
        client = provider;
    }

    public static NoSQLService GetInstance(NoSQLConfiguration config)
    {
        AutofaConfiguration(config);
        var service = container.Resolve<NoSQLService>();
        return service;
    }

    private static void AutofaConfiguration(NoSQLConfiguration config)
    {
        // TODO think of options that the calling app might want to set which will override some
        // of these settings
        var builder = new ContainerBuilder();

        builder.RegisterType<NoSQLService>().AsSelf();
        builder.Register(t => new DocumentClient(new Uri(config.EndpointUri), config.AuthKey)).As<IDocumentClient>();
        builder.RegisterType<AzureDocumentDbService>().As<INoSQLProvider>();
        container = builder.Build();
    }

    public static void StoreDocument(Uri reference, object document)
    {
        // Call client, work out document type or how to get id from it and then return id to caller
    }
}

In my test assembly i create a mock Json document. the problem is i cannot create a DocumentUri reference to be used in StoreDocument in NOSQLService. Here's what the test assembly looks like this:

[TestClass]
    public class AzureDocumentDbTests
    {
        [TestMethod]
        public void TestStoreDocument()
        {
            var config = new NoSQLConfiguration()
            {
                EndpointUri = "<EndpointUri here>",
                AuthKey = "<AuthKey Here>"
            };
            NoSQLService.GetInstance(config);

            Documents document = new Documents();

            document.Id = "1";
            document.Property1 = "Property1";
            document.Property2 = "Property2";

            object output = JsonConvert.SerializeObject(document);



            string databaseId = "DatabaseId";

            string collectionId = "CollectionId";



            NoSQLService.StoreDocument(reference, document);

            Assert.IsTrue(document.Id.Contains("1"), "json document should contain an id");
            Assert.IsTrue(document.Property1.Contains("Property1"), "json document should contain an Property1");
            Assert.IsTrue(document.Property2.Contains("Property2"), "json document should contain an Property2");
            Assert.IsTrue(databaseId.Contains("PPsession1"), "Database ID should contain correct database ID.");
            Assert.IsTrue(collectionId.Contains("CallumTest"), "Collection ID should contain correct collection ID");

        }
    }

Aucun commentaire:

Enregistrer un commentaire