E
E
enchikiben2012-07-30 16:03:16
ASP.NET
enchikiben, 2012-07-30 16:03:16

Organization of classes in C#, passing different types of classes to the parameter

Good afternoon!
Not very familiar with C #, I'm trying to solve a problem (if at all possible), there is a class A that performs certain operations, 2 parameters are passed to its constructor, an instance of the class and a database (it does not affect my problem). Briefly its structure:

class A {
   protected B context;
   public A(B context, C BD){
       context = context;
   }
}

the context parameter (type B) is generated by the system and is read-only, and the turning point was to artificially slip your context (type D) with the necessary methods and data.
The question is how to do it? I was digging towards generic types, but something didn’t work out for me ...

PS I'm sorry, you can't inherit from class B, but its structure is known ...

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
lam0x86, 2012-07-30
@lam0x86

http://ru.wikipedia.org/wiki/Decorator (design pattern)

M
Monnoroch, 2012-07-30
@Monnoroch

If I correctly understood the task, then you can inherit D from B, and overload the necessary methods.

A
Andrey Polyakov, 2012-07-30
@magnitudo

Use dynamic

V
vmysla, 2012-09-18
@vmysla

the context parameter (type B) is generated by the system and is read-only, and the turning point was to artificially slip your context (type D) with the necessary methods and data.

Do you need this for tests, or “in general”? if “at all”, then you designed something wrong. sorry - the essence of the task is not clear ... think about it - you want to slip someone else's into an existing class that has its own context. how will other internal methods behave? what context will they use during your "special call", before and after it? and if some function saves something from the “other context”, how will this affect the behavior of the class?
I don’t know what you are doing, but there are a couple of options:
0. if you write tests, use mocks or stubs (if it’s really necessary)
1. BD – just a “connection” + affects the context
, everything is simple here – factory: IBdContext getBdContext(C BD), almost like yours
2. BD - smart, not just doing something, but also remembering something + affects context
almost the same, but “C BD” can turn out to be a factory, and at least a singleton.
3. context - just an object + affects the BD
bridge pattern. make your own implementations of IContext, and pass them to the "C BD" constructor
4. context is smart, not just does something, but also remembers something + affects the BD
factory IContext (C BD)
5. all smart and something remember - fantasize. something is wrong with the design.
6. all "stupid" - use static helpers.
999. think about it, if you "for a moment" need to slip an "alien object" - something is wrong.
BUT!:
A. if these are "comparation" methods - pass the context as an optional parameter to the function
B. if you have a tree structure with flexible logic, use a composite.
(a question the answer)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question