Coder Social home page Coder Social logo

ambientcontext.dotnet's Introduction

AmbientContext

Implementation of the base ambient context class from which others can be derived

Implementing

Derive from AmbientService with the desired generic type parameter. Create a wrapper for the class being turned into an AmbientService to allow substitution of a mock/fake during testing.

A simple implementation for an AmbientDateTimeService might look like this

public class AmbientDateTimeService : AmbientService<IDateTime>, IDateTime
{
    protected override IDateTime DefaultCreate()
    {
        return new DateTimeAdapter();
    }

    public DateTime Now => Instance.Now;
    public DateTime UtcNow => Instance.UtcNow;
    public DateTime Today => Instance.Today;
}

and the adapter class might look like this

public class DateTimeAdapter : IDateTime
{
    public DateTime Now => DateTime.Now;
    public DateTime UtcNow => DateTime.UtcNow;
    public DateTime Today => DateTime.Today;
}

Default Instances

If the derived service has an acceptable default, implement this by overriding the protected virtual DefaultCreate method.

If there is no valid default, this means the user must supply the Create delegate in the composition root.

Create Delegate

Providing the static create delegate in the composition root means the first time the ambient service is used, this delegate will supply the instance to be used internally.

If the AmbientDateTimeService above did not have a default create implementation, or if you simply wanted to override the existing default, then to set it in the composition root one would do

AmbientDateTimeService.Create = () => new AlternativeDateTimeAdapter();

Usage

To use an AmbientService implementation, simply create a new instance and assign it to a read only property on the class.

public readonly AmbientDateTimeService DateTime = new AmbientDateTimeService();

Since the AmbientDateTimeService will also implement theIDateTime interface, you can use it directly and it will act as a proxy to the underlying instance of the wrapper that was created which in turn calls the .NET BCL DateTime static directly.

Testing

An AmbientService can be mocked for testing by setting the Instance property to a suitable mock/fake implementation.

sut.DateTime.Instance = new Mock<IDateTime>();

ambientcontext.dotnet's People

Contributors

nrjohnstone avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.