代码之家  ›  专栏  ›  技术社区  ›  Junior

如何在中建立事件侦听器通信ASP.NETMVC 5?

  •  6
  • Junior  · 技术社区  · 7 年前

    我有一个项目 c# IoC 使用 Unity-container 处理依赖注入。

    // First we define a base class for the Events
    // This allows me tho identify Events. Perhaps this could be changed to interface instead of a base class
    public abstract class Event
    {
    
    }
    
    // Second we define a base class for our listener
    // each listener must provide a an event that it wish to listen to
    public abstract class Listener<T> where T : Event
    {
        // each listener will add to do logic inside the Handle() method
        public abstract void Handle(T _event);
    }
    
    // Here is my announcing service or event dispatcher
    // This class announces that an event took place for anyone that cares
    public class Announcer : IAnnouncer
    {
        private IUnityContainer Container;
    
        public Announcer(IUnityContainer container)
        {
            Container = container;
        }
    
        public void Announce<T>(T _event) where T : Event
        {
            // Get all listeners
            var listeners = Container.ResolveAll<Listener<T>>();
    
            foreach(var listener in listeners)
            {
                // here I may need to add a support to queue up the events or delay dispatching if needed
                try
                {
                    listener.Handle(_event);
    
                    if(listener is IHandleSuccess _listener)
                    {
                        _listener.Finished(_event, listener);
                    }
                }
                catch (Exception e)
                {
                    if (listener is IHandleFailed _listener)
                    {
                        _listener.Failed(_event, e);
                    }
                }
            }
        }
    }
    

    // First, define a simple event called UserWasCreated
    public class UserWasCreated : Event
    {
        public User User { get; private set; }
    
        public UserWasCreated(User user)
        {
            User = user;
        }
    }
    
    // Listener Example 1, Now we have a listener that would listen for when a user is added 
    // This listener would simply send a welcome email to the new user
    public class SendWelcomeEmail : Listener<UserWasCreated>
    {
        public void Handle(UserWasCreated _event)
        {
            // here we can send an to the event.User....
        }
    }
    
    // Listener Example 2, Now we have a listener that would listen for when a user is added 
    // This listener would simply sign up the user for an orientation
    public class ScheduleOrientation : Listener<UserWasCreated>
    {
        public void Handle(UserWasCreated _event)
        {
            // here we can schedule the orientation
        }
    }
    
    // In the controller we make announcement that the event took place
    public UsersController : Controller
    {   
        private IUserMapper UserMapper;
        private IAnnouncer Announcer;
      private IUserService UserService;
    
        public UsersController(IUserMapper userMapper, IAnnouncer announcer, IUserService userService)
        {
            UserMapper = userMapper;
            Announcer = announcer;
             UserService = userService;
        }
    
        ...
    
        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Create(CreateUserViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                User user = UserMapper.Map(viewModel);
    
                UserService.Create(user);
    
                // Here we announce the the event UserWasCreated took place.
                Announcer.Announce(new UserWasCreated(user));
            }
    
            return View(viewModel);
        }
    }
    

    问题 :是否有更好的方法在ASP.NETMVC 5项目?上面的设置有什么问题?

    0 回复  |  直到 7 年前
    推荐文章