I'm working on a Winforms project with sql server, splitted in several assemblies.
The first assembly Entities
contains DTO like :
public class Attribution
{
public short UserId { get; set; }
public User User { get; set; }
}
public class User
{
public short Id { get; set; }
}
The second assembly Repository
is accessing Sql Server database.
The third assembly Service
is the link between previous.
There are other layers but this is not the point. I need of course DTO's everywhere in the app.
In sql server, Attribution.UserId
and User.Id
are the same datas, located in 2 separate tables, linked by Ìnner join
.
Attribution.UserId
must be public because I need access from Repository
,Service
, etc... But I don't need it in the "logical" part of the app, what I need is Attribution.User
.
At this time I have a UserService
class in which there is a GetUser()
method and I call this method to get the user in my AttributionService.GetAttribution()
method.
Is there a way to restrict access to Attribution.UserId
property to Service
assembly? Or is it a kind of "good practice violation" to query a User
DTO in AttributionService
class?
Many thanks for your recommandation.
`
One option would be to make the set
of the property internal
and the use the InternalsVisibleTo
attribute to grant access to internals to the Repository assembly.
Another less technical and more logical option would be to make the setter private and let the only way for it to be modified be the classes constructor. That way, your repository can get users built, but nobody can modify the ID later.
As a last option you could create an interface that contains only what the non-repository classes should have access to and pass this around. I'm not a big fan because that means you have to cast it back to your concrete class in the repository and that basically means your repository is lying (saying it accepts an ISomething
, but then throwing if the ISomething
is not the exact, concrete Something
it expects).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With