473,699 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

interfaces for data classes

Hi

I was wondering about the use of interfaces for "data classes".
Actually I don't know the accepted term for these types of classes -
they are simply classes which have getters and setters, to contain data
and not really provide any functions.

Is it worth defining interfaces for these types of classes, or is it
"overkill"?

Eg. I might have a class like:

public class User : IUser
{
public string Name { get; set; }
public string Address { get; set; }
public DateTime Birthdate { get; set; }
}

public interface IUser
{
string Name { get; }
string Address { get; }
DateTime Birthdate { get; }
}

In this case, the interface only defines the getters, but setters might
also be defined.

And some other class creates the user objects in a method:

public IUser GetUser()
{
User user = new User();
// set properties
return user;
}

Thanks,
Peter
Sep 24 '08 #1
4 1375
In most common scenarios, an interface here would be overkill and add
confusion. Additionally, coding against an interface (rather than a
class) has implications for data-binding (no inherited members, no
automatic new rows in grids), serialization (xml/dcs won't work) and
generics (no "new()" constraint). And probably many more.

There are some cases when it might be useful, but if you can't see
that it gains you anything then don't do it. Apart from anything else,
it is extra code to maintain for no good reason.

Marc
Sep 24 '08 #2
If your goal is to restrict certain properties to be read-only to public
users, then if you're using C# 2.0 or above you could perhaps (depending on
your project architecture) change the access level of your set property
instead:

public string Name
{
get
{
}
internal set
{
}
}
"Peter" <xd****@hotmail .comwrote in message
news:ek******** ******@TK2MSFTN GP02.phx.gbl...
Hi

I was wondering about the use of interfaces for "data classes".
Actually I don't know the accepted term for these types of classes -
they are simply classes which have getters and setters, to contain data
and not really provide any functions.

Is it worth defining interfaces for these types of classes, or is it
"overkill"?

Eg. I might have a class like:

public class User : IUser
{
public string Name { get; set; }
public string Address { get; set; }
public DateTime Birthdate { get; set; }
}

public interface IUser
{
string Name { get; }
string Address { get; }
DateTime Birthdate { get; }
}

In this case, the interface only defines the getters, but setters might
also be defined.

And some other class creates the user objects in a method:

public IUser GetUser()
{
User user = new User();
// set properties
return user;
}

Thanks,
Peter

Sep 24 '08 #3
Even when it seems like overkill, I would consider it more of a discipline.
You have to get used to referring to things by their interface name "on the
outside world" rather than their concrete implementation.

Writing and using a bunch of concrete classes all the time is not OO
programming.

User u = new User();

vs

IUser u = new User();
OR
IUser u = UserFactory.Cre ateNewUser();

Your controller classes definately need interfaces..... .

IUserController
IUser GetSingleUser(i nt customerId)
IUserCollection GetAllUsers()
void UpdateSingleUse r(IUser u)

You would see this very clearly with how WCF forces a contract....
But I 'hear ya', especially when you know you're only going to have 1
concrete for pretty much "all time", it seems like overkill a tad....
but go ahead and discipline yourself, and the one day that it really pays
off in a big way, you'll be like "Man, I'm glad I took that extra 4 minutes
back then".

One off shoot advantage is that you'll always be able to see a very clean
contract when you look at the interface definition.
Aka, when you pull up the code for IUser, it'll just be the contract, and
its much easier to look at then a concrete with lots and lots and lots of
lines of code.

I'm sure you'll get some other comments. Those are just a few from my side.

.............

Here's a small (downloadable) example of interfaces and WCF btw:
http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!158.entry

"Peter" <xd****@hotmail .comwrote in message
news:ek******** ******@TK2MSFTN GP02.phx.gbl...
Hi

I was wondering about the use of interfaces for "data classes".
Actually I don't know the accepted term for these types of classes -
they are simply classes which have getters and setters, to contain data
and not really provide any functions.

Is it worth defining interfaces for these types of classes, or is it
"overkill"?

Eg. I might have a class like:

public class User : IUser
{
public string Name { get; set; }
public string Address { get; set; }
public DateTime Birthdate { get; set; }
}

public interface IUser
{
string Name { get; }
string Address { get; }
DateTime Birthdate { get; }
}

In this case, the interface only defines the getters, but setters might
also be defined.

And some other class creates the user objects in a method:

public IUser GetUser()
{
User user = new User();
// set properties
return user;
}

Thanks,
Peter

Sep 24 '08 #4
Writing and using a bunch of concrete classes all the time is not OO
programming.
The choice to use an interface or a class is not a determining factor
in whether it is OO (ideally so long as both coding styles are
available for when each is appropriate).
You would see this very clearly with how WCF forces a contract....
WCF forces an interface for the service (methods etc) - but not for
the data objects.

Typically in WCF the data objects would be classes; [DataContract]
isn't even valid for interfaces. You can /possibly/ get this working
via assembly sharing, but the regular "mex" hooks don't like this very
much - things become "object" etc. I don't have access to the link you
posted, so I can't see what it does.

Marc
Sep 24 '08 #5

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

Similar topics

4
1506
by: Robert Zurer | last post by:
I notice that Microsoft puts their interfaces and classes in the same assembly. For example System.Data contains OleDbConnection and IDbConnection etc. I have found it useful to keep my interfaces in a separate assembly for a number of reasons. Among them are Avoiding circular references Hiding implementation from a remoting client Supporting polymorphic behavior across classes
17
2348
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes (this is my terminology and english is not my language so I hope you understand what I'm on about...).
30
2504
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking methods abstract (which is all that an interface does). In addition, the classes allow you to be flexible by adding functionality that child classes inherit. Or by providing optional functionality via virtual methods. Now, I understand that...
7
1585
by: Ant | last post by:
Hi, I’m wondering what practical use is there for creating interfaces. I can fully appreciate creating an abstract class for the purpose of inheriting, but why an interface? Is it just to promote polymorphism while keeping implementation unique? I dunno; I know I must be missing something big here. Any clues would, as always, be greatly appreciated. Thank you Ant
8
1569
by: Dave | last post by:
I have a set of developers who have gone off and implemented an interface for nearly all classes in a project\solution, now some of these classes will need interfaces as they implement the provider design pattern and other classes are exposed to the outside world, but the majority are internal classes and therefore IMO they don't require an interface. I can find plenty of articles on how to implement interfaces but I can't find any...
22
2102
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to a class, that when implemented, they require the implementing calss to make sure that each of the properties, functions etc. are handled by the class. (????) What I am really having a problem with is how they overcome the limitation
27
3846
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software interfaces. Like anything else, it appears Interfaces are by design something that requires documentation. I know this may be obvious, but if I have a class A and I say, well, class A implements IMyInterface. The fact that it implements...
5
3014
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if C# even has Iinterfaces. I took some Java programming classes and Interfaces are a main staple of Java. And in VB.Net I use interfaces for setting up definitions of classes. I am guessing that Abstract classes in C# do the same thing as...
23
2198
by: A.Gallus | last post by:
If I declare a function pure virtual: class A { virtual void myfunc() = 0; } and I derive a class from A: class B : public A
0
8613
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.