Ioc-StructureMap

来源:互联网 发布:邓肯生涯场均数据 编辑:程序博客网 时间:2024/05/16 16:11

1. Global.asax

  protected void Application_Start()        {            BootStrapper.ConfigureStructureMap();         }


2.BootStrapper

    public class BootStrapper    {        public static void ConfigureStructureMap()        {            // Initialize the registry            ObjectFactory.Initialize(x =>            {                x.AddRegistry<ModelRegistry>();            });        }        public class ModelRegistry : Registry        {            public ModelRegistry()            {                ForRequestedType<IGetTotalElectricGrid>().TheDefault.Is.OfConcreteType<GetTotalElectricGridDPI>();                ForRequestedType<IGetAirSystemElectric>().TheDefault.Is.OfConcreteType<GetAirSystemElectricDPI>();                ForRequestedType<IGetItemsElectric>().TheDefault.Is.OfConcreteType<GetItemsElectricDPI>();                ForRequestedType<IGetDataByDay>().TheDefault.Is.OfConcreteType<GetDataByDayDIP>();                ForRequestedType<IGetDataByWeek>().TheDefault.Is.OfConcreteType<GetDataByWeekDPI>();                ForRequestedType<IGetDataByMonth>().TheDefault.Is.OfConcreteType<GetDataByMonthDPI>();                ForRequestedType<IGetDataByYear>().TheDefault.Is.OfConcreteType<GetDataByYearDPI>();            }        }    }


3.HomeController

 [HttpPost]        public string GetChartIt(FormCollection fc)        {            HomePageClientControl homePageClientControl = ObjectFactory.GetInstance<HomePageClientControl>();            return homePageClientControl.GetData(fc["data"].ToString());            //return HomePageClientControl.Instance().GetData(fc["data"].ToString());        }


4.IGetDataByDay

   public interface IGetDataByDay    {        StringBuilder GetDataByDay(string data);    }


5.GetItemsElectricDPI

    public class GetItemsElectricDPI : IGetItemsElectric    {        public StringBuilder GetItemsElectric(string time)        {            string[] str = ClientControl.GetChartDataType.GetTimeByType.GetTimeByTypeTime(time);            DataTable dt = null;            if (time == "日")            {                dt = HomePageDataControl.Instance().GetTypeElectric(str[0], str[1]);            }            else//周,月,年            {                dt = HomePageDataControl.Instance().GetTypeElectricByDay(str[0], str[1]);            }            StringBuilder json = new StringBuilder();            json.Append("{ 'chart': { 'caption': '分项用电比例', 'numberSuffix': '(kwh)', 'baseFont':'微软雅黑','baseFontSize':'12'}, 'data': [ " +                        "{ 'label': '照明插座', 'value': '" + dt.Rows[0][0].ToString() + "' }, " +                        "{ 'label': '空调', 'value': '" + dt.Rows[1][0].ToString() + "' }, " +                        "{ 'label': '动力', 'value': '" + dt.Rows[2][0].ToString() + "' }, " +                        "{ 'label': '特殊', 'value': '" + dt.Rows[3][0].ToString() + "' }]}");            json = json.Replace("'", "\"");            return json;        }    }


6. HomePageClientControl

 

     private readonly IGetTotalElectricGrid _getTotalElectricGrid;        private readonly IGetAirSystemElectric _getAirSystemElectric;        private readonly IGetItemsElectric _getItemsElectric;        private readonly IGetDataByDay _getDataByDay;        private readonly IGetDataByWeek _getDataByWeek;        private readonly IGetDataByMonth _getDataByMonth;        private readonly IGetDataByYear _getDataByYear;        public HomePageClientControl(IGetTotalElectricGrid getTotalElectricGrid,                                      IGetAirSystemElectric getAirSystemElectric,                                     IGetItemsElectric getItemsElectric,                                     IGetDataByDay getDataByDay,                                     IGetDataByWeek getDataByWeek,                                     IGetDataByMonth getDataByMonth,                                     IGetDataByYear getDataByYear)        {            this._getTotalElectricGrid = getTotalElectricGrid;            this._getAirSystemElectric = getAirSystemElectric;            this._getItemsElectric = getItemsElectric;            this._getDataByDay = getDataByDay;            this._getDataByWeek = getDataByWeek;            this._getDataByMonth = getDataByMonth;            this._getDataByYear = getDataByYear;        }        public string GetData(string data)        {            StringBuilder json = new StringBuilder();            //json.Append(GetChartData.GetTotalElectricGrid(data));            json.Append(this._getTotalElectricGrid.GetTotalElectricGrid(data));json.Append("&&");            //json.Append(GetChartData.GetAirSystemElectric(data));            json.Append(this._getAirSystemElectric.GetAirSystemElectric(data));json.Append("&&");            //json.Append(GetChartData.GetItemsElectric(data));            json.Append(this._getItemsElectric.GetItemsElectric(data));json.Append("&&");            //json.Append(GetChartData.GetUnitAreaElectric(data));            //json.Append("&&");            //json.Append(GetChartData.GetPerAverage(data));            //json.Append("&&");            //json.Append(GetChartData.GetTotalEnergy(data));            //json.Append("&&");            //json.Append(GetChartData.GetTotalElectric(data, ""));            //json.Append("&&");            //json.Append(GetChartData.GetTotalWater(data, ""));            //json.Append("&&");            //json.Append(GetChartData.GetTotalGas(data));            if (data.Equals("日")) { json.Append(this._getDataByDay.GetDataByDay(data)); }            if (data.Equals("周")) { json.Append(this._getDataByWeek.GetDataByWeek(data)); }            if (data.Equals("月")) { json.Append(this._getDataByMonth.GetDataByMonth(data)); }            if (data.Equals("年")) { json.Append(this._getDataByYear.GetDataByYear(data)); }            return json.ToString();        }
原创粉丝点击