C# 在决策结构中使用表达式和动作委托

C# 在决策结构中使用表达式和动作委托,c#,delegates,func,C#,Delegates,Func,我有一个实例,我不熟悉C#委托、函数、表达式和动作。我想把一个参数作为一个表达式来传递,这个表达式将检查一些条件,然后它将返回或根据提供的一些条件执行 var result = Uow.GroupLicense.Set .Join(Uow.UserGroup.Set, x => x.GroupGuid, y => y.GroupGuid, (GL, G) => new { G, GL })

我有一个实例,我不熟悉C#委托、函数、表达式和动作。我想把一个参数作为一个表达式来传递,这个表达式将检查一些条件,然后它将返回或根据提供的一些条件执行

 var result = Uow.GroupLicense.Set
                        .Join(Uow.UserGroup.Set, x => x.GroupGuid, y => y.GroupGuid, (GL, G) => new { G, GL })
                        .Join(Uow.Users.Set, x => x.G.UserGuid, y => y.UserGuid, (UG, U) => new { UG, U })
                        .SingleOrDefault(x => x.U.UserID == Username);

            if (result != null && result.UG.GL.IsActive && result.U.IsEnabled && !result.U.IsLocked)
            {
                SessionStorage.LoginAttempts = UpdateLoginAttempts(Username, true);
                SessionStorage.SessionID = HttpContext.Current.Session.SessionID;
                SessionStorage.LoginAttempts = 0;
                SessionStorage.UserName = Username;
                SessionStorage.ABBUserName = Username;
            }
            else if (!result.UG.GL.IsActive)
            {
                result.U.IsEnabled = false;
                result.U.IsLocked = true;
                Uow.Users.Update(result.U);
                Uow.Commit();
                ErrorMessage = "User License Expired";
            }
            else if (result.U.IsLocked)
            {
                ErrorMessage = "User is locked";
            }
            else if (!result.U.IsEnabled)
            {
                ErrorMessage = "User is disabled by administrator";
            }
            else
            {
               ErrorMessage = "User doesnt exist ";
            }
 if this ->  Boolean result = EnsureLicense((x, y) => x.IsEnabled && y.IsActive);
   then do Action1 
   if This -> Boolean result = EnsureLicense((x, y) => !x.IsEnabled && y.IsActive);
   then do Action2 
   if This - > Boolean result = EnsureLicense((x, y) => !x.IsEnabled && y.IsLocked);
   then do Action3.
这是我的原始代码。我想将其转换为基于两个类项条件的决策树。像这样的

 var result = Uow.GroupLicense.Set
                        .Join(Uow.UserGroup.Set, x => x.GroupGuid, y => y.GroupGuid, (GL, G) => new { G, GL })
                        .Join(Uow.Users.Set, x => x.G.UserGuid, y => y.UserGuid, (UG, U) => new { UG, U })
                        .SingleOrDefault(x => x.U.UserID == Username);

            if (result != null && result.UG.GL.IsActive && result.U.IsEnabled && !result.U.IsLocked)
            {
                SessionStorage.LoginAttempts = UpdateLoginAttempts(Username, true);
                SessionStorage.SessionID = HttpContext.Current.Session.SessionID;
                SessionStorage.LoginAttempts = 0;
                SessionStorage.UserName = Username;
                SessionStorage.ABBUserName = Username;
            }
            else if (!result.UG.GL.IsActive)
            {
                result.U.IsEnabled = false;
                result.U.IsLocked = true;
                Uow.Users.Update(result.U);
                Uow.Commit();
                ErrorMessage = "User License Expired";
            }
            else if (result.U.IsLocked)
            {
                ErrorMessage = "User is locked";
            }
            else if (!result.U.IsEnabled)
            {
                ErrorMessage = "User is disabled by administrator";
            }
            else
            {
               ErrorMessage = "User doesnt exist ";
            }
 if this ->  Boolean result = EnsureLicense((x, y) => x.IsEnabled && y.IsActive);
   then do Action1 
   if This -> Boolean result = EnsureLicense((x, y) => !x.IsEnabled && y.IsActive);
   then do Action2 
   if This - > Boolean result = EnsureLicense((x, y) => !x.IsEnabled && y.IsLocked);
   then do Action3.
我不想把它转换成If-else结构代码和多个If-else和else-If结构代码。既然我不能进入开关箱,如何进行这种策略

我想用动作、表达式和Func委托来完成。我想学习如何实施这种策略


请指导我

这里是带有操作和Func委托的解决方案,不使用if…else结构。实际上,这是一种:

处理看起来像

handler1.Handle(licence, user);
那些支持基于委托的代码而不是简单的if-else的人,接受我最深切的同情

更新:如果您想要漂亮的fluent API,那么您可以创建生成器类,该类将创建处理程序链:

public class LicenseHandlerBuilder
{
    private LicenseHandler root;
    private LicenseHandler current;

    private LicenseHandlerBuilder() { }

    public static LicenseHandlerBuilder CreateHandler(
        Func<GroupLicense, User, bool> predicate, Action action)
    {
        var builder = new LicenseHandlerBuilder();
        builder.root = new LicenseHandler(predicate, action);
        builder.current = builder.root;
        return builder;
    }

    public LicenseHandlerBuilder WithNext(
        Func<GroupLicense, User, bool> predicate, Action action)
    {
        var next = new LicenseHandler(predicate, action);
        current.SetNext(next);
        current = next;
        return this;
    }

    public LicenseHandler Build()
    {
        return root;
    }
}

这很好,但最后一条语句要求它是一个布尔值,它再次执行一些操作。如果需要的话,这可以作为一个流畅的界面吗?@Joy哪句话是最后一句?对不起,抱歉。我明白你的意思了。但是没有handler1.SetNext(handler2.SetNext(handler3)),这能做到吗;但是有一个流畅的界面?类似于改变setNext来处理as方法和条件的链接?一方的结果取决于另一方?我真的是这个架构的新手。哇,太棒了!!一些我需要拥有很长时间的东西。当我遇到这个路障时,我无法得到正确的指导。非常感谢!!:)
var handler = LicenseHandlerBuilder
        .CreateHandler((l, u) => l.IsEnabled && u.IsActive, Action1)
        .WithNext((l, u) => !l.IsEnabled && u.IsActive, Action2)
        .WithNext((l, u) => !l.IsEnabled && u.IsLocked, Action3)
        .Build();