473,395 Members | 1,791 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,395 software developers and data experts.

How to assign responsibility?

public abstract class Animal
{
private Id;
public

}

public class Dog : Animal
{
<<fields/properties for Dog>>
}

When Dog or any other derived class of Animal gets added to the database,
we'd like it to have a unique GUID in Id. This only applies for Insert, not
Select, Delete, Update. Responsibility for database I/O belongs to the
derived classes. But how do we force derived classes to invoke GetGuid? We
want GUID to be generated automatically, developers of derived classes
should not be required to know about this GUID or to generate it. Doing it
in constructor of Animal not preferable because GUID is only required for
Insert. Ideas?
Jul 21 '05 #1
3 1398
Hi Marty,

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to define a base class,
and you hope the derived class will be forced to call some code(generate
GUID), and you do not hope this is done in contructor.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

Based on my knowledge, a based class can not force the derived class to
call some code in other method than contructor. Since the design of
inheritability of Class is to let the derived to extend the base class and
the derived class can inherit the based class's method, but the derived
class can decide if it will call the base class's method.
e.g.
The
Console.WriteLine("Animal");
will always be called but the
Console.WriteLine("TestMethod in Animal");
will not, the override method will decided if it wants to do that.

namespace TestC_
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public abstract class Animal
{
public Animal()
{
Console.WriteLine("Animal");
}
public virtual void TestMethod()
{
Console.WriteLine("TestMethod in Animal");
}
}
public class Dog : Animal
{
public Dog()
{
Console.WriteLine("Dog");

}
public override void TestMethod()
{
Console.WriteLine("TestMethod in Dog");
//base.TestMethod();
}

[STAThread]
static void Main(string[] args)
{
Dog df = new Dog();
df.TestMethod();
}
}
}

So in your senario, the Dog will need to call the code that generates GUID
explicitly. Also you can write the code in a method and just the call the
method before you do the insert in the derived class.( I assume the Insert
function should be in the derived class)

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 21 '05 #2
Thank you for the explanation. After thinking about this for a while, I
have decided that we are doing this incorrectly. In this case, base and
derived classes should represent entities (database record). They should
not be responsible for persisting themselves to the database. We are
violating cohesion, and are tightly coupling our entities to persistence
logic.

I think we should have a base class with common attributes (such as Id),
then derived classes can extend that as necessary. To me, the unique Id is
a business rule, and should therefore be handled by a business class (or
maybe data persistence class). I will probably code like this...
entity : base (represent entity) ===> business layer (populates id, invokes
data layer) ===> data persistence layer (performs database I/O)

Thanks for the information!
Jul 21 '05 #3
Hi Marty,

Thanks for your quickly reply!

I am glad that my suggestion are helpful for you.
If you have any concern on this issue, please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 21 '05 #4

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

Similar topics

4
by: N | last post by:
Is there anyone can tell me if the following statement is true? DBA are not experts on SQL. So they are not programmers and can't always answer programming questions. The example of...
5
by: vince | last post by:
I want to assign a null value to a DateTime variable.How do i do that? Thanks Vince
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
3
by: Marty McDonald | last post by:
public abstract class Animal { private Id; public } public class Dog : Animal { <<fields/properties for Dog>>
6
by: david | last post by:
I try to use "for" loop to retrieve and assign values in web form. The code is in the following. But it can not be compiled. What I want to do is: txtQ1.Text =...
4
by: Chris Roth | last post by:
vector<doublev1(5,1); vector<doublev2; v2 = v1; // 1 v2.assign(v1.begin(),v1.end()); // 2 Are 1 and 2 the same, or are their subtle differences between them. Which is preferable, if either? ...
2
by: Ora | last post by:
Hi all, Can any one help me in getting the list of users associated with a responsibility in Oracle Apps. Thanks,
2
by: Cramer | last post by:
More of a theoretical question here: It just occurred to me that the ASP.NET request pipeline delivers much of the GoF Chain of Responsibility pattern. What do you think? If it does not, then, why...
3
by: vamsioracle | last post by:
Can any one help me out, how to implement a rule for delegation of responsibility in SSHR Absence management. Thanks vamsi
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...

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.