我正在使用ASP.Net,实体框架以及Microsoft Unity Framework集成,我在下面设置了数据消耗量:
这是实现:
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; }
}
业务层(具体类)
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);
}
现在,在实现可解析“ IArticleDao”的实体并能够调用我的业务层功能之前,我试图直接在后面的页面中创建具体对象,如:
IArticleDao dao = new ArticleDao();
现在,根据我的理解,由于ArticleDao
继承了GenericDao<ArticleDo, DataContext>
,因此我应该能够访问ArticleDo
对象中的所有dao
实体,但这没有发生,谁能帮助我实现这一目标,我不想继承ArticleDao
中的ArticleDo
,因为那样做会破坏DI集成的目的。另外我该如何使用构造函数注入,是否需要在文件后面的aspx代码中创建构造函数?