5

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.

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.

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,

Now my questions are

1)how can I access classes of DAL in BLL and BLL's in Model

2)and which logic I've to create (in BLL) to Access Database through BLL

the class in which I've connected DB is

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

4

3 回答 3

2

首先,您需要公开您的课程。

公共类数据库

于 2013-10-02T19:51:38.953 回答
2

尝试使用 ninject 之类的依赖注入框架。

这是一个例子:

您的实施:

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

还有一个为武士提供武器的模块:

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

就那么简单。

于 2013-10-02T18:53:30.197 回答
0

这是一个基于您的类的依赖注入的简单示例。

使用任何 IoC(控制反转)容器,使您的实例更易于管理。我认为如果您最初避免使用 IOC 容器的魔力,则更容易概念化。当你向上移动时,它也会让你很高兴拥有它。

NinjectIoC container在另一个答案中提到的,Unity是另一个。

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();
        }
    }
}
于 2013-10-02T22:10:17.000 回答