Battlelamb
Battlelamb

Reputation: 97

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.

Upvotes: 3

Views: 761

Answers (5)

Battlelamb
Battlelamb

Reputation: 97

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

Upvotes: 0

Mykola
Mykola

Reputation: 3363

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

Upvotes: 0

Ivan Stoev
Ivan Stoev

Reputation: 205579

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

Upvotes: 1

dlght
dlght

Reputation: 926

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.

Upvotes: 1

Jeff S
Jeff S

Reputation: 7484

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

Upvotes: 1

Related Questions