Somedeveloper
Somedeveloper

Reputation: 867

c# DAL class and business layer class

HI,

can you tell me if this is possible.

public class Person
{
    public string Name { get; set; }
    public int ID { get; set; }
}

Populate a class call say person which is in an assembly called Entities like this with the population of the code being done in a different assembly called DataAccessLayer (so person and the place where it is populated are not in the same assembly)

//the below code would be reading from a datareader etc but have just done this to make it //easy to explain.

Person p=new Person();
p.Name="tom";
p.id = 10;

The person class is now to be made accessible to another system to allow them to be able to access person. What i would like is to prevent the other system from being able to change the ID. be able to read it but not write. Do i need to create another class etc to allow this and only expose this class to the other system (i.e. a business object) (i.e. ORM)?

i know alot of people are going to say just make the ID readonly. i.e.

public int ID { get; }

but if i do this then i cannot populate the ID from the code similar to above because in my DataAccessLayer i will not be able to set the ID as it is readonly.

thanks Niall

Upvotes: 1

Views: 628

Answers (2)

breez
breez

Reputation: 494

Look toward ORM tools which will assign ID of entity for you and your id property will look:

public class MyEntity
{
     public virtual int ID { get; protected set; }

     // other properties
}

if you choose this way, you don't need to worry about assigning properties and casting of types.

Upvotes: 0

cjk
cjk

Reputation: 46425

You can create an internal constructor for the object that you can pass ID into, then set the flag for the Entities DLL that allows another DLL (DataAccessLayer) to be able to see and use the internal calls within this DLL. (InternalsVisibleTo attribute)

Upvotes: 4

Related Questions