加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 程序设计 > 正文

依赖注入 – 如何将此Castle Windsor DI代码合并到我的Controller和Repository代码中?

发布时间:2020-05-23 11:51:46 所属栏目:程序设计 来源:互联网
导读:注意:我还不能确定这个问题(这太新了),但我会用50分给出一个很好的答案,并给出100分的好答案(如果可能的话). 我需要将DI合并到我的Web API项目中.我目前有预期的Model和Controller文件夹/类,以及相应的Repository类. 这似乎工作了一段时间,但现在我需要使用D

注意:我还不能确定这个问题(这太新了),但我会用50分给出一个很好的答案,并给出100分的好答案(如果可能的话).

我需要将DI合并到我的Web API项目中.我目前有预期的Model和Controller文件夹/类,以及相应的Repository类.

这似乎工作了一段时间,但现在我需要使用DI与控制器,以便我可以将接口类型传递给控制器的构造函数.

我正在努力解决如何实施这个问题;也就是说,如何将DI“extravaganza”整合到我现有的Model / Controller / Repository结构中.我有示例DI代码,但我不知道它应该如何应用于我的项目.

也许有些代码是为了试图明确这一点.我将展示一个我所得到的简单示例,然后是DI代码我想以某种方式合并到它/它.

这是现有的模型/控制器/存储库代码:

模型

public class Department
{   
    public int Id { get; set; }
    public int AccountId { get; set; }
    public string Name { get; set; }
}

CONTROLLER

public class DepartmentsController : ApiController
{
    private readonly IDepartmentRepository _deptsRepository;

    public DepartmentsController(IDepartmentRepository deptsRepository)
    {
        if (deptsRepository == null)
        {
            throw new ArgumentNullException("deptsRepository is null");
        }
        _deptsRepository = deptsRepository;
    }

    public int GetCountOfDepartmentRecords()
    {
        return _deptsRepository.Get();
    }

    public IEnumerable<Department> GetBatchOfDepartmentsByStartingID(int ID,int CountToFetch)
    {
        return _deptsRepository.Get(ID,CountToFetch);
    }

    public void PostDepartment(int accountid,string name)
    {
        _deptsRepository.PostDepartment(accountid,name);
    }

    public HttpResponseMessage Post(Department department)
    {
        // Based on code 2/3 down http://www.codeproject.com/Articles/344078/ASP-NET-WebAPI-Getting-Started-with-MVC4-and-WebAP?msg=4727042#xx4727042xx
        department = _deptsRepository.Add(department);
        var response = Request.CreateResponse<Department>(HttpStatusCode.Created,department);
        string uri = Url.Route(null,new { id = department.Id });
        response.Headers.Location = new Uri(Request.RequestUri,uri);
        return response;
    }

REPOSITORY

public class DepartmentRepository : IDepartmentRepository
{
    private readonly List<Department> departments = new List<Department>();

    public DepartmentRepository()
    {
        using (var conn = new OleDbConnection(
            @"Provider=Microsoft.ACE.OLEDB.12.0;User ID=BlaBlaBla...
        {
            using (var cmd = conn.CreateCommand())
            {
                cmd.CommandText = "SELECT td_department_accounts.dept_no,IIF(ISNULL(t_accounts.name),'No Name provided',t_accounts.name) AS name 
                FROM t_accounts INNER JOIN td_department_accounts ON 
                t_accounts.account_no = td_department_accounts.account_no ORDER BY 
                td_department_accounts.dept_no";
                cmd.CommandType = CommandType.Text;
                conn.Open();
                int i = 1;
                using (OleDbDataReader oleDbD8aReader = cmd.ExecuteReader())
                {
                    while (oleDbD8aReader != null && oleDbD8aReader.Read())
                    {
                        int deptNum = oleDbD8aReader.GetInt16(0);
                        string deptName = oleDbD8aReader.GetString(1);
                        Add(new Department { Id = i,AccountId = deptNum,Name,deptName });
                        i++;
                    }
                }
            }
        }
    }

    public int Get()
    {
        return departments.Count;
    }

    private Department Get(int ID) // called by Delete()
    {
        return departments.First(d => d.Id == ID);
    }

    public IEnumerable<Department> Get(int ID,int CountToFetch)
    {
        return departments.Where(i => i.Id > ID).Take(CountToFetch);
    }

    public Department Add(Department dept)
    {
        if (dept == null)
        {
            throw new ArgumentNullException("Department arg was null");
        }
        // This is called internally,so need to disregard Id vals that already exist
        if (dept.Id <= 0)
        {
            int maxId = departments.Max(d => d.Id);
            dept.Id = maxId + 1;
        }
        if (departments != null) departments.Add(dept);
        return dept;
    }

    public void PostDepartment(int accountid,string name)
    {
        int maxId = departments.Max(d => d.Id);
        Department dept = new Department();
        dept.Id = maxId + 1;
        dept.AccountId = accountid;
        dept.Name = name;
        departments.Add(dept);
    }

    public void Post(Department department)
    {
        int maxId = departments.Max(d => d.Id);
        department.Id = maxId + 1;
        departments.Add(department);
    }

    public void Put(Department department)
    {
        int index = departments.ToList().FindIndex(p => p.Id == department.Id);
        departments[index] = department;
    }

    public void Put(int id,Department department)
    {
        int index = departments.ToList().FindIndex(p => p.Id == id);
        departments[index] = department;
    }

    public void Delete(int id)
    {
        Department dept = Get(id);
        departments.Remove(dept);
    }

现在这里是我想要合并的DI代码

DIInstallers文件夹中的类:

IDepartmentProvider.cs

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

namespace HandheldServer.DIInstallers
{
    public interface IDepartmentProvider
    {
        // These are the methods that are in the sample example IAuthProvider interface; I don't know what I need yet,though...
        //bool Authenticate(string username,string password,bool createPersistentCookie);
        //void SignOut();
    }
}

DepartmentProvider.cs

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

namespace HandheldServer.DIInstallers
{
    public class DepartmentProvider : IDepartmentProvider
    {
        // TODO: Implement methods in IDepartmentProvider,once they have been added
    }
}

DepartmentProviderInstaller.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;

namespace HandheldServer.DIInstallers
{
    public class DepartmentProviderInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container,IConfigurationStore store)
        {
            container.Register(Classes.FromThisAssembly()
                                   .BasedOn(typeof(IDepartmentProvider))
                                   .WithServiceAllInterfaces());

    // If I declare/implement more interface types (other than IDepartmentProvider),I assume there would be another container.Register() call for each of them?
        }
    }
}

DIPlumbing文件夹中的类:

WindsorCompositionRoot.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Castle.Windsor;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;

namespace HandheldServer.DIPlumbing
{
    public class WindsorCompositionRoot : IHttpControllerActivator
    {
        private readonly IWindsorContainer container;

        public WindsorCompositionRoot(IWindsorContainer container)
        {
            this.container = container;
        }

        public IHttpController Create(
            HttpRequestMessage request,HttpControllerDescriptor controllerDescriptor,Type controllerType)
        {
            var controller =
                (IHttpController)this.container.Resolve(controllerType);

            request.RegisterForDispose(
                new Release(
                    () => this.container.Release(controller)));

            return controller;
        }

        private class Release : IDisposable
        {
            private readonly Action release;

            public Release(Action release)
            {
                this.release = release;
            }

            public void Dispose()
            {
                this.release();
            }
        }
    }
}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读