473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

interface as parameter, and concrete class in method

Hi, a "style" question.

There is a method which creates a user in a database. I have to instantiate
a user object, pass it to the method, and I get a completed user object
back.
public IUser Create(IUser user)
{
....
}

where IUser is an interface. Yet in the method itself I find things like:
((User)user).SetId(id);

Where User is a concrete class implementing IUser. Is this horrible? Or is
it perfectly reasonable? (The point of it seems to be to allow the
programmer to set some properties of the user object which the interface
does not declare - there are only getters). Does this mean that if I want to
supply my own implementation of IUser I really have to inherit from User and
not simply implement IUser and pass it to the method?

Thanks,
Peter
Jan 18 '07 #1
3 2151
peter wrote:
Hi, a "style" question.

There is a method which creates a user in a database. I have to instantiate
a user object, pass it to the method, and I get a completed user object
back.
public IUser Create(IUser user)
{
...
}

where IUser is an interface. Yet in the method itself I find things like:
((User)user).SetId(id);

Where User is a concrete class implementing IUser. Is this horrible? Or is
it perfectly reasonable? (The point of it seems to be to allow the
programmer to set some properties of the user object which the interface
does not declare - there are only getters). Does this mean that if I want to
supply my own implementation of IUser I really have to inherit from User and
not simply implement IUser and pass it to the method?
Well, it certainly looks like your Create method can't *really* work
with any IUser. If you want it to, you'll have to add some setters to
the interface. Otherwise, I'd consider changing that interface to take
User. Another alternative - create another interface IUpdatableUser or
something similar, with the setters on, and make User implement both
interfaces. Then you could return IUser but take IUpdateableUser.

Jon

Jan 18 '07 #2
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
Well, it certainly looks like your Create method can't *really* work
with any IUser. If you want it to, you'll have to add some setters to
the interface. Otherwise, I'd consider changing that interface to take
User. Another alternative - create another interface IUpdatableUser or
something similar, with the setters on, and make User implement both
interfaces. Then you could return IUser but take IUpdateableUser.
Thanks for the suggestions. I have found this problem as I have made my own
"user" implementing IUser, passed it to the method and found that it fails
of course. While I do have access to see this CreateUser code I cannot
really change it as it is in use, along with the existing User - so I am
loathe to change them and have to face the possible repurcussions...

Thanks,
Peter
Jan 18 '07 #3
peter wrote:
Well, it certainly looks like your Create method can't *really* work
with any IUser. If you want it to, you'll have to add some setters to
the interface. Otherwise, I'd consider changing that interface to take
User. Another alternative - create another interface IUpdatableUser or
something similar, with the setters on, and make User implement both
interfaces. Then you could return IUser but take IUpdateableUser.

Thanks for the suggestions. I have found this problem as I have made my own
"user" implementing IUser, passed it to the method and found that it fails
of course. While I do have access to see this CreateUser code I cannot
really change it as it is in use, along with the existing User - so I am
loathe to change them and have to face the possible repurcussions...
In that case, you've basically got to use the original User class (or a
derived class) instead of your own implementation of IUser,
unfortunately.

Jon

Jan 18 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Davide M3xican Coppola | last post by:
Hi, I would realize an interface (interface) for a pair of concrete classes (concrete & concrete2), but I have a problem because I have to access to a third class (element) and if I try to declare...
3
by: Christof Warlich | last post by:
Hi, I'm looking for a more concise way than below to extend an existing interface. Just imagine that class Interface would have 1000 other members and class DerivedInterface only needs to add...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
3
by: New Comer | last post by:
Can somebody compare Abstract Class and Interface for me? Thanks
6
by: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from Programming C#: #region Using directives ...
13
by: ^MisterJingo^ | last post by:
I've been reading up on interfaces, one example I came across showed that you can hide a method implemented from an interface from the class which implements it. To use it, you must cast to the...
7
by: tron.thomas | last post by:
Please consider the following code: class Abstract { public: virtual ~Abstract() {} virtual void Method() = 0; }; class Concrete : public virtual Abstract
6
by: Wayne Shu | last post by:
hi everyone! I have a problem in implementing a common class interface. my assignment is to implement a data structure list, and I have define a class template list_base, it's an abstract class,...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.