Think of event injection (non-AOP) from Control reversal (dependency injection) in C # and. Net

Source: Internet
Author: User
Tags aop

Profile

The so-called incident injection is my temporary rise casually coined the word, the thought of reference to rely on injection. Of course, see this word many students will think of AOP, here first noncommittal.

Dependency Injection (Dependency injection) is a process in which a customer class defines only an injection point because it relies only on one interface of the service class and not on the specific service class. In the process of program operation, the customer class does not directly instantiate the specific service class instance, but the client class's running context or specialized component is responsible for instantiating the service class, then injecting it into the customer class to ensure the normal operation of the customer class.

This means that dependency injection acts as a decoupled role in our project scenario. In the project structure it can decouple a module, a service. T2 phage students in his posting described a game-playing example to explain the role of dependency injection. Then we also use the example of the odd to illustrate the scene of the event injection.

Detailed

About the design of the prototype can refer to the T2 classmate to play strange description of the game, I have a direct look at the effect map

Let's put aside T2 's design for dependency injection scenarios. Let's say the demo is available at the moment. But with the game version of the update, more and more moves, the effect is more and more gorgeous, more and more rules. T2 students use dependency injection to solve the focus is the role and weapons between the dependencies, packaging algorithm clusters, so that they can be replaced each other, so that the changes in the algorithm is independent of the use of the algorithm of the customer class.

Then the problem arises if I want to update the process rather than the weapon. For example, playing DotA students know that a hero his skills before and after swinging time is also a very important factor, well, we give the game to add skills before shaking the settings, cut the strange I have to get gold coins, ah, add a bit after the attack to obtain the contents of the gold coins. How to conform to our OCP principles. So, we introduce the concept of event injection.

First, we define the behavior we need.

<summary>
 ///Pre-attack event
 ///</summary> public
 static event eventhandler<eventargs> beforeattackevent;
     
 protected virtual void Beforeattack (EventArgs e)
 {
     eventhandler<eventargs> tmp = beforeattackevent;
     if (TMP!= NULL)
         tmp (this, e);
 }
     
 <summary>
 ///after attack
 ///</summary> public
 static event Eventhandler<gameeventargs > attackedevent;
     
 protected virtual void onattacked (Gameeventargs e)
 {
     eventhandler<gameeventargs> tmp = attackedevent;
     if (tmp!= NULL)
         tmp (this, e);
 }

The only thing defined here is the handle to the event, if the entity that implements our events here violates our OCP principles and the concept of event injection.

The EventArgs here is the base class for the class that contains the event data, and if we need to do additional processing of the injected event, such as I need to get the gold, then the gold coin attribute needs to be described in the event data

such as the post-attack event above

<summary>
///Inject event element
///</summary> public
class Gameeventargs:eventargs
{
    Public Gameeventargs ()
        : this (0)
    {
    } is public
     
    int Coin
    {get
        ;
        Set;
    }
     
    public Gameeventargs (int coin)
    {
        coin = coin;
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.