Giter Site home page Giter Site logo

mibus's Introduction

mibus

Another .NET Micro Bus/Mediator Library, enables clean separation of concern when writing C# applications.

==Usage==

private readonly IMediator _mediator = .Resolve(); List userList = _medator.Execute(new AllUsersQuery());

...

==Detailed example==

Let's say I wanted to have all commands for interacting with a User object, available from one place only in my entire application, I will simply create a single file with the following class definition:

public class UserCommands { #region AllUsersQuery public class AllUsersQuery : IQuery<IEnumerable> { }

    public class Handler : IQueryHandler<AllUsersQuery, IEnumerable<User>>
    {
        public Handler()
        {
        }

        public IEnumerable<User> Handle(AllUsersQuery allRoomListQuery)
        {
            using (var cxt = new CabanaEntities())
            {
                return cxt.Users.ToList().AsEnumerable();
            }
        }
    }
    #endregion

    #region Save Command
    public class Save : ICommand
    {
        public User User { get; set; }
    }

    public class UserSavedEvent : IEvent
    {
        public User User { get; set; }
    }

    public class SaveHandler : ICommandHandler<Save>
    {
        public void Execute(Save command)
        {
            using (var cxt = new CabanaEntities())
            {
                if (command.User.UserID == 0)
                {
                    cxt.Users.Add(command.User);
                }
                else
                {
                    cxt.Users.Attach(command.User);
                }
                
                cxt.SaveChanges();
            }
        }
    }
    #endregion

    #region Delete Command
    public class Delete : ICommand
    {
        public User User { get; set; }
    }

    public class DeleteHandler : ICommandHandler<Delete>
    {
        public void Execute(Delete command)
        {
            using (var cxt = new CabanaEntities())
            {
                cxt.Users.Attach(command.User);
                cxt.SaveChanges();
            }
        }
    }
    #endregion
}

And make calls to it using the mediator like this:

public class UserForm: Form {
    private readonly IMediator _mediator = <IoCContainer>.Resolve<IMediator>();
    
    private void Form_Load(object sender, EventArgs e){
        List<User> userList = _medator.Execute(new AllUsersQuery());
        UserGrid.DataSource = userList;
    }
}

mibus's People

Contributors

okyereadugyamfi avatar

Watchers

James Cloos avatar

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.