简体   繁体   English

如何使用C#.net(VS2012)访问另一个项目中的类(但在同一解决方案中)

[英]How to Access Class in Another Project (But in the Same Solution ) using C#.net (VS2012)

i am working first time on the three layer architecture, I've created three Projects in one Solution, Project one is named BLL , second is named DAL and third one is names Model , I've Created interface in Model, now want to Create Business Logics in BLL and want to connect it to the DAL where I've connected my Data Base. 我第一次在三层架构上工作,我在一个解决方案中创建了三个项目,一个名为BLL ,第二个名为DAL ,第三个名为Model ,我在模型中创建了接口,现在要创建BLL中的业务逻辑,并希望将其连接到我已连接数据库的DAL。

For this Purpose I've added Reference of each Project With other like I've Added Reference of BLL in Model and Added reference of BLL in DAL. 为了这个目的,我添加了每个项目的参考,其他像我在模型中添加了BLL的参考,并在DAL中添加了BLL的参考。

now for the time i've created a class is DAL in which i've Connected my DB and have alos Created a Windows Form in Model, 现在,我已经创建了一个类是DAL,其中我连接了我的数据库并且已经在模型中创建了一个Windows窗体,

Now my questions are 现在我的问题是

1)how can I access classes of DAL in BLL and BLL's in Model 1)如何在模型中访问BLL和BLL中的DAL类

2)and which logic I've to create (in BLL) to Access Database through BLL 2)以及我将通过BLL创建(在BLL中)访问数据库的逻辑

the class in which I've connected DB is 我连接数据库的类是

DB.cs DB.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient();

namespace WindowsFormsApplication3
{
    class DB
    {
        public void fnc_ConnectoToDB(ref SqlConnection cn)
        {
            string strConnectionString = null;

            strConnectionString = @"Data Source=AHSANMUGHAL;Initial Catalog=SurveyBuilder;User ID=sa;Password=ahsan";

            cn = new SqlConnection();
            cn.ConnectionString = strConnectionString;
            cn.Open();      
        }

        public void fnc_CloseCn(ref SqlConnection cn)
        {
            if (cn.State == ConnectionState.Open == true)
            {
                cn.Close();
            }
        }

    }
}

i know this is bit confusing question but I hope you guys will understand it and will ans ASAP 我知道这是一个有点令人困惑的问题,但我希望你们能理解它并尽快解决

Try to use a dependency injection framework like ninject. 尝试使用像ninject这样的依赖注入框架。

Here is an example: 这是一个例子:

Your implementation: 你的实施:

public class Samurai {
    public IWeapon Weapon { get; private set; }
    public Samurai(IWeapon weapon) 
    {
        this.Weapon = weapon;
    }
}

And a module to give the Samurai it's weapon: 还有一个模块给武士它的武器:

public class WarriorModule : NinjectModule
{
    public override void Load() 
    {
        this.Bind<IWeapon>().To<Sword>();
    }
}

Simple as that. 就那么简单。

first of all, you need to make your class public. 首先,你需要公开你的课程。

public class DB 公共类DB

Here is a simple example of Dependency Injection based on your classes. 以下是基于类的依赖注入的简单示例。

This doesnt utilize any IoC (Inversion of Control) containers that would make your instances easier to manage. 不会利用任何IoC(控制反转)容器,这将使您的实例更容易管理。 I think its easier to conceptualize if you avoid the magic of an IOC container initially. 如果你最初避免使用IOC容器的魔力,我认为更容易概念化。 It also makes you glad to have it when you move up that rung. 当你向上移动时,它也会让你很高兴拥有它。

Ninject is an IoC container mentioned in another answer, Unity is another. Ninject是另一个答案IoC container提到的IoC containerUnity是另一个。

using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Model : Form
    {
        public Model()
        {
            InitializeComponent();
            var bll = new BLL(new DAL());
            bll.WriteToDatabase("mydbvalue");
        }
    }

    public interface IBll
    {
        void WriteToDatabase(string value);
    }
    public class BLL: IBll
    {
        private IDal _dataLayer;
        public BLL(IDal dataLayer)
        {
            _dataLayer = dataLayer;
        }

        public void WriteToDatabase(string value)
        {
            _dataLayer.WriteToDatabase(value);
        }
    }

    public interface IDal
    {
        void WriteToDatabase(string value);
    }
    public class DAL:IDal
    {
        public void WriteToDatabase(string value)
        {
            fnc_ConnectToDB();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在c#.net中将对象从表单访问到另一个类 - How to access an object from a form to another class in c#.net 如何在C#类库项目(VS2012)中嵌入和使用.txt文件作为资源? - How can I embed and use a .txt file as resource in a C# Class Library Project (VS2012)? 如何在使用VS2012创建的Blend 4中编辑ac#project? - How to edit a c# project in Blend 4 that was created with VS2012? VS2010和VS2012的两个C#项目文件在同一目录中 - Two C# project files for VS2010 and VS2012 in the same directory 如何在VS2012的解决方案文件中创建可执行文件? - How to create an executable out of a solution file in VS2012? 在VS2012中的ASP.NET项目中加载慢速符号 - Slow symbol loading in an ASP.NET Project in VS2012 C#.Net-如何仅创建类的一个对象并在整个项目中使用该对象 - C#.Net - How can i create only one object of the class and use the same through out the project Nunit没有在测试项目类库(VS2012)中遇到断点 - Nunit not hitting breakpoints in test project class library (VS2012) 如何在VS2010和VS2012中加载数据库项目? - How to load database project in VS2010 and VS2012? 使用Postsharp时在VS2012中编译c#项目时出错 - Error when compiling c# project in VS2012 when using Postsharp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM