Autumn  Blick

Autumn Blick

1596758220

Using Abstract Classes As Dependency-Injection Tokens

sing Abstract Classes As Dependency-Injection Tokens With “providedIn” Semantics In Angular 9.1.9

By Ben Nadel on May 29, 2020

Tags: Javascript / DHTML

Earlier this week, I looked at saving temporary form-data to the [SessionStorage](https://www.bennadel.com/blog/3834-saving-temporary-form-data-to-the-sessionstorage-api-in-angular-9-1-9.htm) API in Angular 9 so that a user wouldn’t lose their form-data if they accidentally refreshed the page. In that exploration, I had a Storage class that used the SessionStorage API internally in a hard-coded fashion. But, I wanted to take a look at how I could make that behavior more dynamic using Dependency-Injection (DI) tokens. A few years ago, I wrote about using abstract classes as DI tokens in Angular 4; however, with the new providedIn @Injectable decorator, I didn’t know how to do this. That is, until I came across a post on tree-shakeable providers by Manfred Steyer. What follows in my attempt to take Manfred’s insights and translate them over to my temporary storage exploration in Angular 9.1.9.

Run this demo in my JavaScript Demos project on GitHub.

View this code in my JavaScript Demos project on GitHub.

A couple of versions ago, Angular introduced a new @Injectable() decorator semantic that would allow us to “provide” Services within our Angular application without having to explicitly define those services in the @NgModule decorators:

@Injectable({
		providedIn: "root"
	})
	export class MyServiceClass { 
		// ....
	}

I couldn’t really tell you what this providedIn:"root" concept is doing mechanically; I can only tell you that it magically adds the given Service to the Dependency-Injection (DI) container. This allows other classes to then inject this Service using the traditional type-based annotations:

import { MyServiceClass } from "./my-service";

	@Injectable({
		providedIn: "root"
	})
	export class SomeConsumerClass { 
		constructor( myService: MyServiceClass ) {
			// ....
		}
	}

Now, getting back to the idea of using an abstract class as a dependency-injection token, I wasn’t sure how to turn the MyServiceClass reference into an abstract class that concrete implementations could implement. I figured that I could always go and add the default, concrete implementation to the @NgModule decorator; however, that would defeat the ease-of-use that the providedIn syntax is bringing to the table.

It wasn’t until I read Manfred’s blog post that I even realized that the @Injectable() decorator has more than just the providedIn key. In fact, it allows for all the same “use” options that we can have in the @NgModule providers:

  • useClass
  • useValue
  • useFactory
  • useExisting

ASIDE: The _documentation for the _[_@Injectable()_](https://angular.io/api/core/Injectable)_ decorator_ lists _providedIn_ as the only option, which is definitely part of my confusion on the matter.

With this new insight, we can expose an abstract class as a dependency-injection token and then use the useClass option to tell it which concrete implementation to use as the default provider.

Circling back to my temporary storage demo, I can now create a TemporaryStorageService class that is abstract, provides a default, concrete implementation, and acts as a dependency-injection token in the Angular application:

// Import the core angular services.
	import { forwardRef } from "@angular/core";
	import { Injectable } from "@angular/core";

	// ----------------------------------------------------------------------------------- //
	// ----------------------------------------------------------------------------------- //

	// By using an ABSTRACT CLASS as the dependency-injection (DI) token, it allows us to
	// use the class as both the token and as an INTERFACE that the concrete classes have to
	// implement. And, by including the "useClass" property in our decorator, it allows us
	// to define the DEFAULT IMPLEMENTATION to be used with this injection token.
	@Injectable({
		providedIn: "root",
		useClass: forwardRef( () => SessionStorageService ) // Default implementation.
	})
	export abstract class TemporaryStorageService {
		public abstract get<T>( key: string ) : Promise<T | null>;
		public abstract remove( key: string ) : void;
		public abstract set( key: string, value: any ) : void;
	}

Here, we’re using the abstract class, TemporaryStorageService, as both the DI token and the Interface for the concrete implementations. We’re then using the useClass option to tell the Angular Injector to provide the SessionStorageService class as the default implementation for said DI token.

NOTE: I’m using the _forwardRef()_ function here because the _SessionStorageService_ class is defined lower-down in the code-file. I could have swapped the order of the definitions and used the class reference directly; but, I wanted to the code-file to read top-to-bottom.

Now, from my other Services and Components, I can inject the TemporaryStorageService token and remain blissfully ignorant of whichever implementation is actually being provided. To see this in action, I’ve created an App component that injects the TemporaryStorageService token and receives one of the following three concrete implementations:

  • SessionStorageService
  • LocalStorageService
  • InMemoryStorageService

#javascript / dhtml #angular

What is GEEK

Buddha Community

Using Abstract Classes As Dependency-Injection Tokens

Abstract class & Abstract method in C# | OOP in C# Part-24

https://youtu.be/vonwoaUl0Hw

#oop #abstract #abstract method #abstract class #what is abstract

Yashi Tyagi

1617449307

CA Classes - Best CA Classes Online

Chartered Accountancy course requires mental focus & discipline, coaching for CA Foundation, CA Inter and CA Finals are omnipresent, and some of the best faculty’s classes have moved online, in this blog, we are going to give the best way to find online videos lectures, various online websites provide the CA lectures, Smartnstudy one of the best site to CA preparation, here all faculty’s video lecture available.

check here : ca classes

#ca classes online #ca classes in delhi #ca classes app #ca pendrive classes #ca google drive classes #best ca classes online

Why Use WordPress? What Can You Do With WordPress?

Can you use WordPress for anything other than blogging? To your surprise, yes. WordPress is more than just a blogging tool, and it has helped thousands of websites and web applications to thrive. The use of WordPress powers around 40% of online projects, and today in our blog, we would visit some amazing uses of WordPress other than blogging.
What Is The Use Of WordPress?

WordPress is the most popular website platform in the world. It is the first choice of businesses that want to set a feature-rich and dynamic Content Management System. So, if you ask what WordPress is used for, the answer is – everything. It is a super-flexible, feature-rich and secure platform that offers everything to build unique websites and applications. Let’s start knowing them:

1. Multiple Websites Under A Single Installation
WordPress Multisite allows you to develop multiple sites from a single WordPress installation. You can download WordPress and start building websites you want to launch under a single server. Literally speaking, you can handle hundreds of sites from one single dashboard, which now needs applause.
It is a highly efficient platform that allows you to easily run several websites under the same login credentials. One of the best things about WordPress is the themes it has to offer. You can simply download them and plugin for various sites and save space on sites without losing their speed.

2. WordPress Social Network
WordPress can be used for high-end projects such as Social Media Network. If you don’t have the money and patience to hire a coder and invest months in building a feature-rich social media site, go for WordPress. It is one of the most amazing uses of WordPress. Its stunning CMS is unbeatable. And you can build sites as good as Facebook or Reddit etc. It can just make the process a lot easier.
To set up a social media network, you would have to download a WordPress Plugin called BuddyPress. It would allow you to connect a community page with ease and would provide all the necessary features of a community or social media. It has direct messaging, activity stream, user groups, extended profiles, and so much more. You just have to download and configure it.
If BuddyPress doesn’t meet all your needs, don’t give up on your dreams. You can try out WP Symposium or PeepSo. There are also several themes you can use to build a social network.

3. Create A Forum For Your Brand’s Community
Communities are very important for your business. They help you stay in constant connection with your users and consumers. And allow you to turn them into a loyal customer base. Meanwhile, there are many good technologies that can be used for building a community page – the good old WordPress is still the best.
It is the best community development technology. If you want to build your online community, you need to consider all the amazing features you get with WordPress. Plugins such as BB Press is an open-source, template-driven PHP/ MySQL forum software. It is very simple and doesn’t hamper the experience of the website.
Other tools such as wpFoRo and Asgaros Forum are equally good for creating a community blog. They are lightweight tools that are easy to manage and integrate with your WordPress site easily. However, there is only one tiny problem; you need to have some technical knowledge to build a WordPress Community blog page.

4. Shortcodes
Since we gave you a problem in the previous section, we would also give you a perfect solution for it. You might not know to code, but you have shortcodes. Shortcodes help you execute functions without having to code. It is an easy way to build an amazing website, add new features, customize plugins easily. They are short lines of code, and rather than memorizing multiple lines; you can have zero technical knowledge and start building a feature-rich website or application.
There are also plugins like Shortcoder, Shortcodes Ultimate, and the Basics available on WordPress that can be used, and you would not even have to remember the shortcodes.

5. Build Online Stores
If you still think about why to use WordPress, use it to build an online store. You can start selling your goods online and start selling. It is an affordable technology that helps you build a feature-rich eCommerce store with WordPress.
WooCommerce is an extension of WordPress and is one of the most used eCommerce solutions. WooCommerce holds a 28% share of the global market and is one of the best ways to set up an online store. It allows you to build user-friendly and professional online stores and has thousands of free and paid extensions. Moreover as an open-source platform, and you don’t have to pay for the license.
Apart from WooCommerce, there are Easy Digital Downloads, iThemes Exchange, Shopify eCommerce plugin, and so much more available.

6. Security Features
WordPress takes security very seriously. It offers tons of external solutions that help you in safeguarding your WordPress site. While there is no way to ensure 100% security, it provides regular updates with security patches and provides several plugins to help with backups, two-factor authorization, and more.
By choosing hosting providers like WP Engine, you can improve the security of the website. It helps in threat detection, manage patching and updates, and internal security audits for the customers, and so much more.

Read More

#use of wordpress #use wordpress for business website #use wordpress for website #what is use of wordpress #why use wordpress #why use wordpress to build a website

Getting Started with ASP.NET Core Dependency Injection

I’ve lately been feeling my way around getting an actual, production-ready ASP.NET Core app developed, and one of the features I’ve really been digging (one of many) is native support for Dependency Injection (DI). DI is huge in modern web apps, so making it the default for ASP.NET

#asp.net core #asp.net core dependency injection #dependency #injection #programming

aaron silva

aaron silva

1622197808

SafeMoon Clone | Create A DeFi Token Like SafeMoon | DeFi token like SafeMoon

SafeMoon is a decentralized finance (DeFi) token. This token consists of RFI tokenomics and auto-liquidity generating protocol. A DeFi token like SafeMoon has reached the mainstream standards under the Binance Smart Chain. Its success and popularity have been immense, thus, making the majority of the business firms adopt this style of cryptocurrency as an alternative.

A DeFi token like SafeMoon is almost similar to the other crypto-token, but the only difference being that it charges a 10% transaction fee from the users who sell their tokens, in which 5% of the fee is distributed to the remaining SafeMoon owners. This feature rewards the owners for holding onto their tokens.

Read More @ https://bit.ly/3oFbJoJ

#create a defi token like safemoon #defi token like safemoon #safemoon token #safemoon token clone #defi token