How to assign responsibility?

  • Thread starter Thread starter Marty McDonald
  • Start date Start date
M

Marty McDonald

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?
 
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.
 
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!
 
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.
 
Back
Top