Separation of Concerns in .NET

Presented by Raman But-Husaim

Agenda

  • Who is this guy?
  • SoC overview
  • Decorators
  • Proxies & Interceptors
  • Q&A

Who is this Guy?

  • Raman But-Husaim
  • 11+ years with code
  • 6+ years in production
  • Master's Degree in CS
  • Tech Geek

Separation of Concerns

Separation of Concerns

A design principle for separating a computer program into distinct sections, so that each section addresses a separate concern.

Separation of Concerns

API

logic

+ modularity;

+ boundaries;

+ encapsulation;

It's all about

Separation of Concerns

functions, methods, classes

modules, projects

individuals

teams

services, applications, features

units, sub-organisations

Not only CS

Separation of Concerns

public class Math {
    public int Abs(...) {
        ...
    }
    
    public double Pow(...) {
        ...
    }
}

UI

Business

DAL

Examples

Separation of Concerns

more maintainable

more stable

more extensible

more reusable

more marketable

Profit

Separation of Concerns

Aspect

Oriented

Programming

(AOP)

AOP

Separation of Concerns

AOP

A programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns.

@wikipedia

Separation of Concerns

AOP (cross-cutting concerns)

Security

Logging

Monitoring

Decorators

Decorators

Allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class.

@wikipedia

Decorators

A decorator pattern is one of the best ways to add features to an object without changing its interface.

@yegor256

Decorators

Problem

extend functionality of a class, module, etc. at run-time

need flexible alternative to subclassing

Decorators

Example

Decorators

Code (Design)

public interface IMessageFormatter {
    string Format(Message message);
}

public class MessageFormatter : IMessageFormatter {
    private readonly IJsonSerializer serializer;

    ...
    
    public string Format(Message message) {
        return serializer.Serialize(message);
    }
}
public class AnonymizingMessageFormatter : IMessageFormatter {
    private readonly IMessageFormatter inner;

    ...
    
    public string Format(Message message) {
        var anonymizedMessage = this.Anonymize(message);
        
        return this.inner.Format(anonymizedMessage);
    }
    
    private Message Anonymize(Message message) {
    // some code to perform anonymization
    }
}

Decorators

<Live Coding>

Proxies & Interceptors

Interceptors

Pattern that is used when software systems or frameworks want to offer a way to change, or augment, their usual processing cycle.

@wikipedia

Interceptors

Schema (before)

component A

component B

API

Interceptors

Schema (after)

component A

component B

API

component C

<interceptor>

Interceptors

Key Aspects

  • change is transparent;
  • used automatically;
  • presence of predefined interface;
  • dispatch mechanism for interceptors;
  • context object to communicate with framework;
public interface IInterceptor {
    void Intercept(IInvocation inv);
}

Proxies

A proxy, in its most general form, is a class functioning as an interface to another thing.

@wikipedia

Proxies

Problem

control the access to an object, eg.

  • sensitive
  • expensive
  • remote

additional functionality when accessing the object

Proxies

Example (WCF)

3rd party service

API

autogenerated

client-proxy

<your code here>

Castle DynamicProxy

A library for generating lightweight proxies at runtime. Proxy objects allow calls to members of an object to be intercepted without modifying the code of the class.

@official docs

Castle DynamicProxy

Proxy + Interceptor =

Real Object

interceptor 1

interceptor 2

interceptor n

interceptor 3

Proxy

Real World

Castle DynamicProxy

Proxies

Interceptors

  • code generation in runtime;
  • controlled mostly* by the library itself;
  • provide extensibility points for consumers (us);
  • poor documentation;
  • created solely by the consumers (us);
  • utilize extensibility points to control proxy behaviour;
  • quite straightforward API*;

Q&A

Useful Links

Useful Links

Separation of Concerns

Useful Links

Decorators

Useful Links

Proxies & Interceptors

Useful Links

Castle DynamicProxy

Useful Links

Images

Separation of Concerns in .NET

By Raman But-Husaim

Separation of Concerns in .NET

Discuss what is SoC and different ways to use this core principle in dotnet applications - decorators and interceptors.

  • 570