asp.net项目中的DI实现

时间:2018-12-25 11:12:39

标签: c# asp.net dependency-injection unity-container

我正在使用ASP.Net,实体框架以及Microsoft Unity Framework集成,我在下面设置了数据消耗量:

  1. 数据对象:     
    1. ArticleDo-这包含数据库所需的所有实体。
    2.     
    3. GenericDo-它包含项目中通用的所有实体。
  2. 道(具体课程)     
    1. ArticleDao-包含特定于IArticleDao的Articles的已实现功能。 (这继承了GenericDao,IArticleDao
    2.     
    3. GenericDao-这是一个抽象类,它包含整个项目中使用的Generic函数的实现。
  3. 接口。     
    1. IArticleDao-包含文章特定功能的声明。 (这继承了IGenericDao)
    2.     
    3. IGenericDao-它包含所有项目中通用的通用函数的声明,例如从数据库中提取特定字段等。(这继承了IGenericDao

这是实现:

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代码中创建构造函数?

0 个答案:

没有答案