简体   繁体   English

一个抽象类,可以由数据库类派生

[英]An abstract class which can be derived by database classes

This is my abstract class: 这是我的抽象类:

namespace MusicStoreApp.BLL.Master
{
    public abstract class Master<T>
    {
        MusicStoreEntities db = new MusicStoreEntities();
        public void Add(T item)
        {
            db.T.Add(item);
            db.SaveChanges();
        }
    }
}

This is my target output in the other classes: 这是我在其他类中的目标输出:

public class AlbumRepository : Master<Album>
    {
        public void Add(Album item)
        {
            db.Albums.Add(item);
            db.SaveChanges();
        }
     }

public class ArtistRepository : Master<Artist>
        {
            public void Add(Artist item)
            {
                db.Artists.Add(item);
                db.SaveChanges();
            }
         }

What i am tring to do here, that i should create a reusable interface-like class. 我在这里要做的是,我应该创建一个可重用的类接口类。 So, i can just type the name of the T reference and it will create the rest of the codes for me. 所以,我只需输入T引用的名称,它就会为我创建其余的代码。

The way your sample is setup can't work because T needs to point to two different classes (the specific instance and the DbSet that contains that class). 您的示例设置方式无法正常工作,因为T需要指向两个不同的类(特定实例和包含该类的DbSet)。 Instead, try this: 相反,试试这个:

namespace MusicStoreApp.BLL.Master
{
    public abstract class Master<T>
    {
        MusicStoreEntities db = new MusicStoreEntities();
        public void Add(T item)
        {
            db.Entry(item).State = System.Data.Entity.EntityState.Added;
            db.SaveChanges();
        }
    }
}

You don't need this T anonymous type. 您不需要此T匿名类型。 Just do something like this: 做这样的事情:

public abstract class Master
{
    public abstract void Add(Master item);
}

Then you can just inherit the Master like this: 那么你可以像这样继承Master:

public class Album : Master
    public override void Add(Album item)
    {
        db.Albums.Add(item);
        db.SaveChanges();
    }
}

If you want to use a repository for the add just remove the add function from master and make interface and inherit from it: 如果要使用存储库进行添加,只需从master中删除add函数并生成接口并从中继承:

public interface IMasterRepository 
{
    public void Add(Master item);
}

public class AlbumRepository : IMasterRepository
    public override void Add(Album item)
    {
        db.Albums.Add(item);
        db.SaveChanges();
    }
}

But don't mix the entity classes with the repositories. 但是不要将实体类与存储库混合在一起。

You are mixing abstract with generic class. 您正在将abstractgeneric类混合在一起。 The former contains something that requires to be implemented by the inheritors while the later provides common implementation that differs by some type(s) of the objects involved. 前者包含需要由继承者实现的内容,而后者提供的通用实现因所涉及对象的某些类型而不同。 From your explanation (and since your "abstract" class does not contain any abstract method), looks like you need a generic class. 从你的解释(因为你的“抽象”类不包含任何抽象方法),看起来你需要一个泛型类。 Something like this 像这样的东西

public class Master<T>
{
    MusicStoreEntities db = new MusicStoreEntities();
    public void Add(T item)
    {
        db.Set<T>().Add(item);
        db.SaveChanges();
    }
}

public class AlbumRepository : Master<Album> { }
public class ArtistRepository : Master<Artist> { }

Note that you don't even need the concrete classes (if that's all they are supposed to do). 请注意,您甚至不需要具体的类(如果他们应该这样做的话)。

You can do so by using reflection. 您可以使用反射来完成此操作。

get property name 获取属性名称

string PropertyName = T.GetType().Name + "s";

retrive the entity property 检索实体属性

var  property = db.GetType().Properties.Where(x => x.Name.CompareTo(PropertyName) == 0).FirstOrDefault();

then work with it directly 然后直接使用它

Thank you for all of your effort to answer :). 感谢您的所有努力回答:)。

I found my answer, I hope it will help you too: 我找到了答案,我希望它也能帮助你:

 public abstract class RepositoryBase<T>:IRepository<T> where T:class
{
    public void Add(T item)
    {
        db.Set<T>().Add(item);
        db.SaveChanges();
    }

    public void Update(int id,T item)
    {
        db.Entry(db.Set<T>().Find(id)).CurrentValues.SetValues(item);
        db.SaveChanges();
    }

    public void Delete(T item)
    {
        db.Set<T>().Remove(item);
        db.SaveChanges();
    }

    public List<T> SelectAll()
    {
        return db.Set<T>().ToList();
    }

    public T SelectByID(int id)
    {
        return db.Set<T>().Find(id);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM