Over a million developers have joined DZone.

How to Use the Cell Design Pattern in C#

DZone's Guide to

How to Use the Cell Design Pattern in C#

Want to learn more about using the cell design pattern in C#? Check out this post about cell patterns using a biological systems example to learn more!

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

In an animal body, signals are sent from one area of the body to another through the endocrine system (and other systems). The hormonal transport system controls what happens if a cell performs a type of processing (often based on some form of input or trigger), and after the processing, it is able to emit a hormone. That hormone is transported in the endocrine system until it reaches a cell with a matching receptor. The cell with the matching receptor will receive the hormone and begin doing some processing of its own.

For me, the big difference in the computational ability of a biological system and a digital system is that a biological system is massively parallelizable. Each cell works completely independently of each other and all processing is fire and forget.

What Do I Expect This to Be Useful For?

This is a good question. This is definitely an exploratory project, and I don't have the answers yet. It seems however that it would be very good for any system that eventually would be split apart, because it very strictly enforces decoupling. In the future, this design could be expanded to include clusters of cells, comprising an organ that could be held in entirely different locations, and the ability to parallelize it would be built into the framework.

What Will This Look Like?

I have a working proof of concept here: https://github.com/cohen990/cell.

The example is converting from ASCII to base 64, and there are a few key concepts. Each cell is responsible for its own processing. It is triggered by a hormone, and after it has completed its work, it will emit another hormone that will propagate through the system.

    public class Base64EncodingCell : Cell<InputHormone>
        public override Action<InputHormone> GetHormonalResponse()
            return hormone => ProcessInput(hormone);

        public void ProcessInput(InputHormone hormone)
            String base64 = ConvertToBase64(hormone.Data);
            Emit(new ProcessingComplete(base64));

        String ConvertToBase64(String data){
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(data);
            return Convert.ToBase64String(bytes);

This is a full cell. The GetHormonalResponse method is on the base class and is used to register the cell with the endocrine system.

The behavior is maintained within each cell, because each cell can act independently on the hormones that are dispersed through the system. For example, you may wish to log the hormones that are being activated:

    public class HormoneLoggingCell : Cell<Hormone>
        public override Action<Hormone> GetHormonalResponse()
            return hormone => LogHormone(hormone);

        public void LogHormone(Hormone hormone)
            Console.WriteLine("Hormone Emitted: " + hormone.GetType());

Registration can take place dynamically or all at initialization time, but there is no way to de-register a cell.

    class Program
        static void Main()
            Console.Write("Input: ");
            var input = Console.ReadLine();

            var inputReceived = new InputReceived(input);


        static void RegisterCells()
            EndocrineSystem.RegisterBinding(new Base64EncodingCell());
            EndocrineSystem.RegisterBinding(new InputValidationCell());
            EndocrineSystem.RegisterBinding(new WriteOutputCell());
            EndocrineSystem.RegisterBinding(new HormoneLoggingCell());

The individual hormones are very anemic classes and are just used for data transfer and behavior triggering.

    public class InputHormone : Hormone
        public string Data { get; }

        public InputHormone(string data)
            Data = data;

This currently looks a lot like an event bus. Because, it's very similar. But, in the future, I believe this pattern could differentiate itself by taking influence from more concepts used in a bodily computation.

Anyways, that's it! That's the concept. Let me know if you thought this was interesting or if you have any ideas or suggestions!

This post was first published on the Codurance blog.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

tutorial ,cell design pattern ,design patterns ,bodily computation ,csharp

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}