DI Implementation in asp.net project - c#

I am using ASP.Net, Entity Framwork along with Microsoft Unity Framework integration, i have below setup for the data consumption:
DataObjects:
ArticleDo - This contains all the entities needed for the database.
GenericDo - This contains all the entities which are common across project.
Dao. (Concrete classes)
ArticleDao - This contains the implemented functions specific for Articles from IArticleDao. (This inherits GenericDao, IArticleDao
GenericDao - This is an abstract class it contains the implementation of the Generic functions used across the project.
Interfaces.
IArticleDao - This contains the declaration of the Article specific functions. (this inherits IGenericDao)
IGenericDao -This contains the declaration of all the generic functions which are common across projects, like pulling in specific field from database etc. (This inherits IGenericDao
Here's the implementation:
DataObjects.
1. ArticleDo:
public class ArticleDo
{
public virtual int Id { get; set; }
public virtual int WordCount { get; set; }
public virtual string Title { get; set; }
}
2. GenericDo:
public class GenericDo
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
Business Layer (Concrete classes)
1. ArticleDao:
public class ArticleDao : GenericDao<ArticleDo, DataContext>, IArticleDao
{
private readonly Data.DataContext db;
public ArticleDao()
{
db = new Data.DataContext();
}
public override string Add(ArticleDo entity)
{
entity.AddedBy = Common.CurrentUsername;
db.Articles.Add(entity);
var returnVal = db.SaveChanges();
if (returnVal > 0)
return "Success";
return "Fail";
}
public List<ArticleDo> Search(string SearchTerm, int CurrentPage, out int Total)
{
SearchTerm = SearchTerm.ToLower();
var totalRecords = List().Where(x => x.ArticleId.ToString() == SearchTerm ||
(x.Title.ToString().ToLower().Contains(SearchTerm)));
Total = totalRecords.Count();
return totalRecords.Skip(CurrentPage * Common.Page_SIZE).Take(Common.Page_SIZE).ToList();
}
}
2. GenericDao:
public abstract class GenericDao<T, C> : IGenericDao<T, C> where T : class where C : DbContext, new ()
{
private readonly DataContext _db = new DataContext();
private C _entities = new C();
public virtual string Add(T entity)
{
_entities.Set<T>().Add(entity);
return "Success";
}
public virtual bool Delete(T entity)
{
_entities.Set<T>().Remove(entity);
return true;
}
public T Get(T entity)
{
var _get = _entities.Set<T>();
if (_get == null) return null;
return _get.First();
}
public virtual List<T> List()
{
return _entities.Set<T>().ToList();
}
public virtual bool Update(T entity)
{
_entities.Entry(entity).State = EntityState.Modified;
return true;
}
public T FindByProperty(string property, object value)
{
var _find = _db.Database.SqlQuery<IEnumerable<T>>(string.Format("SELECT a.* from {0} a where a.p0 = p1", typeof(T).FullName), new object[] { property, value });
if (_find != null && _find.Any())
{
return (T)_find.First();
}
return null;
}
}
DataInterfaces.
1. IArticleDao
public interface IArticleDao : IGenericDao<ArticleDo, DataContext>
{
List<ArticleDo> Search(string SearchTerm, int CurrentPage, out int Total);
}
2. IGenericDao
public interface IGenericDao : IGenericDao<GenericDo, DataContext> { }
public interface IGenericDao<T, C>
{
string Add(T entity);
bool Update(T entity);
bool Delete(T entity);
List<T> List();
T Get(T entity);
T FindByProperty(string property, object value);
}
Now before i implement the entities which would resolve the `IArticleDao` and i would be able to call my business layer functions, i tried to create concrete object directly in the page behind like:
IArticleDao dao = new ArticleDao();
Now as per my understanding, since ArticleDao is inheriting GenericDao<ArticleDo, DataContext> i should be able to access all the ArticleDo entities from the dao object, but that's not happening, could anyone please help me to achieve this, i don't want to inherit ArticleDao in ArticleDo, because that would defeat the purpose of DI integration. Also how could i use the contructor injection, do i need to create the constructor in my aspx code behind file?

Related

return a list of generic C#

I have a singleton class, holding several list of data. I want to have a function to return one of the list based on the requested data type
public interface IRentable
{
long Id { get; set; }
}
public class Book : IRentable
{
private long _id;
public long Id
{
get { return _id; }
set { _id = value; }
}
public string Name { get; set; }
public int Pages { get; set; }
}
public class DVD : IRentable
{
private long _id;
public long Id
{
get { return _id; }
set { _id = value; }
}
public string Name { get; set; }
public TimeSpan Length { get; set; }
}
public class DBReferenceSingleton
{
private List<Book> _bookList;
private List<DVD> _dvdList;
public IEnumerable<Entity> GetEntities<Entity>() where Entity : IRentable
{
switch(typeof(Entity).Name)
{
case nameof(Book):
return _bookList;
case nameof(DVD):
return _dvdList;
}
return null;
}
This is not working, since a conversion is needed. I wonder if there is any elegant solution (without serialize all elements or implement IConvertible)?
Background: I was looking into the implementation of Repository pattern implementation from Microsoft document website and a couple of YouTube videos. Their base class implementations are bounded with DBContext,
[from Microsoft doc]
public class GenericRepository<TEntity> where TEntity : class
{
internal SchoolContext context;
internal DbSet<TEntity> dbSet;
[from youtube]
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
protected readonly DbContext Context;
public Repository(DbContext context)
{
Context = context;
}
public IEnumerable<TEntity> GetAll()
{
return Context.Set<TEntity>().ToList();
}
So, this means this base class cannot be used for databases without EntityFramework. I'm trying to remove DBContext in my base class
public class InMemoryRepository<TEntity> : IRepository<TEntity> where TEntity : IRentable
{
private IEnumerable<TEntity> _entities;
public IEnumerable<TEntity> GetAll()
{
if (_entities == null)
{
_entities = DBReferenceSingleton.GetInstance.GetEntities<TEntity>();
}
return _entities;
}
public TEntity Get(long id)
{
GetAll();
if (_entities == null)
{
return default(TEntity);
}
return _entities.FirstOrDefault(t => t.Id == id);
}
(I was wrong in my previous code, as I was using "class" instead of "IRentable" as I thought it will be generic for all classes. After implementing the Get(long id) function. I found I have to have a IRentable interface so I made the changes. And the answer from The Lemon work perfectly.
Thanks
It doesn't look like generics seem the right tool here; not least because Book and DVD don't have a common base class.
I think you should have two methods:
public List<Book> GetBooks() => _bookList;
public List<DVD> GetDvds() => _dvdList;
Have you tried doing a Cast?
public class Book
{
public string Name { get; set; }
public int Pages { get; set; }
}
public class DVD
{
public string Name { get; set; }
public TimeSpan Length { get; set; }
}
public class DBReferenceSingleton
{
private List<Book> _bookList;
private List<DVD> _dvdList;
public IEnumerable<Entity> GetEntities<Entity>() where Entity : class
{
switch(typeof(Entity).Name)
{
case nameof(Book):
return _bookList as List<Entity>;
case nameof(DVD):
return _dvdList as List<Entity>;
}
return null;
}
}
the "as List< Entity >" will return null if the object is the wrong type, or the object as the type if it's the right type. The A = B as C pattern works for inherited types as well, just remember to check for null returned values for cases where your type isn't as well known as in this situation
A side comment on the usefulness of having a generic method in this case: In this method you're forced to set the type of entity each time explicitly, which means your method is functionally non-generic - so you might as well make two explicit methods.
One case where a generic method like the one you have might be more useful, is if book and dvd both inherited from a base class, and you had some follow up methods that needed to operate off a list of . For example, you might end up wanting to do something like this instead in your code:
public class Book : Rentable
{
public int Pages { get; set; }
}
public class DVD : Rentable
{
public TimeSpan Length { get; set; }
}
public class Rentable
{
public string Name { get; set; }
public string borrowedBy { get; set; }
}
public class DBReferenceSingleton
{
private List<Book> _bookList;
private List<DVD> _dvdList;
public enum RentableType { Book, DVD }
public IEnumerable<Rentable> GetEntities(RentableType entityType)
{
switch (entityType)
{
case RentableType.Book:
return _bookList.ToList<Rentable>();
case RentableType.DVD:
return _dvdList.ToList<Rentable>();
default:
throw new NotImplementedException($"Entity {entityType} not supported");
}
return null;
}
}

Soft deleted child objects appear in parent objects list

I'm using Entity Framework Core 2.2 code first. Soft delete is implemented in the overriden Context class. There is an UnitOfWork class that initializes Repositories for specific entities, while taking the Context as argument in constructor.
Some of the application entities have been left out in this sample, for a clearer overview. I will only use Team, Member, Project and Employee.
While testing, a realized the following: when I delete an object (for instance, a Project), it successfully sets it's "Deleted" property to "true" after SaveChanges(). However, if the solution project is still running, I realized that if this object (Project) has a parent object (Team), it will still appear in the team's List Projects, even though their "Deleted" property is clearly "true". While the process is still running, if I call the repository Get() method that returns all Projects, the deleted project won't show up in the returned projects list, but only in the team's List Projects.
Only when I rerun the solution project, these child objects don't appear in parent's List Projects anymore.
Here is a NUnit test I've written to analyze the issue:
[Test, Order(2)]
public void GetTeamAfterDeletingProject()
{
//There are initially 11 projects
var projects = unit.Projects.Get().ToList();
Assert.AreEqual(11, projects.Count());
Project project = unit.Projects.Get(8);
unit.Projects.Delete(project);
unit.Save();
//now there are 10 projects
projects = unit.Projects.Get().ToList();
Assert.AreEqual(10, projects.Count());
//this team had initially 2 projects, and one of them was deleted
Team team = unit.Teams.Get(3);
//only one project should be left, but there are actually two showing up - this test fails
Assert.AreEqual(1, team.Projects.Count);
project.Deleted = false;
unit.Save();
}
This interferes with my implemented Delete method in the TeamsRepository, which doesn't allow deletion of an object if there are child object's in his list. This means, that, If I wanted to delete the team in the previous test, even if I deleted all his projects, I still wouldn't be able to do it without restarting the app (the whole repository can be found below)
public override void Delete(int id)
{
Team old = Get(id);
if (old.Projects.Count != 0 || old.Members.Count != 0)
{
Services.ThrowChildrenPresentException();
}
Delete(old);
}
I'm wondering if this is the normal behavior when using lazy loading, or did I miss something in the configuration.
protected override void OnConfiguring(DbContextOptionsBuilder optionBuilder)
{
if(_conStr != null)
{
optionBuilder.UseNpgsql(_conStr);
}
optionBuilder.UseLazyLoadingProxies(true);
base.OnConfiguring(optionBuilder);
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Member>().HasQueryFilter(x => !x.Deleted);
builder.Entity<Employee>().HasQueryFilter(x => !x.Deleted);
builder.Entity<Project>().HasQueryFilter(x => !x.Deleted);
builder.Entity<Team>().HasQueryFilter(x => !x.Deleted);
}
public override int SaveChanges()
{
foreach (var entry in ChangeTracker.Entries().Where(x => x.State == EntityState.Deleted && x.Entity is BaseClass))
{
entry.State = EntityState.Modified;
entry.CurrentValues["Deleted"] = true;
}
return base.SaveChanges();
}
This is the Team entity:
public class Team: BaseClass
{
public Team()
{
Members = new List<Member>();
Projects = new List<Project>();
}
public string Name { get; set; }
public string Description { get; set; }
public bool StatusActive { get; set; }
public virtual IList<Member> Members { get; set; }
public virtual IList<Project> Projects { get; set; }
}
public class Member: BaseClass
{
public virtual Team Team { get; set; }
public virtual Employee Employee { get; set; }
public virtual Role Role { get; set; }
public virtual MemberStatus Status { get; set; }
public decimal HoursWeekly { get; set; }
}
public class BaseClass
{
public BaseClass()
{
Created = DateTime.Now;
Creator = 0;
Deleted = false;
}
[Key]
public int Id { get; set; }
public DateTime Created { get; set; }
public int Creator { get; set; }
public bool Deleted { get; set; }
}
The generic Repository and the specific TeamsRepository with overriden methods. The soft deleted projects appear in old.Projects.Count, so I am never able to delete a Team this way (even though they aren't retrieved when
public class Repository<Entity>: IRepository<Entity> where Entity : class
{
protected AppContext _context;
protected DbSet<Entity> _dbSet;
public Repository(AppContext context)
{
_context = context;
_dbSet = _context.Set<Entity>();
}
public void ValidateUpdate(Entity newEntity, int id)
{
if (id != (newEntity as BaseClass).Id)
throw new ArgumentException($"Error! Id of the sent object: {(newEntity as BaseClass).Id} and id in url: {id} do not match");
}
public virtual IQueryable<Entity> Get() => _dbSet;
public virtual IList<Entity> Get(Func<Entity, bool> where) => Get().Where(where).ToList();
public virtual Entity Get(int id)
{
Entity entity = _dbSet.Find(id);
if (entity == null)
throw new ArgumentException($"There is no object with id: {id} in the database");
return entity;
}
public virtual void Insert(Entity entity)
{
entity.Build(_context);
_dbSet.Add(entity);
}
public virtual void Update(Entity entity, int id)
{
entity.Build(_context);
Entity old = Get(id);
ValidateUpdate(entity, id);
_context.Entry(old).CurrentValues.SetValues(entity);
old.Relate(entity);
}
public void Delete(Entity entity) => _dbSet.Remove(entity);
public virtual void Delete(int id)
{
Entity old = Get(id);
Delete(old);
}
}
public class TeamsRepository: Repository<Team>
{
public TeamsRepository(AppContext context) : base(context) { }
public override void Delete(int id)
{
Team old = Get(id);
if (old.Projects.Count != 0 || old.Members.Count != 0)
{
Services.ThrowChildrenPresentException();
}
Delete(old);
}
}

Repository pattern with list as its core in C#

I'm following this site: http://deviq.com/repository-pattern/
Which has an example of a repository pattern using a DB context. I'm trying to implement this generic Repository class with a list (I want the Repository class. That's a requirement of mine). However, I'm having issues with the Find method.
public class Repository<T> : IRepository<T> where T : class
{
private List<T> context;
virtual public T Find(int id)
{
// I can't figure out a way to make this work with the list of a generic type
}
}
Is this even possible to make a predicate in the List.Find() with just an ID parameter? I'm guessing no, but what are options?
Another option, if you can't control the type of T so as to apply an interface, is to force your implementers to do the hard work.
public abstract class Repository<T> : IRepository<T> where T : class
{
private List<T> context;
public virtual public T Find(int id)
{
return context.FirstOrDefault(x => GetId(x) == id);
}
public abstract int GetId(T entity);
}
an example implementation may be
// entity
public class Stooge
{
public Stooges MoronInQuestion {get;set;}
public double MoeEnragementFactor {get;set;}
public void PloinkEyes() { /*snip*/ }
public void Slap() { /*snip*/ }
public void Punch() { /*snip*/ }
// etc
}
// enum for an Id? It's not that crazy, sometimes
public enum Stooges
{
Moe = 1,
Larry = 2,
Curly = 3,
Shemp = 4,
Joe = 5,
/* nobody likes Joe DeRita */
//CurlyJoe = -1,
}
// implementation
public class StoogeRepository : IRepository<Stooge>
{
public override int GetId(Stooge entity)
{
if(entity == null)
throw new WOOWOOWOOException();
return (int)entity.MoronInQuestion;
}
}
You can declare that T has an Id property with something like this:
public interface IEntity
{
int Id { get; }
}
public class Repository<T> : IRepository<T> where T : class, IEntity
{
private List<T> context;
virtual public T Find(int id)
{
return context.SingleOrDefault(p => p.Id == id);
}
}

Controller with same structure not working using an interface

I have created 8 models and controllers respectively for my Project using the same structure, however now this structure is not working. It is giving me this error:
The current type, HelpDesk.Contracts.Repositories.IRepositoryBase`1[HelpDesk.Model.Knowledgebase], is an interface and cannot be constructed. Are you missing a type mapping?
Here is my interface:
namespace HelpDesk.Contracts.Repositories
{
public interface IRepositoryBase<TEntity>
where TEntity : class
{
void Commit();
void Delete(object id);
void Delete(TEntity entity);
System.Linq.IQueryable<TEntity> GetAll();
System.Linq.IQueryable<TEntity> GetAll(object filter);
TEntity GetById(object id);
TEntity GetFullObject(object id);
System.Linq.IQueryable<TEntity> GetPaged(int top = 20, int skip = 0, object orderBy = null, object filter = null);
void Insert(TEntity entity);
void Update(TEntity entity);
}
}
Here is my controller:
namespace HelpDesk.WebUI.Controllers
{
public class KnowledgebaseController : Controller
{
private DataContext db = new DataContext();
IRepositoryBase<Knowledgebase> knowledgebases;
public KnowledgebaseController(IRepositoryBase<Knowledgebase> knowledgebases)
{
this.knowledgebases = knowledgebases;
}
public ActionResult ChooseSearchView()
{
string r = Session["LoggedUserRole"].ToString();
if (r == "Technician")
{
return RedirectToAction("TechKnowledgebaseSearch");
}
else
{
return RedirectToAction("UserKnowledgebaseSearch");
};
}
My DataContext:
namespace HelpDesk.Contracts.Data
{
public class DataContext : DbContext
{
/// <summary>
/// you can either pass the NAME of a connection string (e.g. from a web.
/// ), and explicitly i
/// </summary>
public DataContext() : base("HelpDesk")
{
}
/// <summary>
/// any entity to be persisted must be declared here.
/// </summary>
///
public DbSet<Category> Categories { get; set; }
public DbSet<Knowledgebase> Knowledgebases { get; set; }
public DbSet<Problem> Problems { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Ticket> Tickets { get; set; }
public DbSet<TicketNote> TicketNotes { get; set; }
public DbSet<TicketStatus> TicketStatuses { get; set; }
public DbSet<TicketSubscription> TicketSubscriptions { get; set; }
public DbSet<User> Users { get; set; }
}
}
Here is the repositorybase:
namespace HelpDesk.Contracts.Repositories
{
public abstract class RepositoryBase<TEntity> : HelpDesk.Contracts.Repositories.IRepositoryBase<TEntity> where TEntity : class
{
internal DataContext context;
internal DbSet<TEntity> dbSet;
public RepositoryBase(DataContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual TEntity GetById(object id)
{
return dbSet.Find(id);
}
public virtual IQueryable<TEntity> GetAll()
{
return dbSet;
}
public IQueryable<TEntity> GetPaged(int top = 20, int skip = 0, object orderBy = null, object filter = null)
{
return null; // need to override in order to implement specific filtering and ordering
}
public virtual IQueryable<TEntity> GetAll(object filter)
{
return null; //need to override in order to implement specific filtering
}
public virtual TEntity GetFullObject(object id)
{
return null; //need to override in order to implement specific obect graph.
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Update(TEntity entity)
{
dbSet.Attach(entity);
context.Entry(entity).State = EntityState.Modified;
}
public virtual void Delete(TEntity entity)
{
if (context.Entry(entity).State == EntityState.Detached)
dbSet.Attach(entity);
dbSet.Remove(entity);
}
public virtual void Delete(object id)
{
TEntity entity = dbSet.Find(id);
}
public virtual void Commit()
{
context.SaveChanges();
}
public virtual void Dispose()
{
context.Dispose();
}
}
}
Here I have a similar structured Controller that works just fine and another 7 like that:
namespace HelpDesk.WebUI.Controllers
{
public class RoleController : Controller
{
private DataContext db = new DataContext();
IRepositoryBase<Role> roles;
public RoleController(IRepositoryBase<Role> roles)
{
this.roles = roles;
}
The Exception is thrown when I click on this link:
<li>#Html.ActionLink("Knowledgebase", "ChooseSearchView", "Knowledgebase")</li>
Update your KnowledgebaseController constructor to this:
public KnowledgebaseController()
{
this.knowledgebases = new RepositoryBase<Knowledgebase>(db);
}
and remove abstract from the RepositoryBase<TEntity> class in order to make it instantiable.
Please note: This is just a quick fix. To implement a much solid architecture which facilitates easy unit testing and loosely coupled classes, I would suggest you implement a unit of work. Follow this link Repository Pattern and Unit of Work with Entity Framework in ASP.NET MVC
You can't instantiate an interface, you have to instantiate a class that implements that interface.
public class RepositoryBase<T> : IRepositoryBase
{
public RepositoryBase<T>()
{
}
...
}
Then instantiate your concrete class with that type
IRepositoryBase<Knowledgebase> knowledgebases = new RepositoryBase<Knowledgebase>();
Read more about interfaces and instantiation here: Using Interfaces

Generic access to DbContext

ObjectContext allows generic access to the generated Entities. DbContext appears to have no such support. Accessing EF5 with a generic repository is challenging. Let's say I want a generic mechanism to read any given entity, call it TEntity:
public class DataRepositoryEF5<T> where T: DbContext
{
private ObjectContext _context;
public DataRepositoryEF5(DbContext context)
{
_context = ((IObjectContextAdapter)context).ObjectContext;
}
public IEnumerable<TEntity> ReadAll<TEntity>() where TEntity : class,new()
{
return GetObjectSet<TEntity>().AsEnumerable();
}
protected ObjectSet<TEntity> GetObjectSet<TEntity>() where TEntity : class,new()
{
ObjectSet<TEntity> result;
result = _context.CreateObjectSet<TEntity>();
return result;
}
}
Usage
var context = new MyContext();
var repository = new DataRepositoryEF5<MyContext>(context);
IEnumerable<Document> results = repository.GetAll<Document>();
foreach (var item in results)
{
Console.WriteLine("{0} {1} {2} {3}", item.Description, item.Id, item.Property, item.Whatever);
}
EF used to generate classes with a common base type EntityObject. Since that is no longer the case the best constraint I can have is class...
Second, because there is no equivalent of ObjectContext.CreateObjectSet<>() I am forced to cast from DbSet to ObjectSet.
With this pattern in place I have no need for DbContext. Without generics I am forced to hand code all CRUD operations. Did I miss something? If not, is it possible to tell EF5 to generate code with ObjectContext instead?
have a look at this tip that I posted a while ago
Anyway the trick is by using context.Set<>
public interface IEntity
{
int Id { get; set; }
}
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity
{
private IDbContext _context;
public Repository(IDbContext context)
{
_context = context;
}
private IDbSet<TEntity> DbSet
{
get
{
return _context.Set<TEntity>();
}
}
public IQueryable<TEntity> GetAll()
{
return DbSet.AsQueryable();
}
public void Delete(TEntity entity)
{
DbSet.Remove(entity);
}
.....
This is a more up to date and complete answer.
using Library.Models;
using Library.EF;
namespace Abstractions
{
public interface IKeyedModel
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
}
public class GenericContext<T> : DbContext where T : class, IKeyedModel
{
public DbSet<T> Items { get; set; } = default!;
}
public interface IRepository<T> where T : class, IKeyedModel
{
public GenericContext<T> Context() => new GenericContext<T>();
public T Select(int id) => Context().Items.Single(x => x.Id == id);
public IQueryable<T> Select() => Context().Items;
public void Insert(T item)
{
var context = Context();
context.Items.Add(item);
context.SaveChanges();
}
public void Update(T item)
{
var context = Context();
context.Items.Attach(item);
context.SaveChanges();
}
public void Delete(int id)
{
var context = Context();
var item = context.Items.Single(x => x.Id == id);
context.Items.Remove(item);
context.SaveChanges();
}
}
}
namespace Models
{
[Table(name: "User")]
public class User : IKeyedModel
{
public int Id { get; set; }
public string Name { get; set; } = default!;
}
}
namespace App
{
public class UserRepository : IRepository<User> { /* empty */ }
}

Categories

Resources