Giter VIP home page Giter VIP logo

marcusts / com.marcusts.smartdi Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 9.9 MB

The MAUI READY Smart Di Container is a tiny, powerful Inversion of Control library. Instantiate classes using RegisterAndResolve, a new extension that both registers and creates a class at the same time. Smart DI auto-creates all constructor parameters and even protects against recursion in those parameters. SmartDI can be declared privately and can be nested -- no more global bootstrapping! Smart DI provides a proven "lifetime scope" for stored class variables so they can be automatically removed when their parents are disposed.

Home Page: https://www.nuget.org/packages/Com.MarcusTS.SmartDI/

License: MIT License

C# 100.00%
di-container ioc-container dependency-injection c-sharp maui

com.marcusts.smartdi's Introduction

NOW MAUI READY !!!

Finally, a DI Container Without The Fake "IOC" Swagger

It's just dependency injection - that's all, folks

Modern programmers require some means of creating new classes out of other classes that are passed as parameters, and to do so in an elegant, seamless manner. Those new instances often require caching, especially if they are services or view models. The got-to solution for this challenge has been the mis-named and grossly mis-described "IOC Container". If we can't even name something accurately, it is time to reconsider it entirely. So I have done that here with the tool that all of us actually need: a DI ("Dependency Injection") Container.

What's So Smart About It?

IOC containers always store an instance when you create it. That is extremely wasteful. The Smart DI Container provides only three types of registrations:

1. Any Access Level / Do Not Store

Use these to create instances at any time and without caching. The local variable you retrieve on Resolve() is the only stored reference.

2. Shared Between Instances

When you Resolve() using this access level, you must to pass in a "parent" object that gets indexed to that new class instance. You can also link the same instance to any number of other consumers/parents by calling Resolve() again. Once all of the parents die, the cached instance is also removed.

Note: This requires you to raise the ObjectDisappearingMessage to notify the container about the death of a shared instance parent. That message is declared in our Shared Forms Library, which you can import from Nuget.

We can do this for you if you also use our a few other simple libraries:

LifecycleAware. NuGet.

SmartDI.LifecycleAware. NuGet.

There's also a handy sample app here and on Nuget.

In spite of the ginormous size of other containers on the market, none of them can pass this test. The container must provide a physical mechanism to make this functionality possible. We have one!

3. Global Singleton

The container creates and caches a permanent instance of any type registered with this access level. The cached reference dies when the container itself falls out of scope.

The Smart DI Container is Not inherently global or static

You can declare an instance of the Smart DI Container wherever you please. This supports "nested" scenarios, where containers live within narrowly defined class inheritance trees. Remember: all "global" variables stored/cached will only live as long as the container does.

The Smart DI Container is well-behaved

This container protects against recursive calls, or any other violation of the rules-based registrations you make. For instance, if you register two competing interfaces for the same base type:

_container = new SmartDIContainer();
_container.RegisterTypeAsInterface<FirstSimpleClass>(typeof(IAmSimple));
_container.RegisterTypeAsInterface<SecondSimpleClass>(typeof(IAmSimple));

... and then resolve IAmSimple, you have created a conflict. The container cannot know which one to return. You can set a Boolean property to throw an error in this case. Or you can provide a conflict resolver:

var simple = 
   _container.Resolve<IAmSimple>(
      StorageRules.AnyAccessLevel, null, ForbidSpecificClass<FirstSimpleClass>);

private static IConflictResolution ForbidSpecificClass<T>(
   IDictionary<Type, 
   ITimeStampedCreatorAndStorageRules> registrations)
{
   // Find any registration where the key 
   //    (the main class that was registered and that is being constructed) 
   //    is *not* the forbidden one
   var legalValues = registrations.Where(r => r.Key != typeof(T)).ToArray();

   if (legalValues.IsEmpty())
   {
      return null;
   }

   return 
      new ConflictResolution
         {
            MasterType                = legalValues.First().Key,
            TypeToCastWithStorageRule = 
               legalValues.First().Value.CreatorsAndStorageRules.First()
         };
   }
}

The Smart DI Container is tiny

It occupies almost no space at all, and rarely touches memory, since it does not store anything unnecessarily.

The Smart DI Container is open source C#, and easy to read.

We actually added comments! (And we were not struck by lightning)

The Smart DI Container is tested and proven

See the unit tests.

Quick Start

Please also refer to the Quick Start Guide.

The Smart DI Container Is Open Source; Enjoy Our Entire Public Suite

Shared Utils (MAUI Ready!)

GutHub

NuGet

The Smart DI Container (MAUI Ready!)

GutHub

NuGet

Responsive Tasks (MAUI Ready!)

GutHub

NuGet

PlatformIndependentShared (MAUI Ready!)

GutHub

NuGet

UI.XamForms

GutHub

NuGet

The Modern App Demo

GutHub

ย 

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.