c#通过接口从另一个存储库调用一个存储库

时间:2015-03-25 15:48:49

标签: c# repository-pattern

我是一个完整的新手,并在存储库中使用代码来使用xml文件中的数据过滤查询。我需要重构这段代码,将xml方法带到另一个存储库(通过接口),这样它也可以用来过滤来自另一个存储库的查询。

        public override IQueryable<CatalogueItem> Get(int pageSize, int pageNumber, out int totalRecords, Func<IQueryable<CatalogueItem>, IOrderedQueryable<CatalogueItem>> orderBy, Expression<Func<CatalogueItem, bool>> where = null)
    {
        //Check if BusinessUnit has Courses in Groups
        int countOfGroups = getCountOfGroupsforBusinessUnit();

        if (countOfGroups > 0)
        {
            List<int> varCourseIDList = new List<int>();
            varCourseIDList = getCourseIDList();
            if (where == null)
            {
                where = w => varCourseIDList.Contains(w.CatalogueItemID);
            }
            else
            {
                where = where.And(w => varCourseIDList.Contains(w.CatalogueItemID));
            }
        }

        return base.AppendPagination(this.GetGroupedByIdAndType(where), pageSize, pageNumber, out totalRecords, orderBy);
    }

    //Other Function Here

    public int getCountOfGroupsforBusinessUnit()
    {
        XElement xmlFile = getCoursewareGroupsXMLFile();

        // Count number of elements with that category element value.
        int count = (from agroup in xmlFile.Elements("Cranstoun")
                     select agroup).Count();

        return count;
    }


    public List<int> getCourseIDList()
    {
        XElement xmlFile = getCoursewareGroupsXMLFile();

        var varCourseID = (from courseid in xmlFile.Elements("XXX").Elements("Group").Elements("Course").Elements("CourseID")
                           select courseid);

        List<int> varCourseIDList = new List<int>();

        foreach (var item in varCourseID)
        {
            varCourseIDList.Add(Convert.ToInt32(item.Value));
        }

        return varCourseIDList;
    }

    private static XElement getCoursewareGroupsXMLFile()
    {
        string fileToLoad = HttpContext.Current.Server.MapPath("~/Data/CoursewareGroups.xml");
        XElement xmlFile = XElement.Load(fileToLoad);
        return xmlFile;
    }

如何重构这个以利用接口/存储库。到目前为止,我有以下内容:

IGroupRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lri.Emerald.EntityLayer;

namespace Lri.Emerald.DataLayer.Learning
{
    public interface IGroupRepository
    {
        int getCountOfGroupsforBusinessUnit();
        List<int> getCourseIDList();
    }
}

GroupRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Web;

namespace Lri.Emerald.DataLayer.Learning
{
    public class GroupRepository : IGroupRepository
    {
        public int getCountOfGroupsforBusinessUnit()
        {
            XElement xmlFile = loadCoursewareGroupsXMLFile();

            // Count number of elements with that category element value.
            int count = (from agroup in xmlFile.Elements("Cranstoun")
                         select agroup).Count();

            return count;
        }

        public List<int> getCourseIDList()
        {
            XElement xmlFile = loadCoursewareGroupsXMLFile();

            var varCourseID = (from courseid in xmlFile.Elements("Cranstoun").Elements("Group").Elements("Course").Elements("CourseID")
                               select courseid);

            List<int> varCourseIDList = new List<int>();

            foreach (var item in varCourseID)
            {
                varCourseIDList.Add(Convert.ToInt32(item.Value));
            }

            return varCourseIDList;
        }

        private static XElement loadCoursewareGroupsXMLFile()
        {
            string fileToLoad = HttpContext.Current.Server.MapPath("~/Data/CoursewareGroups.xml");
            XElement xmlFile = XElement.Load(fileToLoad);
            return xmlFile;
        }

    }
}

感谢任何帮助。

提前谢谢。

0 个答案:

没有答案