LINQ2SQL 中用于共享常用方法的抽象类

发布于 2024-09-14 06:49:27 字数 1109 浏览 3 评论 0原文

我在尝试在 linq2sql 设计器创建的两个类之间实现共享方法/属性时遇到问题。

我的两个类有两个主要属性(来自数据库模型):

public partial class DirectorPoll
{
    public bool Completed {get; set;}
    public bool? Reopen { get; set; }
    //more properties
}

public partial class StudentPoll
{
    public bool Completed {get; set;}
    public bool? Reopen { get; set; }
    //more properties
}

现在例如我创建一个抽象类:

public abstract class GenericPoll
{
    public abstract bool Completed { get; set; }
    public abstract bool? Reopen { get; set; }

    public bool CanEdit
    {
        get
        {
            if (Completed == false) return true;
            if (Reopen.GetValueOrDefault(false) == false) return false;
            return true;
        }
    }
}

然后

public partial class DirectorPoll : GenericPoll
public partial class StudentPoll: GenericPoll

但是当我尝试编译时它说“Director 没有实现继承的抽象成员 GenericPoll.Completed.get”。但它就在那里。所以我认为我被迫对设计器自动生成的属性进行覆盖,但如果我稍后更新数据库并重新编译,它将给我同样的错误。

我想我可能在这里遗漏了一些东西,但我尝试了不同的方法但没有成功。那么,除了在每个部分类中实现 CanEdit 之外,我还能做什么呢?谢谢

I'm having trouble trying to implement a shared method/property between two classes created by the linq2sql designer.

My two classes have two main properties (coming from the db model):

public partial class DirectorPoll
{
    public bool Completed {get; set;}
    public bool? Reopen { get; set; }
    //more properties
}

public partial class StudentPoll
{
    public bool Completed {get; set;}
    public bool? Reopen { get; set; }
    //more properties
}

Now for example I create an abstract class:

public abstract class GenericPoll
{
    public abstract bool Completed { get; set; }
    public abstract bool? Reopen { get; set; }

    public bool CanEdit
    {
        get
        {
            if (Completed == false) return true;
            if (Reopen.GetValueOrDefault(false) == false) return false;
            return true;
        }
    }
}

Then

public partial class DirectorPoll : GenericPoll
public partial class StudentPoll: GenericPoll

But when I try to compile it says "Director does not implement inherited abstract member GenericPoll.Completed.get". But it is there. So I think I'm forced to put an override to the property automatically generated by the designer, but if I update the database later and recompile it will give me the same error.

I think I might be missing something here but I've tried different approaches with no success. ¿So what can I do here, besides implementing CanEdit in each of my partial classes? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女山的月亮 2024-09-21 06:49:27

它不是作为覆盖实现的,因此它不算数。然而,隐式接口实现确实计数,所以这是有效的:

partial class DirectorPoll : IGenericPoll {}
partial class StudentPoll : IGenericPoll {}
public interface IGenericPoll
{
    bool Completed { get; set; }
    bool? Reopen { get; set; }
}
public static class GenericPoll {
    public static bool CanEdit(this IGenericPoll instance)
    {
        return !instance.Completed || instance.Reopen.GetValueOrDefault();
    }
}

It isn't implemented as an override, so it doesn't count. However, implicit interface implementations do count, so this works:

partial class DirectorPoll : IGenericPoll {}
partial class StudentPoll : IGenericPoll {}
public interface IGenericPoll
{
    bool Completed { get; set; }
    bool? Reopen { get; set; }
}
public static class GenericPoll {
    public static bool CanEdit(this IGenericPoll instance)
    {
        return !instance.Completed || instance.Reopen.GetValueOrDefault();
    }
}
自演自醉 2024-09-21 06:49:27

一种选择:创建一个包含 CompletedReopen 的接口,使类实现该接口(通过部分类的手动位),然后编写一个扩展方法来扩展该接口界面。我认为这应该可行:

public interface IPoll
{
    bool Completed {get; set;}
    bool? Reopen { get; set; }
}

// Actual implementations are in the generated classes;
// no need to provide any actual code. We're just telling the compiler
// that we happen to have noticed the two classes implement the interface
public partial class DirectorPoll : IPoll {}
public partial class StudentPoll : IPoll {}

// Any common behaviour can go in here.
public static class PollExtensions
{
    public static bool CanEdit(this IPoll poll)
    {
        return !poll.Completed || poll.Reopen.GetValueOrDefault(false);
    }
}

诚然,它必须是一种方法而不是属性,因为不存在扩展属性之类的东西,但这并不算太困难。

(我相信我在 CanEdit 中重构的逻辑是正确的。所有这些明确的对错都在困扰我;)

One option: create an interface containing Completed and Reopen, make the classes implement the interface (via the manual bits of the partial classes), then write an extension method which extends that interface. I think that should work:

public interface IPoll
{
    bool Completed {get; set;}
    bool? Reopen { get; set; }
}

// Actual implementations are in the generated classes;
// no need to provide any actual code. We're just telling the compiler
// that we happen to have noticed the two classes implement the interface
public partial class DirectorPoll : IPoll {}
public partial class StudentPoll : IPoll {}

// Any common behaviour can go in here.
public static class PollExtensions
{
    public static bool CanEdit(this IPoll poll)
    {
        return !poll.Completed || poll.Reopen.GetValueOrDefault(false);
    }
}

Admittedly then it has to be a method rather than a property, as there's no such thing as an extension property, but that's not too much of a hardship.

(I believe my refactoring of your logic in CanEdit is correct. All those explicit trues and falses were doing my head in ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文