Code Monkey home page Code Monkey logo

interview-lifetime-collection's Introduction

Lifetime Collection

Codacy Code Climate CodeFactor Codebeat BetterCode

Implementing a custom collection that tracks and manages the lifetime of every item added to it. I tried to perform this task as if it were part of some business task, at the same time, so that it would be convenient to work with the library as with some kind of NuGet package. Also, the task was to cover the code with tests, but due to a lack of knowledge in this direction, I postponed this certainly important part.

Example

using System.Threading.Tasks;
using LifetimeCollection.Application.LifetimeCollection.API;
using LifetimeCollection.Application.LifetimeCollection.API.Common.Interfaces;
using LifetimeCollection.Application.LifetimeCollection.API.Implementation.Core;
using LifetimeCollection.Domain.API.Entities;
using LifetimeCollection.Domain.API.Events.Lifetime;

namespace LifetimeCollection.Presentation.Console.Application
{
    internal class Program
    {
        private static async Task Main(string[] args)
        {
            //
            // 1.1. Create a collection of integer values.
            //
            ILifetimeCollection<int> ints = new LifetimeCollection<int>();

            //
            // 1.2. Subscribe to event handling.
            //
            ints.ItemAvailable += OnItemAvailable;
            ints.ItemUnavailable += OnItemUnavailable;

            //
            // 1.3. Adding values.
            //
            ints.Add(1);
            ints.Add(2);
            ints.Add(3);

            //
            // 1.4. Delay.
            //
            await Task.Delay(LifetimeCollectionOptions.LifetimeInSeconds * 1000);

            //
            // 1.5. We are trying to get the first element, which should already be inactive (due to a delay).
            //
            System.Console.WriteLine($"Get: {ints[0]}");

            //
            // 2.1. Create a collection with reference types.
            //
            ILifetimeCollection<Person> persons = new LifetimeCollection<Person>();

            //
            // 2.2. Subscribe to event handling.
            //
            persons.ItemAvailable += OnPersonAvailable;
            persons.ItemUnavailable += OnPersonUnavailable;

            //
            // 2.3. Adding values.
            //
            persons.Add(new Person {Id = 1, FirstName = "Barbara", LastName = "Smith"});
            persons.Add(new Person {Id = 2, FirstName = "Gerald", LastName = "Sears"});
            persons.Add(new Person {Id = 3, FirstName = "Vincenzo", LastName = "Osterman"});

            //
            // 2.4. We are trying to immediately read the information about the first person.
            //
            System.Console.WriteLine($"Get: {persons[0]}");

            //
            // 2.5. Delay.
            //
            await Task.Delay(LifetimeCollectionOptions.LifetimeInSeconds * 1000);

            //
            // 2.6. We are trying to get the first person, which should already be inactive (due to a delay).
            //
            System.Console.WriteLine($"Get: {persons[0]}");
        }

        // Event handling.

        private static void OnItemAvailable(object? sender, ItemAvailableEvent<int> e)
        {
            System.Console.WriteLine($"OnItemAvailable: {e.AvailableEntity.Entity}");
        }

        private static void OnItemUnavailable(object? sender, ItemUnavailableEvent<int> e)
        {
            System.Console.WriteLine($"OnItemUnavailable: {e.UnavailableEntity.Entity}");
        }

        private static void OnPersonUnavailable(object? sender, ItemUnavailableEvent<Person> e)
        {
            System.Console.WriteLine($"OnPersonUnavailable: {e.UnavailableEntity.Entity}");
        }

        private static void OnPersonAvailable(object? sender, ItemAvailableEvent<Person> e)
        {
            System.Console.WriteLine($"OnPersonAvailable: {e.AvailableEntity.Entity}");
        }
    }
}

Output

OnItemAvailable: 1
OnItemAvailable: 2
OnItemAvailable: 3
OnItemUnavailable: 1
Get: 0
OnPersonAvailable: (1) Barbara Smith
OnPersonAvailable: (2) Gerald Sears
OnPersonAvailable: (3) Vincenzo Osterman
Get: (1) Barbara Smith
OnPersonUnavailable: (1) Barbara Smith
Get: 

Status

Library Package
LifetimeCollection.Domain
LifetimeCollection.Application

License

This repository is licensed under Apache-2.0.

FOSSA Status

Copyright 2021 Maksym Liannoi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

interview-lifetime-collection's People

Contributors

liannoi avatar

Forkers

fossabot

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.