473,407 Members | 2,676 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,407 software developers and data experts.

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 1353
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**************@TK2MSFTNGP02.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.CreateNewUser();

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

IUserController
IUser GetSingleUser(int customerId)
IUserCollection GetAllUsers()
void UpdateSingleUser(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.spaces.live.com/Blog/cns!A68482B9628A842A!158.entry

"Peter" <xd****@hotmail.comwrote in message
news:ek**************@TK2MSFTNGP02.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
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...
17
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...
30
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...
7
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...
8
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...
22
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...
27
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...
5
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...
23
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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 project—planning, 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.