Giter Site home page Giter Site logo

autofac-interception's Introduction

Autofac-Interception

Build status

This repo contains sample project that implements cross cutting concerns in .Net core application using interception feature of Autofac

Configure interceptors (ex: logging ) in IoC Container

builder.Register(x => new LogInterceptor());

Configure interception for types in IoC Container

builder.RegisterType<BarService>().As<IBarService>().EnableInterfaceInterceptors();
builder.RegisterType<FooService>().As<IFooService>().EnableInterfaceInterceptors();

Types free of cross cutting concerns (notice no logging calls in these methods)

[Intercept(typeof(LogInterceptor))]
public class FooService : IFooService
{
    ...
    public double CalculateData(int minValue, int maxValue, int someOtherParam)
    {
        //Equivalent to getting data from back-end service
        var data = _barService.GetData(minValue, maxValue);

        //Equivalent to computing result based on business logic 
        var result = (double) data / someOtherParam;

        return result;
    }
}
public class BarService : IBarService
{
    ...
    public int GetData(int minValue, int maxValue)
    {
        return _randomNumberGenerator.Next(minValue, maxValue);
    }
}

Application Execution

var fooService = container.Resolve<IFooService>();
var result = fooService.CalculateData(30, 70, 10);

Console.WriteLine($"Value of result is: {result}");

Application Output (method calls logged by interceptor)

LogInterceptor::AutoFac_Interception.FooService.CalculateData() - called with 30,70,10
LogInterceptor::AutoFac_Interception.BarService.GetData() - called with 30,70
LogInterceptor::AutoFac_Interception.BarService.GetData() - returned 43
LogInterceptor::AutoFac_Interception.FooService.CalculateData() - returned 4.3
Value of result is: 4.3

autofac-interception's People

Contributors

hitenpatel01 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.