性能私人集; [英] Properties private set;

查看:123
本文介绍了性能私人集;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它只是允许类来设置,但有什么意义?

I know it only allows the class to set it, but what is the point?

我如何解决有只读ID的问题?

How do I solve the problem of having readonly ids?

说我有一个Person类:

Say I have a person class:

public class Person
    {
        public string Name { get;  set; }
        public int Id { get; private set; }
        public int Age { get; set; }
    }



这是在 Entities.dll ,由一个图形用户界面,BL和DAL使用

And this is in an Entities.dll, used by a GUI, BL and DAL.

在GUI调用BL:

   List<Person> p =  BL.PeopleBL.GetPeople();

有关这个例子的缘故调用DAL:

For the sake of the example calls the DAL:

...
while(dr.read())
{
    returnPersonList.add( new Person{ Age=dr.GetInt32(1), Id=dr.GetInt32(0), Name=dr.GetString(2)})
}
...

当然,我不能这样做的原因ID是一个私人集;
什么是做到这一点的正确方法?

of course I cannot do that cause Id is a private set; What is the proper way to do this?

我怎样才能让BL /达尔设置的ID,但不是在GUI?

How can I let the BL/Dal set the Id, but not on the GUI?

或者是这个甚至没有正确使用的私人一套?

Or is this not even the proper use of a private set?

我只是想补充一点,这是典型的数据库应用程序,其中的PK是ID,不应该(只由BL / DAL)来改变

I just wanted to add that this is your typical DB app, where the pk is the Id and should not be changed( only by the BL/DAL)

推荐答案

这是一个可能的解决方案,虽然不是很干净的:

This is one possible solution although not very clean:


  1. 让你需要暴露在BAL和放大器的财产; DAL 内部

  2. 标记 BAL.dll &安培; DAL.dll 内部可见的在的AssemblyInfo.cs

  1. Make the property you need to expose to BAL & DAL internal
  2. Mark BAL.dll & DAL.dll Internal Visible in assemblyinfo.cs

public class Person
{
    public Person(int id)
    {
         this.Id=id;
    }

    public string Name { get;  set; }
    public int Id { get; internal set; }
    public int Age { get; set; }
}



的AssemblyInfo.cs Entities.dll

[assembly: InternalsVisibleTo("DAL"), InternalsVisibleTo("BAL")]

这样,所有的内部将是DAL&安培可见; BAL。这可能不是理想的,但我只是建议一个可能的解决方案。

That way all your internals will be visible to DAL & BAL. This may not be desirable but I'm just suggesting one possible solution.

这篇关于性能私人集;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆