I will try to explain what I want, but it will be hard. ) So I have entity:
public class User
{
[Key]
public Int32 Id { get; set; }
public String Name { get; set; }
public List<Article> Article { get; set; }
public String Surname { get; set; }
}
I have the realization of DbContext
and its interface:
public interface IMyContext
{
DbSet<T> Set<T>() where T : class;
}
public class MyContext : DbContext, IMyContext
{
public DbSet<User> Users { get; set; }
protected override void Dispose(bool disposing)
{
this.SaveChanges();
base.Dispose(disposing);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//modelBuilder.Entity<User>().HasMany(e => e.Article).WithRequired(e => e.User).WillCascadeOnDelete(true);
base.OnModelCreating(modelBuilder);
}
}
I have the this entity repository and it's interface:
public interface IUserRepository
{
void Add(User entity);
void Delete(User entity);
User GetById(Int32 id);
List<User> GetAll();
}
public class UserRepository : RepositoryBase<User>, IUserRepository
{
[Inject]
public UserRepository(IMyContext context):base(context)
{
}
}
Also I'm using ninject to inject dependency. In the usage it looks like this(dont tell me that I shouldn't use the repository in my pressentation layer directly, I should use it through the BussinesLogic
layer. It's just an example):
var repository = DI.Resolve<IUserRepository>();
repository.Add(new User(){Name="Vasja1", Id = 1, Surname = "Petrov"});
Let's imagine that we have in our pressentation layer the Asp.NET application. There is a lot of places where my application can modify entities. Question where i should use SaveChanges()
? I put it in context.Dispose()
method but dispose also have to be called. Thx for answers. Will be very gratitude if you will show the example.