我找到了部分的方法非常有用,但没有部分属性。是否有一个很好的理由,他们没有列入?我有什么办法?到了、部分、很好、我找

由网友(旧人旧梦旧时光°)分享简介:更新:请注意,我知道我不能这样做......这就是我真的很希望可以工作。也许有,将职责分离,没有一些其他的方式?所以,我在找的是...... UPDATE:Please note that I know I cannot do this...this is what I was really hoping cou...

更新: 请注意,我知道我不能这样做......这就是我真的很希望可以工作。也许有,将职责分离,没有一些其他的方式?所以,我在找的是......

UPDATE: Please note that I know I cannot do this...this is what I was really hoping could work. Maybe there is some other way that would separate responsibility, no? So what I am looking for is...

实体框架强制多重责任到类(普通逻辑,基本的注释和CRUD接口的能力)。我只是想采取什么样的通常都是在一个类...并通过分离实体框架和常规逻辑类的持久能力。

Entity Framework forces multiple responsibilities into the class (regular logic, basic annotations, and CRUD interface ability). I just want to take what would normally all be in one class...and separate the persistent ability of the class via Entity Framework and the regular logic.

我的心路历程:最近我一直陷入实体框架,但不喜欢这个主意,一些实体类的都在做有点过分。逻辑,接口与数据访问和实体框架注解。为了解决这个问题,我想使我的实体类文件的部分,并实现数据访问功能远离类的其他方面。这工作得很好,很干净!

MY THOUGHT PROCESS: Recently I have been getting into Entity Framework, but don't like the idea that some of the Entity classes are doing somewhat too much. Logic, interfacing with data access, and Entity Framework Annotations. To fix this, I wanted to make my Entity class file partial, and implement the data access functionality away from the other aspects of the class. This works well and is very clean!

当我这样做,我想这将大大有利于使我的属性部分,并从EF属性注解有实施远!这将清理文件,并允许单个的责任。然而,这是不允许的!令人失望的。

While I was doing that, I figured it would be greatly beneficial to make my properties partial, and have the implementation away from the EF Property Annotations!! This would clear up the files and allow single responsibility. However, this is not allowed! Bummer.

局部性质,就会像部分方法来实现。在一个局部性质的定义,并在其他部分财产的执行......就像照片显示(或意见),在顶部的链接,和下面的code。

The partial properties would be implemented just like partial methods. The definition in one partial property, and the implementation in the other partial property...just like the photos suggest (or the comments) in the link at the top, and the code below.

public partial class Agency : PropertyValidator, IAgency
{
    private string _name;

    public partial string Name 
    {
        get { return _name; }
        set
        {
            // PropertyValidator stuff
            if (string.IsNullOrEmpty(value))
                AddErrorToProperty("Agency name must contain a reasonable alphanumeric value.");
            if (string.IsNullOrWhiteSpace(value))   
                AddErrorToProperty("Agency name cannot contain all spaces.");

            SetPropertyIfValid(ref _name, value);
        }
    }
}

再有就是处理所有抽象数据库项目的其他部分类...

Then there is the other partial class that handles all abstract database items...

public partial class Agency : IPersitentEntity, IAgency
{       
    [Key]    // NOTE these Annotations are because of Entity Framework...nice separation! 
    public int ID { get; set; } // From IPersitentEntity

    [Required]
    [MinLength(3), MaxLength(50)]
    public partial string Name { get; set; } // IAgency NOTE this is not valid, but the 
                                             // separation is amazing!

    // From IPersitentEntity provide CRUD support
    public void Create() { throw new NotImplementedException(); }
    public void Retrieve(int id) { throw new NotImplementedException(); }
    public void Update(int id) { throw new NotImplementedException(); }
    public void Delete(int id) { throw new NotImplementedException(); }
}

现在,我的注解和逻辑结合在一起。这是一种奇怪的,因为我已经分离出抽象的数据库项目......除了EF注释!

Right now, I have to combine the Annotations and Logic together. It is kind of weird because I already separated out the abstract database items...except the EF Annotations!

推荐答案

部分主要是为code一代,我认为它是有用分离出不同,我认为不建议改变我的code基类中的职责,以方便我自己个人的项目,小级别的分离。我从来没有见过在现场除了code一代人做......了。

While partial is mainly for code generation, I have found it useful to separate out different responsibilities in the class that I have deemed not recommended to change my code base to facilitate that small level of separation on my own person projects. I have never seen that done in the field...again besides code generation.

我不知道这是一个个人项目,但如果是,我只想使用部分像你必须分离出持久的项目。我已经看过了分离出这进一步创建的数据库code和业务逻辑的另一个层面的网站。实现,可以是一个沉重的负担,如果你从来没有过,并添加类量好项目。

I don't know if this is a personal project, but if it was, I would just use the partial like you have to separate out the persistent items. I have looked at websites that separate out this further by creating another level between the database code and the business logic. Implementing that can be a burdensome if you never did it before, and adds a good amount of classes to your project.

如果这是一个个人项目,这可能是不值得的,如果你想独立出来的不同的责任,那么部分类的方式似乎工作。如果您需要更改访问数据的方式,那么你只改变了部分类,没有动人的常规业务逻辑。

If this is a personal project, that might not be worth it, and if you want to "separate" out the different responsibilities, then partial classes your way does seem to work. If you need to change the way you access data, then you change that partial class only, no touching of the regular business logic.

但是,你出的外观与上性能最有可能的注释的分离,但我不知道......

But you are out of look with the separation of the Annotations on the properties most likely, but I wonder...

如果您声明的执着部分类的属性,而是叫做getter和setter私有方法的常规业务逻辑部分类!?!?这样的逻辑是在一个局部类和注释将在其他部分的类,你希望他们

If you declared the properties in the persistent partial class, but called getter and setter private methods in the regular business logic partial class!?!? This way the logic is in that one partial class, and the Annotations will be in the other partial class where you want them

阅读全文

相关推荐

最新文章