简体   繁体   中英

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.

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). 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. Just do something like this:

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

Then you can just inherit the Master like this:

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:

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. 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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