Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have an MVVM WPF application that will make use (hopefully) with a repository and unit-of-work.

This is my Entity-Framework Database first model:

public partial class tUser
{    
    public string ID { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Password { get; set; }
}

My implementation of the User Repository (didn't use a Generic Repository):

public class UsersRepository : IUserRepository
{
    private DbEntities db;

    public UsersRepository (DbEntities db)
    {
        this.db = db;
    }

    public tUser GetById(object userId)
    {
        return db.tUser.Find(userId);
    }

    public IEnumerable<tUser> GetAll()
    {
        return db.tUser.ToList();
    }

    public virtual IEnumerable<tUser> GetSome(
        Expression<Func<tUser, bool>> filter = null,
        Func<IQueryable<tUser>, IOrderedQueryable<tUser>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<tUser> query = db.Set<tUser>();

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }

    public void InsertUser(tUser user)
    {
        db.tUser.Add(user);
    }

    public void DeleteUser(object userId)
    {
        var user = db.tUser.Find(userId);
        db.tUser.Remove(user);
    }

    public void UpdateUser(tUser user)
    {
        db.Entry(user).State = EntityState.Modified;
    }

    public void Save()
    {
        db.SaveChanges();
    }

    #region dispose
    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                db.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    #endregion
}

Unit-Of-Work:

public class UnitOfWork : IDisposable
{
    private DbEntities db = new DbEntities();

    private IUserRepository userRepository;
    public IUserRepository UserRepository
    {
        get
        {
            return userRepository ?? new UsersRepository(db);
        }
    }

    public void Save()
    {
        db.SaveChanges();
    }

    public void Dispose()
    {
        db.Dispose();
    }

User Manager window ViewModel:

public class UsersManagerViewModel : ObservableObject
{
    private UnitOfWork unit = new UnitOfWork();

    private string _searchText;
    public string SearchText
    {
        get { return _searchText; }
        set
        {
            if (_searchText != value)
            {
                _searchText = value;
                SelectedUser = null;
                RaisePropertyChanged();
            }
        }
    }

    private UserViewModel _selectedUser;
    public UserViewModel SelectedUser
    {
        get { return _selectedUser; }
        set
        {
            if (_selectedUser != value)
            {
                _selectedUser = value;
                RaisePropertyChanged();
            }
        }
    }
}

UserViewModel:

public class UserViewModel : ObservableObject
{
    private readonly tUser entity;

    public UserViewModel()
        : this(new tUser())
    { 
    }

    public UserViewModel(tUser _entity)
    {
        this.entity = _entity;
    }

    public tUser Model { get { return this.entity; } }

    public string UserName
    {
        get { return entity.Name; }
        set
        {
            entity.Name = value;
            RaisePropertyChanged();
            RaisePropertyChanged("UserFullName");
        }
    }

    public string SurName
    {
        get { return entity.Surname; }
        set
        {
            entity.Surname = value;
            RaisePropertyChanged();
            RaisePropertyChanged("UserFullName");
        }
    }

    public string UserFullName
    {
        get { return entity.Name + " " + entity.Surname; }
        set { RaisePropertyChanged(); }
    }
}

Are my Implementations correct?
Should I remove the private DbEntities db; field from my Repository and wrap each call with a using (var db = new DbEntities())?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.